-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Type promotion failed in named records #55473
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
This looks like it's working as currently intended. (Edit, narrator voice: It did not.) Code inside a closure cannot promote a variable if there are assignments to it outside of the closure (maybe only later). The fact that the only assignment is to a non- |
So is this an error that belongs to the IDE plug-in? I did not get any error prompts in the vscode Dart plug-in v3.86.0. |
If it's an unnamed record type, this code can work properly. void main() {
int? a;
Future(
() {
(String, String) test = (
"${a!.isEven}",
"${a.isEven}",
);
print(test);
},
);
a = 0;
}
|
Good point. It seems like DDC is promoting the variable in one case, so it should in the other too. |
In our codebase we had a similar case (and given that it's a closure parameter (which could even be void main() {
f((x) => [x!.a, x.b]); // OK
f((x) => (x!.a, x.b)); // OK
f((x) => (a: x!.a, b: x.b)); // ERR
}
void f(void Function(X?) c) {
c(X());
}
class X {
final a = 1;
final b = 2;
} passes
|
Seems to be related to information flow between expressions in a record expression that contains named fields. void main() {
({int x, int y})? a = (x: 42, y: 37) as dynamic;
var yes = DateTime.now().millisecondsSinceEpoch > 0;
// Named fields, requires promotion to flow between field expressions.
// All fail.
if (yes) {
print((a!.x, n2: a.y)); // ERR
}
if (yes) {
print((n1: a!.x, a.y)); // ERR
}
if (yes) {
print((n1: a!.x, n2: a.y)); // ERR
}
if (yes) {
print((a!.x, a.y, n3: 0)); // ERR
}
// Promotion flow out of record. Works.
if (yes) {
print((a!.x, n2: 0));
print(a.y);
}
if (yes) {
print((n1: a!.x, n2: 0));
print(a.y);
}
// Promotion flow inside field expression. Works.
if (yes) {
print((a!.x + a.y, n2: 0));
}
if (yes) {
print((n1: a!.x + a.y, n2: 0));
}
// No mamed fields in same record as promotion. Works.
if (yes) {
print((a!.x, (n2: a.y)));
print(a.y);
}
if (yes) {
print((a!.x, a.y));
}
// Not records, all work.
if (yes) {
print([a!.x, a.y]);
}
if (yes) {
foo(a!.x, a.y);
}
if (yes) {
bar(x: a!.x, y: a.y);
}
}
void foo([int x = 0, int y = 0]) {
print("$x,$y");
}
void bar({int x = 0, int y = 0}) {
print("$x,$y");
} |
Run the following code in DartPad:
The text was updated successfully, but these errors were encountered: