Skip to content

Commit c2fd94d

Browse files
committed
#3182. Add test for augmenting function-typed variable
1 parent 3932876 commit c2fd94d

13 files changed

Lines changed: 1838 additions & 0 deletions
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 augmentation by `Function`.
33+
/// @author sgrekhov22@gmail.com
34+
35+
// SharedOptions=--enable-experiment=augmentations
36+
37+
var topLevelVariable = () {};
38+
39+
augment abstract Function topLevelVariable;
40+
// ^^^^^^^^
41+
// [analyzer] unspecified
42+
// [cfe] unspecified
43+
44+
class C {
45+
static final staticVariable = () {};
46+
var instanceVariable = () {};
47+
}
48+
49+
augment class C {
50+
augment static Function get staticVariable;
51+
// ^^^^^^^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
augment static set staticVariable(Function _);
55+
// ^^^^^^^^
56+
// [analyzer] unspecified
57+
// [cfe] unspecified
58+
augment abstract Function instanceVariable;
59+
// ^^^^^^^^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
}
63+
64+
mixin M {
65+
static var staticVariable = () {};
66+
var instanceVariable = () {};
67+
}
68+
69+
augment mixin M {
70+
augment static Function get staticVariable;
71+
// ^^^^^^^^
72+
// [analyzer] unspecified
73+
// [cfe] unspecified
74+
augment static set staticVariable(Function _);
75+
// ^^^^^^^^
76+
// [analyzer] unspecified
77+
// [cfe] unspecified
78+
augment abstract Function instanceVariable;
79+
// ^^^^^^^^
80+
// [analyzer] unspecified
81+
// [cfe] unspecified
82+
}
83+
84+
enum E {
85+
e0;
86+
static var staticVariable = () {};
87+
final instanceVariable = () {};
88+
}
89+
90+
augment enum E {
91+
;
92+
augment static Function get staticVariable;
93+
// ^^^^^^^^
94+
// [analyzer] unspecified
95+
// [cfe] unspecified
96+
augment static set staticVariable(Function _);
97+
// ^^^^^^^^
98+
// [analyzer] unspecified
99+
// [cfe] unspecified
100+
augment abstract final Function instanceVariable;
101+
// ^^^^^^^^
102+
// [analyzer] unspecified
103+
// [cfe] unspecified
104+
}
105+
106+
class A {}
107+
108+
extension Ext on A {
109+
static var staticVariable = () {};
110+
}
111+
112+
augment extension Ext {
113+
augment static get Function staticVariable;
114+
// ^^^^^^^^
115+
// [analyzer] unspecified
116+
// [cfe] unspecified
117+
augment static set staticVariable(Function _);
118+
// ^^^^^^^^
119+
// [analyzer] unspecified
120+
// [cfe] unspecified
121+
}
122+
123+
extension type ET(void Function() instanceVariable) {
124+
static var staticVariable = () {};
125+
}
126+
127+
augment extension type ET {
128+
augment static get Function staticVariable;
129+
// ^^^^^^^^
130+
// [analyzer] unspecified
131+
// [cfe] unspecified
132+
augment static set staticVariable(Function _);
133+
// ^^^^^^^^
134+
// [analyzer] unspecified
135+
// [cfe] unspecified
136+
augment abstract final Function instanceVariable;
137+
// ^^^^^^^^
138+
// [analyzer] unspecified
139+
// [cfe] unspecified
140+
}
141+
142+
main() {
143+
print(topLevelVariable);
144+
print(C);
145+
print(M);
146+
print(E);
147+
print(A);
148+
print(ET);
149+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)