Skip to content

#3057. Add more for-statement tests #3110

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 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that if variable is assigned `after(D)` then it is definitely assigned
/// `before(C)`.
/// @author [email protected]

test1() {
int n;
for (n = 42; n > 0;) { // n definitely assigned
break;
}
}

test2() {
int n;
[
for (n = 42; n < 0;)
0
];
}

test3() {
int n;
<int, int>{
for (n = 42; n < 0;)
0: 0
};
}

main() {
test1();
test2();
test3();
}
70 changes: 70 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that `assignedIn(U)` is detected by flow analysis.
/// @author [email protected]

test1() {
late int n;
for (
;
() {
if (1 > 2) {
n; // possibly assigned
}
return true;
}();
n = 42
) {}
}

test2() {
late int n;
[
for (
;
() {
if (1 > 2) {
n;
}
return true;
}();
n = 42
)
0,
];
}

test3() {
late int n;
<int, int>{
for (
;
() {
if (1 > 2) {
n;
}
return true;
}();
n = 42
)
0: 0,
};
}

main() {
print(test1);
print(test2);
print(test3);
}
86 changes: 86 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that `assignedIn(S)` is detected by flow analysis.
/// @author [email protected]

test1() {
late int n;
for (
;
() {
if (1 > 2) {
n; // possibly assigned
}
return true;
}();
) {
n = 42;
}
}

test2() {
late int n;
[
for (
;
() {
if (1 > 2) {
n;
}
return true;
}();
)
n = 42,
];
}

test3() {
late int n;
<int, int>{
for (
;
() {
if (1 > 2) {
n;
}
return true;
}();
)
n = 42: 0,
};
}

test4() {
late int n;
<int, int>{
for (
;
() {
if (1 > 2) {
n;
}
return true;
}();
)
0: n = 42,
};
}

main() {
print(test1);
print(test2);
print(test3);
print(test4);
}
52 changes: 52 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t06.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that an assignment in `C` doesn't affect value of `before(D)`.
/// @author [email protected]

test1() {
late int n;
for (n; (n = 42) < 0;) { // Definitely unassigned
// ^
// [analyzer] unspecified
// [cfe] unspecified
break;
}
}

test2() {
int n;
[
for (n; (n = 42) < 0;) 0
// ^
// [analyzer] unspecified
// [cfe] unspecified
];
}

test3() {
int n;
<int, int>{
for (n; (n = 42) < 0;) 0: 0
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
}

main() {
print(test1);
print(test2);
print(test3);
}
52 changes: 52 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t07.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that an assignment in `U` doesn't affect value of `before(D)`.
/// @author [email protected]

test1() {
late int n;
for (n;; n = 42) { // Definitely unassigned
// ^
// [analyzer] unspecified
// [cfe] unspecified
break;
}
}

test2() {
int n;
[
for (n; ;n = 42) 0
// ^
// [analyzer] unspecified
// [cfe] unspecified
];
}

test3() {
int n;
<int, int>{
for (n; ;n = 42) 0: 0
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
}

main() {
print(test1);
print(test2);
print(test3);
}
64 changes: 64 additions & 0 deletions TypeSystem/flow-analysis/reachability_for_A01_t08.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 for statement: If `N` is a for statement of the form
/// `for (D; C; U) S,` then:
/// - Let `before(D) = before(N)`.
/// - Let `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`
/// - Let `before(S) = split(true(C))`.
/// - Let `before(U) = merge(after(S), continue(S))`.
/// - Let `after(N) = inheritTested(join(false(C), unsplit(break(S))), after(U))`
///
/// @description Checks that
/// `before(C) = conservativeJoin(after(D), assignedIn(N), capturedIn(N))`. Test
/// that an assignment in `S` doesn't affect value of `before(D)`.
/// @author [email protected]

test1() {
late int n;
for (n;;) { // Definitely unassigned
// ^
// [analyzer] unspecified
// [cfe] unspecified
n = 42;
break;
}
}

test2() {
int n;
[
for (n; ;) n = 42
// ^
// [analyzer] unspecified
// [cfe] unspecified
];
}

test3() {
int n;
<int, int>{
for (n; ;) n = 42: 0
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
}

test4() {
int n;
<int, int>{
for (n; ;) 0: n = 42
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
}

main() {
print(test1);
print(test2);
print(test3);
print(test4);
}
Loading