Skip to content
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
17 changes: 8 additions & 9 deletions TypeSystem/flow-analysis/promotion_via_assignment_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// 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
/// @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 a variable is promotable to the type `T` if all
/// requirements above are met.
Expand Down
20 changes: 8 additions & 12 deletions TypeSystem/flow-analysis/promotion_via_assignment_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// 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
/// @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 `captured` is `true` then promotion via
/// assignment is not performed
Expand All @@ -21,14 +20,11 @@ class T extends S {
int foo() => 42;
}

Function? f;

main() {
S x = new S();
void bar(var v) {
x = v;
}
f = bar;
if (x is T) {} // make `T` a type of interest
x = new T();
x.foo();
Expand Down
49 changes: 28 additions & 21 deletions TypeSystem/flow-analysis/promotion_via_assignment_A03_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@
// 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
/// @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 `T <: S` and `S <: T` then promotion via
/// assignment is not performed
/// assignment is not performed.
/// @author sgrekhov@unipro.ru

import '../../Utils/expect.dart';
import 'dart:async';
import '../../Utils/static_type_helper.dart';

test1(FutureOr<Object> v) {
if (v is Object) {} // ignore: unnecessary_type_check
v = Object();
// `Object` <: `FutureOr<Object>` and `FutureOr<Object>` <: `Object`.
// Therefore `v` is not promoted to `Object`.
v.expectStaticType<Exactly<FutureOr<Object>>>();
}

class S {}
class T extends S {
int foo() => 42;
test2(FutureOr<Object?> v) {
if (v is Object?) {} // ignore: unnecessary_type_check
v = 1 > 2 ? null: Object();
// `Object?` <: `FutureOr<Object?>` and `FutureOr<Object?>` <: `Object?`.
// Therefore `v` is not promoted to `Object?`.
v.expectStaticType<Exactly<FutureOr<Object?>>>();
}

main() {
dynamic x = new S();
if (x is T) {} // make `T` a type of interest
x = new T();
x.foo();
Expect.throws(() {
x.bar();
}, (e) => e is NoSuchMethodError);
test1(Object());
test2(Object());
test2(null);
}
32 changes: 32 additions & 0 deletions TypeSystem/flow-analysis/promotion_via_assignment_A03_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 `T` is not subtype of `S` then promotion via
/// assignment cannot be performed.
/// @author sgrekhov22@gmail.com
/// @issue 60646

main() {
Object? s = "Some string";
s as String; // `s` promoted to `String`
if (s is int) {} // Make `int` a type of interest for `s`.
s = 42; // `s` demoted to `Object`
s.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
s.substring(0);
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
17 changes: 8 additions & 9 deletions TypeSystem/flow-analysis/promotion_via_assignment_A04_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// 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
/// @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 `T` is not a type of interest for `x` in
/// `tested` then promotion via assignment is not performed.
Expand Down