source_gen icon indicating copy to clipboard operation
source_gen copied to clipboard

isAssignableFromType throws null check error for Function types

Open leonsenft opened this issue 3 years ago • 5 comments

The implementation null asserts DartType.element2: https://github.com/dart-lang/source_gen/blob/0d4c54d2a299643ba2a783ef2e9fcf878305a62d/source_gen/lib/src/type_checker.dart#L170-L171

Which is always null for Function types: https://github.com/dart-lang/sdk/blob/d6ff1193c5eb1d4b98e232f545cce49c63492b5c/pkg/analyzer/lib/src/dart/element/type.dart#L95-L96

leonsenft avatar Sep 09 '22 21:09 leonsenft

@grouma this has been the cause of several unique unhandled exceptions in the Angular compiler.

leonsenft avatar Sep 09 '22 21:09 leonsenft

cc @natebosch

grouma avatar Sep 09 '22 21:09 grouma

Making an implementation that works for structural types is not trivial.

I think that these types also don't work in the angular compiler though - you can't inject a Function type right? Is there some safeguard you can move earlier in the compilation to prevent this?

natebosch avatar Sep 09 '22 22:09 natebosch

I understand that, but perhaps it would make sense to throw an explicit error instead of knowingly failing a null check.

For now, I'm creating an extension method to wrap this method and throw such an error if the underlying type is a FunctionType.

leonsenft avatar Sep 09 '22 22:09 leonsenft

Yeah, I think we can throw some more informative errors, and perhaps even more eagerly in some cases.

natebosch avatar Sep 09 '22 22:09 natebosch