-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Should the pattern schema context cause assignability check failures? #51585
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
/cc @eernstg |
That's fine, the pattern type schema When type inference is complete, the static type of the right hand side of the assignment is used to fill in the blanks of the pattern on the left hand side. In this case it's a record pattern, which means that there are no actual type arguments, so nothing happens to the top level of the pattern. Assignability is checked for the top level, and it succeeds: The initializing expression has type The pattern is then traversed, so we visit each field and fill in missing parts, and check assignability: The first positional field has declared type I don't know how the demotion of |
Thanks for the explanation, Erik! I have a question about one detail: why is the required type of the record pattern |
That's exactly because the required type of a record pattern should "check the shape, but not the types". It's up to the individual subpatterns to ensure assignability (after coercions and/or demotions as needed) of each field of the initializing expression unto the corresponding field pattern. The relevant spec location is here. |
Thanks, Erik! |
I encountered an issue working on https://dart-review.googlesource.com/c/sdk/+/277004, and I have a question about pattern schemas and how they affect type inference in expressions.
Consider the following program. The variable
b
has the typenum
and is promoted to typeint
. Later it is used in a pattern assignment expression. In that expression the pattern schema is computed first, and it's(int)
, that is, a record type with one positional field of typeint
. That pattern schema is used as the context for(3.14,)
, and when it comes to the individual fields of that record literal,3.14
is checked for assignability to the type of the field, which is inferred to beint
. That check fails, and the CFE reports the compile-time error shown below.However, that behavior is different from non-pattern assignment. The line
b = 3.14;
doesn't produce a compile-time error. Additionally, some tests expect the pattern assignment expression to be accepted by the compiler, for example, the following: https://github.com/dart-lang/co19/blob/7b4f3e723211011878e185db6fa57fbafabd6bb7/LanguageFeatures/Patterns/type_inference_A26_t01.dart#L34-L37So, my question is which one of the two behaviors is expected from the compiler?
The text was updated successfully, but these errors were encountered: