[JIT] Display an error message when with item is not an object (#52335)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52335
**Summary**
`with` statements can only be used with objects that have `__enter__`
and `__exit__` defined. At present, any attempt to use an expression
that returns something that is not an instance of a class type results
in a cryptic internal assert failure instead of a useful error message.
This is because the code that generates IR for `with` statements uses
`Type::expect` as if it were `Type::cast`; that is, as if it returns
`nullptr` on failure.
This commit fixes this issue by checking the `kind()` of the type of the
expression used as the with item before calling `expect<ClassType>()` on
it.
**Test Plan**
This commit adds a unit test to `test_with_errors` to test this case.
Test Plan: Imported from OSS
Reviewed By: gmagogsfm
Differential Revision: D26504909
Pulled By: SplitInfinity
fbshipit-source-id: 92d108e0c010370fd45131a57120f50c0b85c401