Skip to content

Commit

Permalink
#2559. Add augmenting expression tests for fields with no initializers (
Browse files Browse the repository at this point in the history
#2626)

Add augmenting expression tests for fields with no initializers
  • Loading branch information
sgrekhov committed Apr 30, 2024
1 parent f258594 commit 6066581
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that if an `augmented` is used in an augmenting field's
/// initializer and the member being augmented is a nullable field with no
/// initializer then `augmented` evaluates to `null`.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

import augment 'augmented_expression_A03_t07_lib.dart';
import '../../Utils/expect.dart';

final String augmented = "Augmented variable, should not be used";

String? topLevelVariable;

class C {
static String? staticVariable;
String? instanceVariable;
}

mixin M {
static String? staticVariable;
String? instanceVariable;
}

enum E {
e1;
static String? staticVariable;
}

class A {}

extension Ext on A {
static String? staticVariable;
}

class MA = Object with M;

main() {
Expect.equals("Augment: null", topLevelVariable);
Expect.equals("Augment: null", C.staticVariable);
Expect.equals("Augment: null", C().instanceVariable);
Expect.equals("Augment: null", M.staticVariable);
Expect.equals("Augment: null", MA().instanceVariable);
Expect.equals("Augment: null", E.staticVariable);
Expect.equals("Augment: null", Ext.staticVariable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that if an `augmented` is used in an augmenting field's
/// initializer and the member being augmented is a nullable field with no
/// initializer then `augmented` evaluates to `null`.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

augment library 'augmented_expression_A03_t07.dart';

augment String? topLevelVariable = "Augment: $augmented";

augment class C {
augment static String? staticVariable = "Augment: $augmented";
augment String? instanceVariable = "Augment: $augmented";
}

augment mixin M {
augment static String? staticVariable = "Augment: $augmented";
augment String? instanceVariable = "Augment: $augmented";
}

augment enum E {
augment e1;

augment static String? staticVariable = "Augment: $augmented";
}

augment extension Ext {
augment static String? staticVariable = "Augment: $augmented";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that if an `augmented` is used in an augmenting field's
/// initializer and the member being augmented is a nullable field with no
/// initializer then `augmented` evaluates to `null`.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

import augment 'augmented_expression_A03_t08_lib.dart';
import '../../Utils/expect.dart';

final String augmented = "Augmented variable, should not be used";

Function? topLevelVariable;

class C {
static Function? staticVariable;
Function? instanceVariable;
}

mixin M {
static Function? staticVariable;
Function? instanceVariable;
}

enum E {
e1;
static Function? staticVariable;
}

class A {}

extension Ext on A {
static Function? staticVariable;
}

class MA = Object with M;

main() {
Expect.equals("Augment: null", topLevelVariable());
Expect.equals("Augment: null", C.staticVariable());
Expect.equals("Augment: null", C().instanceVariable());
Expect.equals("Augment: null", M.staticVariable());
Expect.equals("Augment: null", MA().instanceVariable());
Expect.equals("Augment: null", E.staticVariable());
Expect.equals("Augment: null", Ext.staticVariable());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that if an `augmented` is used in an augmenting field's
/// initializer and the member being augmented is a nullable field with no
/// initializer then `augmented` evaluates to `null`.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

augment library 'augmented_expression_A03_t08.dart';

augment Function? topLevelVariable = () {
return "Augment: $augmented";
};

augment class C {
augment static Function? staticVariable = () {
return "Augment: $augmented";
};
augment Function? instanceVariable = () {
return "Augment: $augmented";
};
}

augment mixin M {
augment static Function? staticVariable = () {
return "Augment: $augmented";
};
augment Function? instanceVariable = () {
return "Augment: $augmented";
};
}

augment enum E {
augment e1;
augment static Function? staticVariable = () {
return "Augment: $augmented";
};
}

augment extension Ext {
augment static Function? staticVariable = () {
return "Augment: $augmented";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that it is a compile-time error to use `augmented` in an
/// augmenting field's initializer if the member being augmented is not a field
/// with an initializer and the variable's type is not-nullable
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

import augment 'augmented_expression_A03_t10_lib.dart';

final String augmented = "Augmented variable, should not be used";

class C {
String instanceVariable;
final String finalInstanceVariable;
C(this.instanceVariable, this.finalInstanceVariable);
}

enum E {
e1("x");
final String finalInstanceVariable;
const E(this.finalInstanceVariable);
}

main() {
print(C);
print(E);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that it is a compile-time error to use `augmented` in an
/// augmenting field's initializer if the member being augmented is not a field
/// with an initializer and the variable's type is not-nullable
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

augment library 'augmented_expression_A03_t10.dart';

augment class C {
augment String instanceVariable = "Augment: $augmented";
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
augment final String finalInstanceVariable = "Augment: $augmented";
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

augment enum E {
augment e1("x");

augment final String finalInstanceVariable = "Augment: $augmented";
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024, 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 exact result of an `augmented` expression depends on what is
/// being augmented, but it generally follows the same rules as any normal
/// identifier:
/// ...
/// - Augmenting fields: Within an augmenting field, augmented can only be used
/// in an initializer expression, and refers to the original field's
/// initializer expression, which is immediately evaluated.
///
/// If augmented refers to a variable declaration (as defined by a declaration
/// and a number of prior augmentations) with no initializer expression, and the
/// variable's type is nullable, augmented evaluates to null. If the variable's
/// type is not nullable, then it's a compile-time error.
///
/// @description Checks that it is a compile-time error to use `augmented` in an
/// augmenting field's initializer if the member being augmented is not a field
/// with an initializer and the variable's type is not-nullable
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=macros

import augment 'augmented_expression_A03_t11_lib.dart';

final String augmented = "Augmented variable, should not be used";

class C {
Function instanceVariable;
final Function finalInstanceVariable;
C(this.instanceVariable, this.finalInstanceVariable);
}

void foo() {}

enum E {
e1(foo);
final Function finalInstanceVariable;
const E(this.finalInstanceVariable);
}

main() {
print(C);
print(E);
}

0 comments on commit 6066581

Please sign in to comment.