-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
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 type num and is promoted to type int. 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 type int. 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 be int. That check fails, and the CFE reports the compile-time error shown below.
test() {
var (num b,) = (0,);
b as int;
b.isEven;
(b,) = (3.14,);
//b = 3.14;
}// /tmp/asdf3.dart:5:11: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
// (b,) = (3.14,);
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-L37
So, my question is which one of the two behaviors is expected from the compiler?