From 55384eb5ddd086b59c61b32d6fd38b303c209225 Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Mon, 10 Feb 2025 16:14:13 +0200 Subject: [PATCH] #3057. Add more flow analysis tests for type Never. Part 2. --- .../flow-analysis/reachability_A01_t17.dart | 34 ++++++++ .../flow-analysis/reachability_A01_t18.dart | 34 ++++++++ .../flow-analysis/reachability_A01_t19.dart | 49 ++++++++++++ .../flow-analysis/reachability_A01_t20.dart | 34 ++++++++ .../flow-analysis/reachability_A01_t21.dart | 34 ++++++++ .../flow-analysis/reachability_A01_t22.dart | 34 ++++++++ .../flow-analysis/reachability_A01_t23.dart | 80 +++++++++++++++++++ .../flow-analysis/reachability_A01_t24.dart | 80 +++++++++++++++++++ .../flow-analysis/reachability_A01_t25.dart | 80 +++++++++++++++++++ 9 files changed, 459 insertions(+) create mode 100644 TypeSystem/flow-analysis/reachability_A01_t17.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t18.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t19.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t20.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t21.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t22.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t23.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t24.dart create mode 100644 TypeSystem/flow-analysis/reachability_A01_t25.dart diff --git a/TypeSystem/flow-analysis/reachability_A01_t17.dart b/TypeSystem/flow-analysis/reachability_A01_t17.dart new file mode 100644 index 0000000000..24a3095af0 --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t17.dart @@ -0,0 +1,34 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a list literal. +/// @author sgrekhov22@gmail.com + +void test(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + n, // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + ]; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t18.dart b/TypeSystem/flow-analysis/reachability_A01_t18.dart new file mode 100644 index 0000000000..6185e439af --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t18.dart @@ -0,0 +1,34 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a map literal. +/// @author sgrekhov22@gmail.com + +void test(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + n, // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t19.dart b/TypeSystem/flow-analysis/reachability_A01_t19.dart new file mode 100644 index 0000000000..1722dadf62 --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t19.dart @@ -0,0 +1,49 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a map literal. +/// @author sgrekhov22@gmail.com + +void test1(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + 1: n, // The code after this point is unreachable + 2: i = 42}; // Variable is initialized in a dead code. This leaves it definitely unassigned + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test2(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + n: // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test1); + print(test2); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t20.dart b/TypeSystem/flow-analysis/reachability_A01_t20.dart new file mode 100644 index 0000000000..89155602fa --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t20.dart @@ -0,0 +1,34 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test an `if` statement in a list literal. +/// @author sgrekhov22@gmail.com + +void test(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + if (n is String) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + ]; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t21.dart b/TypeSystem/flow-analysis/reachability_A01_t21.dart new file mode 100644 index 0000000000..611c286c52 --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t21.dart @@ -0,0 +1,34 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test an `if` statement in a set literal. +/// @author sgrekhov22@gmail.com + +void test(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + if (n is String) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t22.dart b/TypeSystem/flow-analysis/reachability_A01_t22.dart new file mode 100644 index 0000000000..9cb253bf8a --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t22.dart @@ -0,0 +1,34 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test an `if` statement in a map literal. +/// @author sgrekhov22@gmail.com + +void test(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + if (n is String) // The code after this point is unreachable + 1: i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t23.dart b/TypeSystem/flow-analysis/reachability_A01_t23.dart new file mode 100644 index 0000000000..9a43d4205a --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t23.dart @@ -0,0 +1,80 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a `for(;;)` statement in a list literal. +/// @author sgrekhov22@gmail.com +/// @issue 60089 + +void test1(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + for (n;;) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + ]; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test2(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + for (;n;) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + ]; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test3(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + for (;;n) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + ]; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test4(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + [ + for (var j = 0; j < 0; n) // Ok, not a dead code + i = 42 + ]; + } + i; // Not an error +} + +main() { + print(test1); + print(test2); + print(test3); + print(test4); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t24.dart b/TypeSystem/flow-analysis/reachability_A01_t24.dart new file mode 100644 index 0000000000..a193177583 --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t24.dart @@ -0,0 +1,80 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a `for(;;)` statement in a set literal. +/// @author sgrekhov22@gmail.com +/// @issue 60089 + +void test1(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (n;;) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test2(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (; n;) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test3(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (;; n) // The code after this point is unreachable + i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test4(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (var j = 0; j < 0; n) // Ok, not a dead code + i = 42 + }; + } + i; // Not an error +} + +main() { + print(test1); + print(test2); + print(test3); + print(test4); +} diff --git a/TypeSystem/flow-analysis/reachability_A01_t25.dart b/TypeSystem/flow-analysis/reachability_A01_t25.dart new file mode 100644 index 0000000000..43858afc25 --- /dev/null +++ b/TypeSystem/flow-analysis/reachability_A01_t25.dart @@ -0,0 +1,80 @@ +// 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 +/// - Variable or getter: If `N` is an expression of the form `x` where the +/// type of `x` is `T` then: +/// If `T <: Never` then: +/// - Let `after(N) = unreachable(before(N))`. +/// Otherwise: +/// - Let `after(N) = before(N)`. +/// +/// @description Checks that the code is unreachable after a variable of type +/// `Never`. Test a `for(;;)` statement in a map literal. +/// @author sgrekhov22@gmail.com +/// @issue 60089 + +void test1(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (n;;) // The code after this point is unreachable + 1: i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test2(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (; n;) // The code after this point is unreachable + 1: i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test3(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (;; n) // The code after this point is unreachable + 1: i = 42 // Variable is initialized in a dead code. This leaves it definitely unassigned + }; + } + i; // It is an error to read a local late variable when it is definitely unassigned. +//^ +// [analyzer] unspecified +// [cfe] unspecified +} + +void test4(Never n) { + late int i; + bool b = (() => true)(); + if (b) { + { + for (var j = 0; j < 0; n) // Ok, not a dead code + 1: i = 42 + }; + } + i; // Not an error +} + +main() { + print(test1); + print(test2); + print(test3); + print(test4); +}