Comments (7)
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).
That is the case here.
The fact that the only assignment is to a non-null
isn't taken into account. The value may change at a time that is not known to not happen between the a!
and the a.isEven
, and that's enough to disable promotion.
from sdk.
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.
from sdk.
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;
}
from sdk.
Good point. It seems like DDC is promoting the variable in one case, so it should in the other too.
from sdk.
from sdk.
In our codebase we had a similar case (and given that it's a closure parameter (which could even be final
)) I felt this is a stronger example of the issue:
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 dart analyze
(which wants one to not add a !
to x.b
), but fails compilation with
compileDDC
main.dart:3:33: Error: Property 'b' cannot be accessed on 'X?' because it is potentially null.
- 'X' is from 'package:dartpad_sample/main.dart' ('/tmp/dartpadTJDUDK/lib/main.dart').
Try accessing using ?. instead.
f((final x) => (a: x!.a, b: x.b));
^
from sdk.
Seems to be related to information flow between expressions in a record expression that contains named fields.
(As in: it's not there. The promotions of a prior field are not used as starting state for the next field expression, but only if the record expression itself 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");
}
from sdk.
Related Issues (20)
- Type inference for abstract classes with recurring generics can't be inferred HOT 10
- The front end is not happy about wrong constructor declaration
- The front end is not happy about wrong extension type constructor declaration
- pkg/analyzer/test/src/dart/resolution/augmented_invocation_test fails with RuntimeError on analyzer-asserts-win bot HOT 2
- Code completion suggestions are irrelevant / context unaware HOT 2
- [Wildcard Variables] Language Tests in `tests/language` HOT 5
- [Wildcard Variables] All tests in `tests/language` and co19 passing.
- [Wildcard Variables] Experiment Flag HOT 3
- [Wildcard Variables] CFE Implementation
- [CP] Cherry-Pick "[cfe] Ensure default values in synthesized function nodes" HOT 6
- [vm] use_code_comments_flag_test timing out on vm-aot-linux-release-simarm_x64 HOT 1
- [analysis_server] Non-const constructors aren't suggested in a const context HOT 2
- Move analyzer test utilities to analyzer_utilities package HOT 3
- [Wildcard Variables] Feature Specification HOT 6
- Problems with moving comments when automatically moving child arg to last parameter position of widget constructor
- Unreachable code during SelectRepresentations HOT 1
- "Stop on uncaught exceptions" sometimes stops on caught exceptions HOT 4
- Improve thrown `FormatException` from `int.parse` when there is an "invalid radix-n" number HOT 1
- Very poor JIT performance with const Map<Type, ..> HOT 7
- DateTime.fromMillisecondsSinceEpoch constructor returns invalid time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.