Skip to content

Fixes #1361. Private fields promotion tests added #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2022
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
34 changes: 34 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable if all of the
/// conditions above are met
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class C {
final int? _x;
final int? _y = 42;
C(this._x);

void test() {
if (_x != null) {
_x.isOdd;
}
if (_y != null) {
_y.isOdd;
}
}
}

main() {
C(42).test();
}
30 changes: 30 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable if all of the
/// conditions above are met. Test type variable
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class C<T> {
final T _x;
C(this._x);

void test() {
if (_x is int) {
_x.isOdd;
}
}
}

main() {
C(42).test();
}
35 changes: 35 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable if all of the
/// conditions above are met. Test type variable
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class A {}
class B extends A {
void foo() {}
}

class C<T extends A> {
final T _x;
C(this._x);

void test() {
if (_x is B) {
_x.foo();
}
}
}

main() {
C(B()).test();
}
59 changes: 59 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable if all of the
/// conditions above are met. Test the case when there are other concrete
/// instance getters with the same name in the same library and they are also
/// final fields
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class A {
final int? _x;
A(this._x);

void testA() {
if (_x != null) {
_x.isOdd;
}
}
}

class B {
final String? _x;
B(this._x);

void testB() {
if (_x != null) {
_x.substring(0);
}
}
}

class C extends A {
final int? _x;
C(this._x) : super(_x);

void testC() {
if (_x != null) {
_x.isOdd;
}
if (super._x != null) {
super._x.isOdd;
}
}
}

main() {
A(42).testA();
B("X").testB();
C(1).testC();
}
63 changes: 63 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable if all of the
/// conditions above are met. Test that the right variable is promoted
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class A {
final int? _x;
A(this._x);
}

class C extends A {
final int? _x;
C(this._x) : super(_x);

void testC() {
if (_x != null) {
super._x.isOdd;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, nice corner case! My current prototype code doesn't handle it properly. I'll make sure to fix it :)

// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
if (super._x != null) {
_x.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
}

mixin M on A {
final int? _x = 0;

void testM() {
if (_x != null) {
super._x.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
if (super._x != null) {
_x.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
}

main() {
C(42).testC();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

27 changes: 27 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable even if it is late
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class C {
late final int? _x;

void test() {
_x = 42;
_x.isOdd;
}
}

main() {
C().test();
}
31 changes: 31 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is promotable even if it is late
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class A {
void foo() {}
}

class C<T extends A> {
late final T? _x;

void test(T t) {
_x = t;
_x.foo();
}
}

main() {
C().test(A());
}
40 changes: 40 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is not promotable if it is not
/// private
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class C {
final int? x;
final int? y = 42;
C(this.x);

void test() {
if (x != null) {
x.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
if (y != null) {
y.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
}

main() {
C(42).test();
}
33 changes: 33 additions & 0 deletions LanguageFeatures/Private-fields-promotion/promotion_A03_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022, 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 An instance field is promotable only if
/// (a) it is private,
/// (b) it is final, and
/// (c) all other concrete instance getters with the same name in the same
/// library are also final fields
///
/// @description Checks that an instance field is not promotable if it is not
/// private. Test type variable
/// @author [email protected]

// SharedOptions=--enable-experiment=inference-update-2

class C<T> {
final T x;
C(this.x);

void test() {
if (x is int) {
x.isOdd;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
}

main() {
C(42).test();
}
Loading