Skip to content

Commit d230f5c

Browse files
authored
Fixes #1361. Private fields promotion tests added (#1391)
Authored by @sgrekhov.
1 parent b477f13 commit d230f5c

27 files changed

+1053
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable if all of the
12+
/// conditions above are met
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class C {
18+
final int? _x;
19+
final int? _y = 42;
20+
C(this._x);
21+
22+
void test() {
23+
if (_x != null) {
24+
_x.isOdd;
25+
}
26+
if (_y != null) {
27+
_y.isOdd;
28+
}
29+
}
30+
}
31+
32+
main() {
33+
C(42).test();
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable if all of the
12+
/// conditions above are met. Test type variable
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class C<T> {
18+
final T _x;
19+
C(this._x);
20+
21+
void test() {
22+
if (_x is int) {
23+
_x.isOdd;
24+
}
25+
}
26+
}
27+
28+
main() {
29+
C(42).test();
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable if all of the
12+
/// conditions above are met. Test type variable
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class A {}
18+
class B extends A {
19+
void foo() {}
20+
}
21+
22+
class C<T extends A> {
23+
final T _x;
24+
C(this._x);
25+
26+
void test() {
27+
if (_x is B) {
28+
_x.foo();
29+
}
30+
}
31+
}
32+
33+
main() {
34+
C(B()).test();
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable if all of the
12+
/// conditions above are met. Test the case when there are other concrete
13+
/// instance getters with the same name in the same library and they are also
14+
/// final fields
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=inference-update-2
18+
19+
class A {
20+
final int? _x;
21+
A(this._x);
22+
23+
void testA() {
24+
if (_x != null) {
25+
_x.isOdd;
26+
}
27+
}
28+
}
29+
30+
class B {
31+
final String? _x;
32+
B(this._x);
33+
34+
void testB() {
35+
if (_x != null) {
36+
_x.substring(0);
37+
}
38+
}
39+
}
40+
41+
class C extends A {
42+
final int? _x;
43+
C(this._x) : super(_x);
44+
45+
void testC() {
46+
if (_x != null) {
47+
_x.isOdd;
48+
}
49+
if (super._x != null) {
50+
super._x.isOdd;
51+
}
52+
}
53+
}
54+
55+
main() {
56+
A(42).testA();
57+
B("X").testB();
58+
C(1).testC();
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable if all of the
12+
/// conditions above are met. Test that the right variable is promoted
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class A {
18+
final int? _x;
19+
A(this._x);
20+
}
21+
22+
class C extends A {
23+
final int? _x;
24+
C(this._x) : super(_x);
25+
26+
void testC() {
27+
if (_x != null) {
28+
super._x.isOdd;
29+
// ^^^^^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
}
33+
if (super._x != null) {
34+
_x.isOdd;
35+
// ^^^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
}
40+
}
41+
42+
mixin M on A {
43+
final int? _x = 0;
44+
45+
void testM() {
46+
if (_x != null) {
47+
super._x.isOdd;
48+
// ^^^^^
49+
// [analyzer] unspecified
50+
// [cfe] unspecified
51+
}
52+
if (super._x != null) {
53+
_x.isOdd;
54+
// ^^^^^
55+
// [analyzer] unspecified
56+
// [cfe] unspecified
57+
}
58+
}
59+
}
60+
61+
main() {
62+
C(42).testC();
63+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable even if it is late
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=inference-update-2
15+
16+
class C {
17+
late final int? _x;
18+
19+
void test() {
20+
_x = 42;
21+
_x.isOdd;
22+
}
23+
}
24+
25+
main() {
26+
C().test();
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is promotable even if it is late
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=inference-update-2
15+
16+
class A {
17+
void foo() {}
18+
}
19+
20+
class C<T extends A> {
21+
late final T? _x;
22+
23+
void test(T t) {
24+
_x = t;
25+
_x.foo();
26+
}
27+
}
28+
29+
main() {
30+
C().test(A());
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is not promotable if it is not
12+
/// private
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class C {
18+
final int? x;
19+
final int? y = 42;
20+
C(this.x);
21+
22+
void test() {
23+
if (x != null) {
24+
x.isOdd;
25+
// ^^^^^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
29+
if (y != null) {
30+
y.isOdd;
31+
// ^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
}
36+
}
37+
38+
main() {
39+
C(42).test();
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022, 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 An instance field is promotable only if
6+
/// (a) it is private,
7+
/// (b) it is final, and
8+
/// (c) all other concrete instance getters with the same name in the same
9+
/// library are also final fields
10+
///
11+
/// @description Checks that an instance field is not promotable if it is not
12+
/// private. Test type variable
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=inference-update-2
16+
17+
class C<T> {
18+
final T x;
19+
C(this.x);
20+
21+
void test() {
22+
if (x is int) {
23+
x.isOdd;
24+
// ^^^^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}
28+
}
29+
}
30+
31+
main() {
32+
C(42).test();
33+
}

0 commit comments

Comments
 (0)