Skip to content

Commit 7bf88af

Browse files
authored
#2145. Add more local variable declaration tests (#2254)
Rename Local Variable declaration tests. Delete duplicates.
1 parent 90598f1 commit 7bf88af

13 files changed

+495
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion The declared type of a local variable with a declaration of one
6+
/// of the forms
7+
/// late? T v = e; late? final T v = e; const T v = e; is T
8+
///
9+
/// @description Checks that the declared type of a local variable with the
10+
/// specified type `T` is `T`
11+
/// @author sgrekhov22@gmail.com
12+
13+
import '../../../Utils/static_type_helper.dart';
14+
15+
main() {
16+
// We use `as dynamic` below to avoid a type promotion on an assignment
17+
late num v1 = 1 as dynamic;
18+
late final num v2 = 2 as dynamic;
19+
num v3 = 3 as dynamic;
20+
final num? v4 = 4 as dynamic;
21+
const num v5 = 5 as dynamic;
22+
23+
v1.expectStaticType<Exactly<num>>();
24+
v2.expectStaticType<Exactly<num>>();
25+
v3.expectStaticType<Exactly<num>>();
26+
v4.expectStaticType<Exactly<num?>>();
27+
v5.expectStaticType<Exactly<num>>();
28+
}

Language/Statements/Local_Variable_Declaration/declared_type_A02_t01.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,40 @@
77
/// as follows:
88
/// • If the static type of e is Null then the declared type of v is dynamic.
99
///
10-
/// @description Checks that a variable declaration statement `var id;`
11-
/// introduces a new variable `id` with `dynamic` static type into the innermost
12-
/// enclosing scope.
10+
/// @description Checks that the variable declaration statements
11+
/// `late? var v = e; late? final v = e; const v = e;` introduce a new variable
12+
/// `v` with `dynamic` static type into the innermost enclosing scope.
1313
/// @author vasya
1414
1515
import '../../../Utils/expect.dart';
1616

1717
class C {}
1818

