|
| 1 | +// Copyright (c) 2026, 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 We say that an augmenting function or constructor's signature |
| 6 | +/// matches if: |
| 7 | +/// - It has the same number of type parameters with the same type parameter |
| 8 | +/// names (same identifiers) and bounds (after type annotation inheritance), |
| 9 | +/// if any (same types, even if they may not be written exactly the same in |
| 10 | +/// case one of the declarations needs to refer to a type using an import |
| 11 | +/// prefix). |
| 12 | +/// - The return type (if not omitted) is the same as the augmented |
| 13 | +/// declaration's return type. |
| 14 | +/// - It has the same number of positional and optional parameters as the |
| 15 | +/// augmented declaration. |
| 16 | +/// - It has the same set of named parameter names as the augmented declaration. |
| 17 | +/// - For all corresponding pairs of parameters: |
| 18 | +/// - They have the same type (if not omitted in the augmenting declaration). |
| 19 | +/// - They have the same `required` and `covariant` modifiers. |
| 20 | +/// - For all positional parameters: |
| 21 | +/// - The augmenting function's parameter name is `_`, or |
| 22 | +/// - The augmenting function's parameter name is the same as the name of the |
| 23 | +/// corresponding positional parameter in every preceding declaration that |
| 24 | +/// doesn't have `_` as its name. |
| 25 | +/// ... |
| 26 | +/// It's a compile-time error if: |
| 27 | +/// - The signature of the augmenting getter or setter does not match the |
| 28 | +/// signature of the augmented getter or setter. |
| 29 | +/// |
| 30 | +/// @description Check that it is a compile-time error if a function-typed |
| 31 | +/// variable is augmented by another function-typed variable, but the function |
| 32 | +/// types do not match. Test different return types. |
| 33 | +/// @author sgrekhov22@gmail.com |
| 34 | +
|
| 35 | +// SharedOptions=--enable-experiment=augmentations |
| 36 | + |
| 37 | +var topLevelVariable1 = () => 42 as num; |
| 38 | + |
| 39 | +augment abstract int Function() topLevelVariable1; |
| 40 | +// ^^^^^^^^^^^^^^ |
| 41 | +// [analyzer] unspecified |
| 42 | +// [cfe] unspecified |
| 43 | + |
| 44 | +var topLevelVariable2 = () => 42 as num; |
| 45 | + |
| 46 | +augment abstract Object Function() topLevelVariable2; |
| 47 | +// ^^^^^^^^^^^^^^^^^ |
| 48 | +// [analyzer] unspecified |
| 49 | +// [cfe] unspecified |
| 50 | + |
| 51 | +class C { |
| 52 | + static var staticVariable = () => 42 as num; |
| 53 | + var instanceVariable = () => 42 as num; |
| 54 | +} |
| 55 | + |
| 56 | +augment class C { |
| 57 | + augment static int Function() get staticVariable; |
| 58 | +// ^^^^^^^^^^^^^^ |
| 59 | +// [analyzer] unspecified |
| 60 | +// [cfe] unspecified |
| 61 | + augment static set staticVariable(int Function() _); |
| 62 | +// ^^^^^^^^^^^^^^ |
| 63 | +// [analyzer] unspecified |
| 64 | +// [cfe] unspecified |
| 65 | + augment abstract Object Function() instanceVariable; |
| 66 | +// ^^^^^^^^^^^^^^^^^ |
| 67 | +// [analyzer] unspecified |
| 68 | +// [cfe] unspecified |
| 69 | +} |
| 70 | + |
| 71 | +mixin M { |
| 72 | + static var staticVariable = () => 42 as num; |
| 73 | + var instanceVariable = () => 42 as num; |
| 74 | +} |
| 75 | + |
| 76 | +augment mixin M { |
| 77 | + augment static int Function() get staticVariable; |
| 78 | +// ^^^^^^^^^^^^^^ |
| 79 | +// [analyzer] unspecified |
| 80 | +// [cfe] unspecified |
| 81 | + augment static set staticVariable(int Function() _); |
| 82 | +// ^^^^^^^^^^^^^^ |
| 83 | +// [analyzer] unspecified |
| 84 | +// [cfe] unspecified |
| 85 | + augment abstract Object Function() instanceVariable; |
| 86 | +// ^^^^^^^^^^^^^^^^^ |
| 87 | +// [analyzer] unspecified |
| 88 | +// [cfe] unspecified |
| 89 | +} |
| 90 | + |
| 91 | +enum E { |
| 92 | + e0; |
| 93 | + static var staticVariable = () => 42 as num; |
| 94 | + final instanceVariable = () => 42 as num; |
| 95 | +} |
| 96 | + |
| 97 | +augment enum E { |
| 98 | + ; |
| 99 | + augment static int Function() get staticVariable; |
| 100 | +// ^^^^^^^^^^^^^^ |
| 101 | +// [analyzer] unspecified |
| 102 | +// [cfe] unspecified |
| 103 | + augment static set staticVariable(int Function() _); |
| 104 | +// ^^^^^^^^^^^^^^ |
| 105 | +// [analyzer] unspecified |
| 106 | +// [cfe] unspecified |
| 107 | + augment abstract final Object Function() instanceVariable; |
| 108 | +// ^^^^^^^^^^^^^^^^^ |
| 109 | +// [analyzer] unspecified |
| 110 | +// [cfe] unspecified |
| 111 | +} |
| 112 | + |
| 113 | +class A {} |
| 114 | + |
| 115 | +extension Ext on A { |
| 116 | + static var staticVariable1 = () => 42 as num; |
| 117 | + static var staticVariable2 = () => 42 as num; |
| 118 | +} |
| 119 | + |
| 120 | +augment extension Ext { |
| 121 | + augment static int Function() get staticVariable1; |
| 122 | +// ^^^^^^^^^^^^^^ |
| 123 | +// [analyzer] unspecified |
| 124 | +// [cfe] unspecified |
| 125 | + augment static set staticVariable1(int Function() _); |
| 126 | +// ^^^^^^^^^^^^^^ |
| 127 | +// [analyzer] unspecified |
| 128 | +// [cfe] unspecified |
| 129 | + augment static int Function() get staticVariable2; |
| 130 | +// ^^^^^^^^^^^^^^ |
| 131 | +// [analyzer] unspecified |
| 132 | +// [cfe] unspecified |
| 133 | + augment static set staticVariable2(int Function() _); |
| 134 | +// ^^^^^^^^^^^^^^ |
| 135 | +// [analyzer] unspecified |
| 136 | +// [cfe] unspecified |
| 137 | +} |
| 138 | + |
| 139 | +extension type ET(num Function() instanceVariable) { |
| 140 | + static var staticVariable = () => 42 as num; |
| 141 | +} |
| 142 | + |
| 143 | +augment extension type ET { |
| 144 | + augment static int Function() get staticVariable; |
| 145 | +// ^^^^^^^^^^^^^^ |
| 146 | +// [analyzer] unspecified |
| 147 | +// [cfe] unspecified |
| 148 | + augment static set staticVariable(int Function() _); |
| 149 | +// ^^^^^^^^^^^^^^ |
| 150 | +// [analyzer] unspecified |
| 151 | +// [cfe] unspecified |
| 152 | + augment abstract final Object Function() instanceVariable; |
| 153 | +// ^^^^^^^^^^^^^^^^^ |
| 154 | +// [analyzer] unspecified |
| 155 | +// [cfe] unspecified |
| 156 | +} |
| 157 | + |
| 158 | +main() { |
| 159 | + print(topLevelVariable1); |
| 160 | + print(topLevelVariable2); |
| 161 | + print(C); |
| 162 | + print(M); |
| 163 | + print(E); |
| 164 | + print(A); |
| 165 | + print(ET); |
| 166 | +} |
0 commit comments