Typevar matching fix + implicit conversions from Scalar to int/float (#26453)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/26453
Previously, schema matching would incorrectly widen typevar bindings
when later occurrences were supertypes of earlier ones. This allowed
callsites like `floatlist.append(tensor.item())` to pass the typechecker,
causing a runtime assert (issue #24856).
An earlier, reverted fix (#25136) insisted on strict equality across all
occurrences of a typevar, necessitating explicit casts around Scalar-typed
arguments to int- or float-typed parameters, like `tensor.item()` above.
This was per the original type system design, but turned out to break
existing user code that relied on the de facto dynamic downcast. (The
error required a specialized list representation.)
The current fix includes the prevention of typevar widening, but
adds logic to insert implicit conversions from Scalar to float or int
as needed to satisfy a matched schema.
Test Plan: Imported from OSS
Differential Revision: D17470598
Pulled By: bhosmer
fbshipit-source-id: d260dbf3cd78b9c2f2229bc61afc84e1910b5659