19-
main() {
19+
test1() {
2020
var id;
2121
Expect.throws(() {
2222
id.whatever;
2323
});
2424
id = false;
2525
id = "";
2626
id = 2;
27-
id = new C();
27+
id = C();
2828
id = () {};
2929
}
30+
31+
test2() {
32+
late var id;
33+
Expect.throws(() {
34+
id.whatever;
35+
});
36+
id = false;
37+
id = "";
38+
id = 2;
39+
id = C();
40+
id = () {};
41+
}
42+
43+
main() {
44+
test1();
45+
test2();
46+
}

Language/Statements/Local_Variable_Declaration/declared_type_A02_t02.dart

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
/// as follows:
88
/// • If the static type of e is Null then the declared type of v is dynamic.
99
///
10-
/// @description Checks that a variable declaration statement `var id = null;`
11-
/// introduces a new variable `id` with `dynamic` static type into the innermost
12-
/// enclosing scope.
10+
/// @description Checks that the variable declaration statements
11+
/// `late? var v = null; late? final v = null; const v = null;` introduce a new
12+
/// variable `v` with `dynamic` static type into the innermost enclosing scope.
1313
/// @author vasya
1414
1515
import '../../../Utils/expect.dart';
1616

1717
class C {}
1818

19-
main() {
19+
test1() {
2020
var id = null;
2121
Expect.throws(() {
2222
id.whatever;
@@ -27,3 +27,34 @@ main() {
2727
id = new C();
2828
id = () {};
2929
}
30+
31+
test2() {
32+
late var id = null;
33+
Expect.throws(() {
34+
id.whatever;
35+
});
36+
id = false;
37+
id = "";
38+
id = 2;
39+
id = new C();
40+
id = () {};
41+
}
42+
43+
main() {
44+
late final v1 = null;
45+
final v2 = null;
46+
const v3 = null;
47+
48+
Expect.throws(() {
49+
v1.whatever;
50+
});
51+
Expect.throws(() {
52+
v2.whatever;
53+
});
54+
Expect.throws(() {
55+
v3.whatever;
56+
});
57+
58+
test1();
59+
test2();
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion The declared type of a local variable with a declaration of one
6+
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
7+
/// as follows:
8+
/// ...
9+
/// • If the static type of e is of the form X & T where X is a type variable,
10+
/// the declared type of v is X. In this case v is immediately promoted to X & T
11+
///
12+
/// @description Checks that the static type of a variable declared by the
13+
/// statements `late? var v = e; late? final v = e;` is `X` if the static type
14+
/// of `e` is `X & T` where `X` is a type variable. Also test that `v` is
15+
/// promoted to `X & T`
16+
/// @author sgrekhov22@gmail.com
17+
18+
import '../../../Utils/static_type_helper.dart';
19+
20+
test1<T>(T t) {
21+
if (t is int) {
22+
var v = t;
23+
v.isEven;
24+
T x = v;
25+
v = x;
26+
}
27+
}
28+
29+
test2<T>(T t) {
30+
if (t is int) {
31+
late var v = t;
32+
v.isEven;
33+
T x = v;
34+
v = x;
35+
}
36+
}
37+
38+
test3<T>(T t) {
39+
if (t is int) {
40+
final v = t;
41+
v.isEven;
42+
T x = v;
43+
}
44+
}
45+
46+
test4<T>(T t) {
47+
if (t is int) {
48+
late final v = t;
49+
v.isEven;
50+
T x = v;
51+
}
52+
}
53+
54+
main() {
55+
test1<int>(1);
56+
test2<num>(2);
57+
test3<int>(3);
58+
test4<num>(4);
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion The declared type of a local variable with a declaration of one
6+
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
7+
/// as follows:
8+
/// ...
9+
/// • If the static type of e is of the form X & T where X is a type variable,
10+
/// the declared type of v is X. In this case v is immediately promoted to X & T
11+
///
12+
/// @description Checks that the static type of a variable declared by the
13+
/// statements `late? var v = e; late? final v = e;` is `X` if the static type
14+
/// of `e` is where `X` is a type variable. Also check that `v` is not
15+
/// erroneously promoted
16+
/// @author sgrekhov22@gmail.com
17+
18+
test1<T>(T t) {
19+
if (t is int) {
20+
}
21+
var v = t;
22+
v.isEven;
23+
// ^^^^^^
24+
// [analyzer] unspecified
25+
// [cfe] unspecified
26+
}
27+
28+
test2<T>(T t) {
29+
if (t is int) {
30+
}
31+
late var v = t;
32+
v.isEven;
33+
// ^^^^^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
}
37+
38+
test3<T>(T t) {
39+
if (t is int) {
40+
}
41+
final v = t;
42+
v.isEven;
43+
// ^^^^^^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
}
47+
48+
test4<T>(T t) {
49+
if (t is int) {
50+
}
51+
late final v = t;
52+
v.isEven;
53+
// ^^^^^^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
}
57+
58+
main() {
59+
test1<int>(1);
60+
test2<num>(2);
61+
test3<int>(3);
62+
test4<num>(4);
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion The declared type of a local variable with a declaration of one
6+
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
7+
/// as follows:
8+
/// ...
9+
/// • Otherwise, the declared type of v is the static type of e.
10+
///
11+
/// @description Checks that static type of a variable declared by the
12+
/// statements `late? var v = e; late? final v = e; const v = e;` is the static
13+
/// type of `e`
14+
/// @author sgrekhov22@gmail.com
15+
16+
import '../../../Utils/static_type_helper.dart';
17+
18+
test<T>(T t) {
19+
if (t is int) {
20+
late final v = t;
21+
v.isEven;
22+
T x = v;
23+
}
24+
}
25+
26+
main() {
27+
late var v1 = "1";
28+
v1.expectStaticType<Exactly<String>>();
29+
30+
var v2 = 2;
31+
v2.expectStaticType<Exactly<int>>();
32+
33+
late final v3 = 3 as num;
34+
v3.expectStaticType<Exactly<num>>();
35+
36+
final v4 = 4 as int?;
37+
v4.expectStaticType<Exactly<int?>>();
38+
39+
const v5 = 5 as num?;
40+
v5.expectStaticType<Exactly<num?>>();
41+
42+
test<int>(42);
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion The declared type of a local variable with a declaration of one
6+
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
7+
/// as follows:
8+
/// ...
9+
/// • Otherwise, the declared type of v is the static type of e.
10+
///
11+
/// @description Checks that the static type of a variable declared by the
12+
/// statements `late? var v = e; late? final v = e; const v = e;` is the static
13+
/// type of `e`. Check that `v` is not erroneously promoted
14+
/// @author sgrekhov22@gmail.com
15+
16+
import '../../../Utils/static_type_helper.dart';
17+
18+
test<T>(T t) {
19+
if (t is int) {
20+
}
21+
late final v = t;
22+
v.isEven;
23+
// ^^^^^^
24+
// [analyzer] unspecified
25+
// [cfe] unspecified
26+
}
27+
28+
main() {
29+
test<num>(3.14);
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Assume that D is a local variable declaration with the modifier
6+
/// late that declares a variable v, which has an initializing expression e. It
7+
/// is a compile-time error if e contains an await expression a, unless there is
8+
/// a function f which is the immediately enclosing function for a, and f is not
9+
/// the immediately enclosing function for D.
10+
///
11+
/// @description Checks that it is a compile-time error if initializing
12+
/// expression of a late local variable contains an `await` expression
13+
/// @author sgrekhov22@gmail.com
14+
15+
import 'dart:async';
16+
17+
Future<T> foo<T>(T t) => Future<T>.value(t);
18+
19+
main() async {
20+
late var v1 = await Future<int>.value(1);
21+
// ^^^^^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
25+
late final v2 = await foo<int>(2);
26+
// ^^^^^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
30+
late String v3 = await foo<String>("3");
31+
// ^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
35+
late final int v4 = await Future<int>.value(4);
36+
// ^^^^^
37+
// [analyzer] unspecified
38+
// [cfe] unspecified
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)