-
Notifications
You must be signed in to change notification settings - Fork 29
#3057. Add more null-check tests #3081
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d77d014
#3057. Add more null-check tests
sgrekhov 8939a0a
Apply review suggestions
sgrekhov 9b4b698
Update reachability_A15_t03.dart
eernstg 45a8cac
Update reachability_A15_t04.dart
eernstg c4488f1
Update reachability_A17_t03.dart
eernstg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Local variable conditional assignment: If `N` is an expression | ||
| /// of the form `x ??= E1` where `x` is a local variable, then: | ||
| /// - Let `before(E1) = split(promote(x, Null, before(N)))`. | ||
| /// - Let `M1 = assign(x, E1, after(E1))` | ||
| /// - Let `M2 = split(promoteToNonNull(x, before(N)))` | ||
| /// - Let `after(N) = merge(M1, M2)` | ||
| /// | ||
| /// @description Checks that flow analysis promotes a local variable after a | ||
| /// conditional assignment. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import '../../Utils/static_type_helper.dart'; | ||
|
|
||
| class A {} | ||
| class C extends A { | ||
| void foo() {} | ||
| } | ||
|
|
||
| void test(A? o) { | ||
| o ??= C(); | ||
| o.expectStaticType<Exactly<A>>(); | ||
| } | ||
|
|
||
| main() { | ||
| test(A()); | ||
| test(C()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Local variable conditional assignment: If `N` is an expression | ||
| /// of the form `x ??= E1` where `x` is a local variable, then: | ||
| /// - Let `before(E1) = split(promote(x, Null, before(N)))`. | ||
| /// - Let `M1 = assign(x, E1, after(E1))` | ||
| /// - Let `M2 = split(promoteToNonNull(x, before(N)))` | ||
| /// - Let `after(N) = merge(M1, M2)` | ||
| /// | ||
| /// @description Checks that if `x` is non-nullable then `E1` is dead code. | ||
| /// @author sgrekhov22@gmail.com | ||
| /// @issue 60114 | ||
|
|
||
| void test(int o) { | ||
| late int i; | ||
| o ??= (i = 42); // `i` is initialized in dead code | ||
| i; // Definitely unassigned | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| } | ||
|
|
||
| main() { | ||
| print(test); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Local variable conditional assignment: If `N` is an expression | ||
| /// of the form `x ??= E1` where `x` is a local variable, then: | ||
| /// - Let `before(E1) = split(promote(x, Null, before(N)))`. | ||
| /// - Let `M1 = assign(x, E1, after(E1))` | ||
| /// - Let `M2 = split(promoteToNonNull(x, before(N)))` | ||
| /// - Let `after(N) = merge(M1, M2)` | ||
| /// | ||
| /// @description Checks that if `x` is nullable then `E1` is not dead code. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| void test(int? o) { | ||
| late int i; | ||
| o ??= (i = 42); | ||
| try { | ||
| i; | ||
| } catch (_) {} | ||
| } | ||
|
|
||
| main() { | ||
| test(1); | ||
| test(null); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Local variable conditional assignment: If `N` is an expression | ||
| /// of the form `x ??= E1` where `x` is a local variable, then: | ||
| /// - Let `before(E1) = split(promote(x, Null, before(N)))`. | ||
| /// - Let `M1 = assign(x, E1, after(E1))` | ||
| /// - Let `M2 = split(promoteToNonNull(x, before(N)))` | ||
| /// - Let `after(N) = merge(M1, M2)` | ||
| /// | ||
| /// @description Checks that if `x` is a subtype of `Null` then `E1` is | ||
| /// always executed. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import '../../Utils/expect.dart'; | ||
|
|
||
| void test<T extends Null>(T n) { | ||
| int i; | ||
| n ??= (i = 42) as dynamic; | ||
| i; // Definitely assigned | ||
| } | ||
|
|
||
| main() { | ||
| Expect.throws(() { | ||
| test(null); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Conditional assignment to a non local-variable: If `N` is an | ||
| /// expression of the form `E1 ??= E2` where `E1` is not a local variable, and | ||
| /// the type of `E1` is `T1`, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - If `T1` is strictly non-nullable, then: | ||
| /// - Let `before(E2) = unreachable(after(E1))`. | ||
| /// - Let `after(N) = after(E1)`. | ||
| /// - Otherwise: | ||
| /// - Let before(E2) = split(after(E1)). | ||
| /// - Let after(N) = merge(after(E2), split(after(E1))). | ||
| /// | ||
| /// @description Checks that if `E1` is not a local variable and is strictly | ||
| /// non-nullable, then `before(E2) = unreachable(after(E1))`. | ||
| /// @author sgrekhov22@gmail.com | ||
| /// @issue 60114 | ||
|
|
||
| class C { | ||
| int m = 0; | ||
| test() { | ||
| late int i; | ||
| m ??= (i = 42); | ||
| i; // Definitely unassigned | ||
| // ^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| } | ||
| } | ||
|
|
||
| int n = 0; | ||
|
|
||
| main() { | ||
| late int i; | ||
| n ??= (i = 42); | ||
| i; | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| print(C); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Conditional assignment to a non local-variable: If `N` is an | ||
| /// expression of the form `E1 ??= E2` where `E1` is not a local variable, and | ||
| /// the type of `E1` is `T1`, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - If `T1` is strictly non-nullable, then: | ||
| /// - Let `before(E2) = unreachable(after(E1))`. | ||
| /// - Let `after(N) = after(E1)`. | ||
| /// - Otherwise: | ||
| /// - Let before(E2) = split(after(E1)). | ||
| /// - Let after(N) = merge(after(E2), split(after(E1))). | ||
| /// | ||
| /// @description Checks that if `E1` is not a local variable and is nullable, | ||
| /// then `E2` is not dead code. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| class C { | ||
| int? m; | ||
| C(this.m); | ||
|
|
||
| test() { | ||
| late int i; | ||
| m ??= (i = 42); | ||
| try { | ||
| i; | ||
| } catch (_) {} | ||
| } | ||
| } | ||
|
|
||
| int? n; | ||
|
|
||
| main() { | ||
| late int i; | ||
| n ??= (i = 42); | ||
| i; | ||
|
|
||
| C(0); | ||
| C(null); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Conditional assignment to a non local-variable: If `N` is an | ||
| /// expression of the form `E1 ??= E2` where `E1` is not a local variable, and | ||
| /// the type of `E1` is `T1`, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - If `T1` is strictly non-nullable, then: | ||
| /// - Let `before(E2) = unreachable(after(E1))`. | ||
| /// - Let `after(N) = after(E1)`. | ||
| /// - Otherwise: | ||
| /// - Let before(E2) = split(after(E1)). | ||
| /// - Let after(N) = merge(after(E2), split(after(E1))). | ||
| /// | ||
| /// @description Checks that if `E1` is not a local variable and is nullable, | ||
| /// then flow analysis doesn't affect the static type of `E1`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import '../../Utils/static_type_helper.dart'; | ||
|
|
||
| class A {} | ||
| class B extends A { | ||
| void foo() {} | ||
| } | ||
|
|
||
| A? o; | ||
|
|
||
| class C { | ||
| A? m; | ||
| test() { | ||
| m ??= B(); | ||
| m.expectStaticType<Exactly<A?>>(); | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| o ??= B(); | ||
| o.expectStaticType<Exactly<A?>>(); | ||
| C().test(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Conditional assignment to a non local-variable: If `N` is an | ||
| /// expression of the form `E1 ??= E2` where `E1` is not a local variable, and | ||
| /// the type of `E1` is `T1`, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - If `T1` is strictly non-nullable, then: | ||
| /// - Let `before(E2) = unreachable(after(E1))`. | ||
| /// - Let `after(N) = after(E1)`. | ||
| /// - Otherwise: | ||
| /// - Let before(E2) = split(after(E1)). | ||
| /// - Let after(N) = merge(after(E2), split(after(E1))). | ||
| /// | ||
| /// @description Checks that if `E1` is not a local variable and is a subtype | ||
| /// of `Null`, then `E2` is always executed. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import '../../Utils/expect.dart'; | ||
|
|
||
| class C<T extends Null> { | ||
| T m; | ||
| C(this.m); | ||
|
|
||
| test() { | ||
| int i; | ||
| m ??= (i = 42) as dynamic; | ||
| i; // Definitely assigned | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| Expect.throws(() { | ||
| C(null).test(); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - Conditional expression: If `N` is a conditional expression of | ||
| /// the form `E1 ? E2 : E3`, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - Let `before(E2) = split(true(E1))`. | ||
| /// - Let before(E3) = split(false(E1))`. | ||
| /// - Let `after(N) = merge(after(E2), after(E3))`. | ||
| /// - Let `true(N) = merge(true(E2), true(E3))`. | ||
| /// - Let `false(N) = merge(false(E2), false(E3))`. | ||
| /// | ||
| /// @description Checks flow analysis after conditional expression. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| void test1() { | ||
| late int i; | ||
| int j; | ||
| true ? j = 42 : i = 42; | ||
| j; // Ok, j is definitely assigned | ||
| i; | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| } | ||
|
|
||
| void test2() { | ||
| late int i; | ||
| int j; | ||
| false ? i = 42 : j = 42; | ||
| j; // Ok, j is definitely assigned | ||
| i; | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| } | ||
|
|
||
| main() { | ||
| print(test1); | ||
| print(test2); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| /// @assertion - If-null: If `N` is an if-null expression of the form | ||
| /// `E1 ?? E2`, where the type of E1 is T1, then: | ||
| /// - Let `before(E1) = before(N)`. | ||
| /// - If `T1` is strictly non-nullable, then: | ||
| /// - Let `before(E2) = unreachable(after(E1))`. | ||
| /// - Let `after(N) = after(E1)`. | ||
| /// - Otherwise: | ||
| /// - Let `before(E2) = split(after(E1))`. | ||
| /// - Let `after(N) = merge(after(E2), split(after(E1)))`. | ||
| /// | ||
| /// @description Checks that if `E1` is non-nullable then `E2` is unreachable. | ||
| /// @author sgrekhov22@gmail.com | ||
| /// @issue 60114 | ||
|
|
||
| void test(int x) { | ||
| late int i; | ||
| x ?? (i = 42); // `i` is initialized in dead code | ||
| i; // Definitely unassigned | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
| } | ||
|
|
||
| int y = 0; | ||
|
|
||
| main() { | ||
| late int i; | ||
| y ?? (i = 42); | ||
| i; | ||
| //^ | ||
| // [analyzer] unspecified | ||
| // [cfe] unspecified | ||
|
|
||
| print(test); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.