diff --git a/TypeSystem/flow-analysis/promotion_via_assignment_A05_t01.dart b/TypeSystem/flow-analysis/promotion_via_assignment_A05_t01.dart new file mode 100644 index 0000000000..5132e1b362 --- /dev/null +++ b/TypeSystem/flow-analysis/promotion_via_assignment_A05_t01.dart @@ -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 We say that a variable `x` is promotable via assignment of an +/// expression of type `T` given variable model `VM` if +/// - `VM = VariableModel(declared, promoted, tested, assigned, unassigned, +/// captured)` +/// - and captured is false +/// - and `S` is the current type of `x` in `VM` +/// - and `T <: S` and not `S <: T` +/// - and `T` is a type of interest for `x` in `tested` +/// +/// @description Checks that if the type of a variable is not specified and +/// there is no initializer or is `dynamic` then it can be promoted via +/// assignment. +/// @author sgrekhov22@gmail.com + +class S {} + +class T extends S { + int foo() => 42; +} + +test1() { + var x; + x = S(); + if (x is T) {} // Make `T` a type of interest + x = T(); + x.foo(); + x.bar(); // x is not `dynamic` +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +test2() { + dynamic x; + x = S(); + if (x is T) {} + x = T(); + x.foo(); + x.bar(); +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified +} + +main() { + print(test1); + print(test2); +} diff --git a/TypeSystem/flow-analysis/promotion_via_assignment_A05_t02.dart b/TypeSystem/flow-analysis/promotion_via_assignment_A05_t02.dart new file mode 100644 index 0000000000..54834e39c7 --- /dev/null +++ b/TypeSystem/flow-analysis/promotion_via_assignment_A05_t02.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 We say that a variable `x` is promotable via assignment of an +/// expression of type `T` given variable model `VM` if +/// - `VM = VariableModel(declared, promoted, tested, assigned, unassigned, +/// captured)` +/// - and captured is false +/// - and `S` is the current type of `x` in `VM` +/// - and `T <: S` and not `S <: T` +/// - and `T` is a type of interest for `x` in `tested` +/// +/// @description Checks that if the type of a variable is not specified and +/// there is an initializer then its type is inferred from the initializing +/// expression. +/// @author sgrekhov22@gmail.com + +class S {} + +class T extends S { + int foo() => 42; +} + +main() { + var x = S(); + if (x is T) {} // Make `T` a type of interest + x = T(); + x.foo(); + x.bar(); // Check that `x` is not `dynamic` +// ^^^ +// [analyzer] unspecified +// [cfe] unspecified +} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A01_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A01_t01.dart deleted file mode 100644 index 418e0f25fc..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A01_t01.dart +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that a variable is promotable to the type T if all -/// requirements above are met. -/// @author sgrekhov@unipro.ru -/// @issue 41669 - -import "../../Utils/expect.dart"; - -class T { - int foo() => 42; -} - -main() { - var x; - x = new T(); - x.foo(); - Expect.throws(() {x.bar();}, (e) => e is NoSuchMethodError); // Check that x is dynamic -} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A02_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A02_t01.dart deleted file mode 100644 index 6ad89b6147..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A02_t01.dart +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that if captured is true then promotion via -/// initialization does not happen. -/// @author sgrekhov@unipro.ru - -import '../../Utils/expect.dart'; - -class S {} -class T extends S { - int foo() => 42; -} - -Function? f; - -main() { - var x; - bar() { - x = new S(); - } - f = bar; - x = new T(); - Expect.throws(() { - x.g(); - }, (e) => e is NoSuchMethodError); -} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A03_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A03_t01.dart deleted file mode 100644 index 3e5e0e94f6..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A03_t01.dart +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that if promoted is not empty then promotion via -/// initialization does not happen. -/// @author sgrekhov@unipro.ru - -import '../../Utils/expect.dart'; - -class S {} -class T extends S { - int foo() => 42; -} - -main() { - var x; - if (x is S) { - x = new T(); - Expect.throws(() { - x.g(); - }, (e) => e is NoSuchMethodError); - } -} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A04_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A04_t01.dart deleted file mode 100644 index fc1a642517..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A04_t01.dart +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that if x is declared with an explicit type then -/// promotion via initialization does not happen. -/// @author sgrekhov@unipro.ru - -class S {} -class T extends S { - int foo() => 42; -} - -main() { - S x; - x = new T(); - x.foo(); -// ^^^ -// [analyzer] unspecified -// [cfe] unspecified -} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A05_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A05_t01.dart deleted file mode 100644 index 182196f1a9..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A05_t01.dart +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that if x is declared with no explicit type but with an -/// initializer then promotion via initialization does not happen. -/// @author sgrekhov@unipro.ru - -class S {} -class T extends S { - int foo() => 42; -} - -main() { - var x = new S(); - x = new T(); - x.foo(); -// ^^^ -// [analyzer] unspecified -// [cfe] unspecified -} diff --git a/TypeSystem/flow-analysis/promotion_via_initialization_A06_t01.dart b/TypeSystem/flow-analysis/promotion_via_initialization_A06_t01.dart deleted file mode 100644 index 9f2e3c250f..0000000000 --- a/TypeSystem/flow-analysis/promotion_via_initialization_A06_t01.dart +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2020, 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 We say that a variable x is promotable via initialization given -/// variable model VM if x is a local variable (not a formal parameter) and: -/// -/// VM = VariableModel(declared, promoted, tested, assigned, unassigned, -/// captured) -/// and captured is false -/// and promoted is empty -/// and x is declared with no explicit type and no initializer -/// and assigned is false and unassigned is true -/// -/// @description Checks that if assigned is true and unassigned is false then -/// promotion via initialization does not happen. -/// @author sgrekhov@unipro.ru -/// @issue 41669 - -import "../../Utils/expect.dart"; - -class S {} -class T extends S { - int foo() => 42; -} - -main() { - var x; - x = new S(); - x = new T(); - Expect.equals(42, x.foo()); -}