-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Pattern match missing inference and generic method instantiation #51871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yeah, my suspicion is that this is just a bug where the context type of the relational method isn't be passed down for downwards inference to the relational pattern's constant operand. I should note that this is a bug, but it's a pretty trivial one—almost no real-world relational or equality operators have a type annotation for the RHS that would lead to any interesting type inference. |
@munificent I have some questions about generalizing that kind of coercion to other pattern-related spots. Should the same be performed in switch expressions, for example, in the following program? T id<T>(T t) => t;
typedef IntFn = int Function(int);
typedef TFn = T Function<T>(T);
class Compare {
operator <(IntFn f) => f is TFn;
}
test1() {
const c = id;
return switch(Compare()) {
< c => throw "Error",
_ => "OK"
};
}
main() {
test1();
} Generalizing even more, should all types of coercions be enabled for the operands of all relational patterns in all pattern-related language constructs? If so, the following CFE test, which currently is compiled without any errors, will be rejected because of the attempt to coerce string |
We do not have a specification of the precise set of locations where coercions are applied (they are specified to be applicable everywhere, based on the context type and the type of the expression only, but they have always been implemented such that they were only applied in a smaller set of locations aka assignment locations). This means that we can't find anything about not applying coercions outside those assignment locations in the language specification or any other specification document. However, I believe that we will specify this; in particular, there is no support in the language team for changing |
Thank you for the clarification, Erik, and for confirming that the existing CFE unit test should indeed be treated as expecting a compile-time error. I'm working on the fix at https://dart-review.googlesource.com/c/sdk/+/292001. |
A missing generic instantiation is causing a compile time error here:
sdk/tests/language/patterns/implicit_instantiation_test.dart
Lines 32 to 41 in 0d000e8
In a discussion with @munificent he mentioned that this pattern match should have the same downward inference and generic instantiation as this similar code that doesn't contain any patterns and runs without any errors:
cc @johnniwinther
The text was updated successfully, but these errors were encountered: