Skip to content

Commit e3a088d

Browse files
authored
#3330. Add static extensions tests. Part 2. (#3382)
Add static extensions tests. Part 2.
1 parent 8e2093c commit e3a088d

File tree

8 files changed

+532
-0
lines changed

8 files changed

+532
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2025, 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 Consider an expression `e` which is a member invocation with
6+
/// syntactic receiver `C` and an associated member name `m`. Assume that `m` is
7+
/// a static member declared by `D`. The static analysis and dynamic semantics
8+
/// of this expression is the same as in Dart before the introduction of this
9+
/// feature.
10+
///
11+
/// @description Checks that if `C` contains a static member with the basename
12+
/// `m` then it is not an error for extensions to declare a static member with
13+
/// the same basename. Invocation of `m` will invoke the `on` declaration member
14+
/// @author [email protected]
15+
16+
// SharedOptions=--enable-experiment=static-extensions
17+
18+
import '../../Utils/expect.dart';
19+
20+
String log = "";
21+
22+
class C {
23+
static int foo = 42;
24+
}
25+
26+
mixin M {
27+
static int foo() => 42;
28+
}
29+
30+
extension type ET(int _) {
31+
static int get foo => 42;
32+
}
33+
34+
enum E {
35+
e0;
36+
static void set foo(int v) {
37+
log = "$v";
38+
}
39+
}
40+
41+
extension ExtC on C {
42+
static String foo() => "ExtC";
43+
}
44+
45+
extension ExtM on M {
46+
static String foo() => "ExtM";
47+
}
48+
49+
extension ExtET on ET {
50+
static String foo() => "ExtET";
51+
}
52+
53+
extension ExtE on E {
54+
static String foo() => "ExtE";
55+
}
56+
57+
main() {
58+
Expect.equals(42, C.foo);
59+
Expect.equals(42, M.foo());
60+
Expect.equals(42, ET.foo);
61+
E.foo = 42;
62+
Expect.equals("42", log);
63+
64+
Expect.equals("ExtC", ExtC.foo());
65+
Expect.equals("ExtM", ExtM.foo());
66+
Expect.equals("ExtET", ExtET.foo());
67+
Expect.equals("ExtE", ExtE.foo());
68+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2025, 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 Consider an expression `e` which is a member invocation with
6+
/// syntactic receiver `C` and an associated member name `m`. Assume that `m` is
7+
/// a static member declared by `D`. The static analysis and dynamic semantics
8+
/// of this expression is the same as in Dart before the introduction of this
9+
/// feature.
10+
///
11+
/// @description Checks that if `C` contains a static member with the basename
12+
/// `m` then it is not an error for extensions to declare a static member with
13+
/// the same basename. Invocation of `m` will invoke the `on` declaration member
14+
/// @author [email protected]
15+
16+
// SharedOptions=--enable-experiment=static-extensions
17+
18+
import '../../Utils/expect.dart';
19+
20+
String log = "";
21+
22+
class C {
23+
int foo = 42;
24+
}
25+
26+
mixin M {
27+
int foo() => 42;
28+
}
29+
30+
class MA = Object with M;
31+
32+
extension type ET(int _) {
33+
int get foo => 42;
34+
}
35+
36+
enum E {
37+
e0;
38+
void set foo(int v) {
39+
log = "$v";
40+
}
41+
}
42+
43+
extension ExtC on C {
44+
static String foo() => "ExtC";
45+
}
46+
47+
extension ExtM on M {
48+
static String foo() => "ExtM";
49+
}
50+
51+
extension ExtET on ET {
52+
static String foo() => "ExtET";
53+
}
54+
55+
extension ExtE on E {
56+
static String foo() => "ExtE";
57+
}
58+
59+
main() {
60+
Expect.equals(42, C().foo);
61+
Expect.equals(42, MA().foo());
62+
Expect.equals(42, ET(0).foo);
63+
E.e0.foo = 42;
64+
Expect.equals("42", log);
65+
66+
Expect.equals("ExtC", ExtC.foo());
67+
Expect.equals("ExtM", ExtM.foo());
68+
Expect.equals("ExtET", ExtET.foo());
69+
Expect.equals("ExtE", ExtE.foo());
70+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2025, 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 Consider an expression `e` which is a member invocation with
6+
/// syntactic receiver `C` and an associated member name `m`. Assume that `m` is
7+
/// a static member declared by `D`. The static analysis and dynamic semantics
8+
/// of this expression is the same as in Dart before the introduction of this
9+
/// feature.
10+
/// ...
11+
/// When `D` declares a static member whose basename is the basename of `m`, but
12+
/// `D` does not declare a static member named `m` or a constructor named `C.m`,
13+
/// a compile-time error occurs.
14+
///
15+
/// @description Checks that it is a compile-time error if `D` declares a static
16+
/// member whose basename is the basename of `m`, but `D` does not declare a
17+
/// member named `m`
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=static-extensions
21+
22+
class C {
23+
static final int foo = 42;
24+
}
25+
26+
mixin M {
27+
static int foo() => 42;
28+
}
29+
30+
extension type ET(int _) {
31+
static int get foo => 42;
32+
}
33+
34+
enum E {
35+
e0;
36+
static int foo() => 42;
37+
}
38+
39+
extension ExtC on C {
40+
static void set foo(int _) {}
41+
}
42+
43+
extension ExtM on M {
44+
static void set foo(int _) {}
45+
}
46+
47+
extension ExtET on ET {
48+
static void set foo(int _) {}
49+
}
50+
51+
extension ExtE on E {
52+
static void set foo(int _) {}
53+
}
54+
55+
main() {
56+
C.foo = 42;
57+
// ^^^
58+
// [analyzer] unspecified
59+
// [cfe] unspecified
60+
M.foo = 42;
61+
// ^^^
62+
// [analyzer] unspecified
63+
// [cfe] unspecified
64+
ET.foo = 42;
65+
// ^^^
66+
// [analyzer] unspecified
67+
// [cfe] unspecified
68+
E.foo = 42;
69+
// ^^^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2025, 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 Consider an expression `e` which is a member invocation with
6+
/// syntactic receiver `C` and an associated member name `m`. Assume that `m` is
7+
/// a static member declared by `D`. The static analysis and dynamic semantics
8+
/// of this expression is the same as in Dart before the introduction of this
9+
/// feature.
10+
/// ...
11+
/// When `D` declares a static member whose basename is the basename of `m`, but
12+
/// `D` does not declare a static member named `m` or a constructor named `C.m`,
13+
/// a compile-time error occurs.
14+
///
15+
/// @description Checks that it is a compile-time error if `D` declares a static
16+
/// member whose basename is the basename of `m`, but `D` does not declare a
17+
/// member named `m`
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=static-extensions
21+
22+
class C {
23+
static void set foo(int _) {}
24+
}
25+
26+
mixin M {
27+
static void set foo(int _) {}
28+
}
29+
30+
extension type ET(int _) {
31+
static void set foo(int _) {}
32+
}
33+
34+
enum E {
35+
e0;
36+
static void set foo(int _) {}
37+
}
38+
39+
extension ExtC on C {
40+
static final int foo = 42;
41+
}
42+
43+
extension ExtM on M {
44+
static int foo() => 42;
45+
}
46+
47+
extension ExtET on ET {
48+
static int get foo => 42;
49+
}
50+
51+
extension ExtE on E {
52+
static int get foo => 42;
53+
}
54+
55+
main() {
56+
C.foo;
57+
// ^^^
58+
// [analyzer] unspecified
59+
// [cfe] unspecified
60+
M.foo();
61+
// ^^^
62+
// [analyzer] unspecified
63+
// [cfe] unspecified
64+
ET.foo;
65+
// ^^^
66+
// [analyzer] unspecified
67+
// [cfe] unspecified
68+
E.foo;
69+
// ^^^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2025, 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 Consider an expression `e` which is a member invocation with
6+
/// syntactic receiver `C` and an associated member name `m`. Assume that `m` is
7+
/// a static member declared by `D`. The static analysis and dynamic semantics
8+
/// of this expression is the same as in Dart before the introduction of this
9+
/// feature.
10+
/// ...
11+
/// Assume that it is an extension `E` that declares a static member named `m`.
12+
/// The invocation is then treated as `E.m()`.
13+
///
14+
/// @description Checks that invocation of `m` is then treated as `E.m()`. Test
15+
/// a static variable as `m`.
16+
/// @author [email protected]
17+
18+
// SharedOptions=--enable-experiment=static-extensions
19+
20+
import '../../Utils/expect.dart';
21+
22+
class C {}
23+
24+
mixin M {}
25+
26+
extension type ET(int _) {}
27+
28+
enum E {
29+
e0;
30+
}
31+
32+
extension ExtC on C {
33+
static String foo = "ExtC";
34+
}
35+
36+
extension ExtM on M {
37+
static String foo = "ExtM";
38+
}
39+
40+
extension ExtET on ET {
41+
static String foo = "ExtET";
42+
}
43+
44+
extension ExtE on E {
45+
static String foo = "ExtE";
46+
}
47+
48+
main() {
49+
Expect.equals("ExtC", C.foo);
50+
Expect.equals("ExtM", M.foo);
51+
Expect.equals("ExtET", ET.foo);
52+
Expect.equals("ExtE", E.foo);
53+
54+
Expect.equals("ExtC", ExtC.foo);
55+
Expect.equals("ExtM", ExtM.foo);
56+
Expect.equals("ExtET", ExtET.foo);
57+
Expect.equals("ExtE", ExtE.foo);
58+
}

0 commit comments

Comments
 (0)