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)
- Simplify branch conditions based on ranges & simplify control flow afterwards if condition turns out to be constant
- Issue in number multiplication with 10, 20, etc HOT 4
- Feature Request: Add `--insert-ignore-comments` flag to `dart fix` command for existing code HOT 3
- [vm/core] `_GrowableList.add` has unnecessary bounds check HOT 1
- docImports failing to allow for resolution in some flutter animation library code HOT 4
- Implement augmentations HOT 12
- Support macros when analyzer is used as a library HOT 5
- Is that possible to set breakpoints in the macro code itself? HOT 2
- Inability to Convert Dart BigInt to JSBigInt using new js_interop HOT 1
- Dart CLI commands hang on a slow/unstable internet connection due to analytics HOT 8
- Types Generated using the LibraryTypesMacro throws an error when instantiating HOT 2
- Analyzer Feedback from IntelliJ HOT 1
- Doc comment references cannot resolve an extension member when prefixed by the extended type's name HOT 11
- Different augmented output from macro during development and program running
- pkg/dds/test/devtools_server/instance_reuse_test timing out HOT 4
- [Bug] Uri.parse failes to currectly parse url like: http://user:^@example.com HOT 5
- string.allMatches returns an Iterable of length pattern.length + 1 when aString is empty HOT 6
- [CP] Pub: Fix to using path dependencies in git dependencies on windows HOT 2
- The analyzer encounters an unhandled exception during constant verification in a switch statement HOT 1
- Erasure of extension types in DartObject is blocking adoption of dart:js_interop HOT 8
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.