forked from dart-lang/co19
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dart-lang#2145. Add more local variable declaration tests
- Loading branch information
Showing
11 changed files
with
408 additions
and
9 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Language/Statements/Local_Variable_Declaration/declared_type_A01_t01.dart
This file contains 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) 2023, 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 The declared type of a local variable with a declaration of one | ||
/// of the forms | ||
/// late? T v = e; late? final T v = e; const T v = e; is T | ||
/// | ||
/// @description Checks that the declared type of a local variable with the | ||
/// specified type `T` is `T` | ||
/// @author [email protected] | ||
import '../../../Utils/static_type_helper.dart'; | ||
|
||
main() { | ||
late num v1 = 1; | ||
late final num v2 = 2; | ||
num v3 = 3; | ||
final num? v4 = 4; | ||
const num v5 = 5; | ||
|
||
v1.expectStaticType<Exactly<num>>(); | ||
v2.expectStaticType<Exactly<num>>(); | ||
v3.expectStaticType<Exactly<num>>(); | ||
v4.expectStaticType<Exactly<num?>>(); | ||
v5.expectStaticType<Exactly<num>>(); | ||
} |
This file contains 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
This file contains 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
65 changes: 65 additions & 0 deletions
65
Language/Statements/Local_Variable_Declaration/declared_type_A03_t01.dart
This file contains 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,65 @@ | ||
// Copyright (c) 2023, 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 The declared type of a local variable with a declaration of one | ||
/// of the forms late? var v = e; late? final v = e; const v = e; is determined | ||
/// as follows: | ||
/// ... | ||
/// • If the static type of e is of the form X & T where X is a type variable, | ||
/// the declared type of v is X. In this case v is immediately promoted to X & T | ||
/// | ||
/// @description Checks that static type of a variable declared by the | ||
/// statements `late? var v = e; late? final v = e;` is `X` if the static type | ||
/// of `e` is `X & T` where `X` is a type variable. Also test that `v` is | ||
/// promoted to `X & T` | ||
/// @author [email protected] | ||
import '../../../Utils/static_type_helper.dart'; | ||
|
||
test1<T>(T t) { | ||
if (t is int) { | ||
var v = t; | ||
v.isEven; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
var v = t; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
|
||
test2<T>(T t) { | ||
if (t is int) { | ||
late var v = t; | ||
v.isEven; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
late var v = t; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
|
||
test3<T>(T t) { | ||
if (t is int) { | ||
final v = t; | ||
v.isEven; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
final v = t; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
|
||
test4<T>(T t) { | ||
if (t is int) { | ||
late final v = t; | ||
v.isEven; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
late final v = t; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
|
||
main() { | ||
test1<int>(1); | ||
test2<num>(2); | ||
test3<int>(3); | ||
test4<num>(4); | ||
} |
43 changes: 43 additions & 0 deletions
43
Language/Statements/Local_Variable_Declaration/declared_type_A04_t01.dart
This file contains 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) 2023, 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 The declared type of a local variable with a declaration of one | ||
/// of the forms late? var v = e; late? final v = e; const v = e; is determined | ||
/// as follows: | ||
/// ... | ||
/// • Otherwise, the declared type of v is the static type of e. | ||
/// | ||
/// @description Checks that static type of a variable declared by the | ||
/// statements `late? var v = e; late? final v = e; const v = e;` is the static | ||
/// type of `e` | ||
/// @author [email protected] | ||
import '../../../Utils/static_type_helper.dart'; | ||
|
||
test4<T>(T t) { | ||
if (t is int) { | ||
late final v = t; | ||
v.isEven; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
late final v = t; | ||
v.expectStaticType<Exactly<T>>(); | ||
} | ||
|
||
main() { | ||
late var v1 = "1"; | ||
v1.expectStaticType<Exactly<String>>(); | ||
|
||
var v2 = 2; | ||
v2.expectStaticType<Exactly<int>>(); | ||
|
||
late final v3 = 3 as num; | ||
v3.expectStaticType<Exactly<num>>(); | ||
|
||
final v4 = 4 as int?; | ||
v4.expectStaticType<Exactly<int?>>(); | ||
|
||
const v5 = 5 as num?; | ||
v5.expectStaticType<Exactly<num?>>(); | ||
} |
41 changes: 41 additions & 0 deletions
41
Language/Statements/Local_Variable_Declaration/late_await_A01_t01.dart
This file contains 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) 2023, 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 Assume that D is a local variable declaration with the modifier | ||
/// late that declares a variable v, which has an initializing expression e. It | ||
/// is a compile-time error if e contains an await expression a, unless there is | ||
/// a function f which is the immediately enclosing function for a, and f is not | ||
/// the immediately enclosing function for D. | ||
/// | ||
/// @description Checks that it is a compile-time error if initializing | ||
/// expression of a late local variable contains an `await` expression | ||
/// @author [email protected] | ||
import 'dart:async'; | ||
|
||
Future<T> foo<T>(T t) => Future<T>.value(t); | ||
|
||
main() async { | ||
late var v1 = await Future<int>.value(1); | ||
// ^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
late final v2 = await foo<int>(2); | ||
// ^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
late String v3 = await foo<String>("3"); | ||
// ^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
late final int v4 = await Future<int>.value(4); | ||
// ^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
return 0; | ||
} |
33 changes: 33 additions & 0 deletions
33
Language/Statements/Local_Variable_Declaration/late_await_A01_t02.dart
This file contains 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,33 @@ | ||
// Copyright (c) 2023, 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 Assume that D is a local variable declaration with the modifier | ||
/// late that declares a variable v, which has an initializing expression e. It | ||
/// is a compile-time error if e contains an await expression a, unless there is | ||
/// a function f which is the immediately enclosing function for a, and f is not | ||
/// the immediately enclosing function for D. | ||
/// | ||
/// @description Checks that it is not an error if there is a function `f` which | ||
/// is the immediately enclosing function for `a`, and `f` is not the | ||
/// immediately enclosing function for `D` | ||
/// @author [email protected] | ||
import 'dart:async'; | ||
import '../../../Utils/static_type_helper.dart'; | ||
|
||
Future<T> foo<T>(T t) => Future<T>.value(t); | ||
|
||
main() { | ||
late var v1 = () async => await Future<int>.value(1); | ||
v1.expectStaticType<Exactly<Future<int> Function()>>(); | ||
|
||
late final v2 = () async {return await foo<int>(2);}; | ||
v2.expectStaticType<Exactly<Future<int> Function()>>(); | ||
|
||
late Future<String> v3 = () async {return await foo<String>("3");}(); | ||
print(v3); | ||
|
||
late final Future<int> Function() v4 = () async => await Future<int>.value(4); | ||
print(v4); | ||
} |
31 changes: 31 additions & 0 deletions
31
Language/Statements/Local_Variable_Declaration/late_final_A01_t01.dart
This file contains 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) 2023, 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 Let D be a late and final local variable declaration that | ||
/// declares a variable v. If an object o is assigned to v in a situation where | ||
/// v is unbound then v is bound to o. If an object o is assigned to v in a | ||
/// situation where v is bound to an object o′ then a dynamic error occurs (it | ||
/// does not matter whether o is the same object as o′). | ||
/// | ||
/// @description Checks that if an object `o` is assigned to a late final local | ||
/// variable `v` in a situation where `v` is unbound then `v` is bound to `o` | ||
/// @author [email protected] | ||
import '../../../Utils/expect.dart'; | ||
|
||
class C { | ||
int id; | ||
C(this.id); | ||
} | ||
|
||
main() { | ||
C c1 = C(1); | ||
late final v1 = c1; | ||
Expect.identical(c1, v1); | ||
|
||
C c2 = C(2); | ||
late final v2; | ||
v2 = c2; | ||
Expect.identical(c2, v2); | ||
} |
35 changes: 35 additions & 0 deletions
35
Language/Statements/Local_Variable_Declaration/late_final_A01_t02.dart
This file contains 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,35 @@ | ||
// Copyright (c) 2023, 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 Let D be a late and final local variable declaration that | ||
/// declares a variable v. If an object o is assigned to v in a situation where | ||
/// v is unbound then v is bound to o. If an object o is assigned to v in a | ||
/// situation where v is bound to an object o′ then a dynamic error occurs (it | ||
/// does not matter whether o is the same object as o′). | ||
/// | ||
/// @description Checks that it is a dynamic error if an object `o` is assigned | ||
/// to a late final local variable `v` in a situation where `v` is bound to some | ||
/// object | ||
/// @author [email protected] | ||
import '../../../Utils/expect.dart'; | ||
|
||
class C { | ||
int id; | ||
C(this.id); | ||
} | ||
|
||
main() { | ||
C c = C(1); | ||
late final v; | ||
if (2 > 1) { | ||
v = c; | ||
} | ||
Expect.throws(() { | ||
v = C(42); | ||
}); | ||
Expect.throws(() { | ||
v = c; | ||
}); | ||
} |
Oops, something went wrong.