Skip to content

Commit 2ed121f

Browse files
kallentuCommit Queue
authored andcommitted
[tests] Enum shorthands - Tests for constructors and static members.
A few starting tests for constructor and static member shorthands. Test compile-time errors for `.new<types>` and `.new<types>()`. Test that type alias get expanded before the shorthand inference is applied. Bug: #57038 Change-Id: If32c9c027021ab31e7a9a5a07167058fac62f521 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398588 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent 77caf36 commit 2ed121f

File tree

6 files changed

+299
-0
lines changed

6 files changed

+299
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2024, 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+
// Errors involving enum shorthands of constructors.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
void main() {
12+
// Using a constructor shorthand without any context.
13+
14+
var ctorNew = .new();
15+
// ^
16+
// [analyzer] unspecified
17+
// [cfe] unspecified
18+
19+
const ctorConstNew = .new();
20+
// ^
21+
// [analyzer] unspecified
22+
// [cfe] unspecified
23+
24+
var ctorNamed = .regular();
25+
// ^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
29+
const ctorConstNamed = .regular();
30+
// ^
31+
// [analyzer] unspecified
32+
// [cfe] unspecified
33+
34+
// `.new<type-args>()` and `.new<type-args>` are a compile-time error.
35+
36+
UnnamedConstructorTypeParameters typeParameters = .new<int>();
37+
// ^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
41+
UnnamedConstructorTypeParameters Function() tearOff = .new<int>;
42+
// ^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2024, 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+
// Testing shorthands for constructor calls.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
class ConstructorClassContext {
12+
final ConstructorClass? x;
13+
ConstructorClassContext(this.x);
14+
ConstructorClassContext.named({this.x});
15+
ConstructorClassContext.optional([this.x]);
16+
}
17+
18+
class ConstructorExtContext {
19+
final ConstructorExt? x;
20+
ConstructorExtContext(this.x);
21+
ConstructorExtContext.named({this.x});
22+
ConstructorExtContext.optional([this.x]);
23+
}
24+
25+
void main() {
26+
int x = 1;
27+
28+
ConstructorClass ctor = .new(x);
29+
ConstructorClass ctor1 = .regular(x);
30+
ConstructorClass ctor2 = .named(x: x);
31+
ConstructorClass ctor3 = .optional(x);
32+
ConstructorClass ctor4 = .constRegular(x);
33+
ConstructorClass ctor5 = .constNamed(x: x);
34+
ConstructorClass ctor6 = .constOptional(x);
35+
36+
ConstructorExt ctorExt = .new(x);
37+
ConstructorExt ctorExt1 = .regular(x);
38+
ConstructorExt ctorExt2 = .named(x: x);
39+
ConstructorExt ctorExt3 = .optional(x);
40+
ConstructorExt ctorExt4 = .constRegular(x);
41+
ConstructorExt ctorExt5 = .constNamed(x: x);
42+
ConstructorExt ctorExt6 = .constOptional(x);
43+
44+
ConstructorClass? ctorNullable = .new(x);
45+
ConstructorClass? ctorNullable1 = .regular(x);
46+
ConstructorClass? ctorNullable2 = .named(x: x);
47+
ConstructorClass? ctorNullable3 = .optional(x);
48+
ConstructorClass? ctorNullable4 = .constRegular(x);
49+
ConstructorClass? ctorNullable5 = .constNamed(x: x);
50+
ConstructorClass? ctorNullable6 = .constOptional(x);
51+
52+
ConstructorExt? ctorExtNullable = .new(x);
53+
ConstructorExt? ctorExtNullable1 = .regular(x);
54+
ConstructorExt? ctorExtNullable2= .named(x: x);
55+
ConstructorExt? ctorExtNullable3 = .optional(x);
56+
ConstructorExt? ctorExtNullable4 = .constRegular(x);
57+
ConstructorExt? ctorExtNullable5 = .constNamed(x: x);
58+
ConstructorExt? ctorExtNullable6 = .constOptional(x);
59+
60+
UnnamedConstructor Function() ctorTearoff = .new;
61+
62+
// Parameter context type.
63+
ConstructorClassContext(.new(1));
64+
ConstructorClassContext.named(x: .optional(1));
65+
ConstructorClassContext.optional(.optional(1));
66+
67+
ConstructorExtContext(.new(1));
68+
ConstructorExtContext.named(x: .optional(1));
69+
ConstructorExtContext.optional(.optional(1));
70+
71+
// Collection
72+
<ConstructorClass>[.new(x), .regular(x), .constRegular(x)];
73+
<ConstructorClass?>[.new(x), .regular(x), .constRegular(x)];
74+
<ConstructorExt>[.new(x), .regular(x), .constRegular(x)];
75+
<ConstructorExt?>[.new(x), .regular(x), .constRegular(x)];
76+
}
77+

tests/language/enum_shorthands/enum_shorthand_helper.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,44 @@ class _IntegerWithMixin extends Integer with IntegerMixin {
3737
const _IntegerWithMixin(int integer) : this._(integer);
3838
const _IntegerWithMixin._(super.integer) : super._();
3939
}
40+
41+
// Selector chain test declarations.
42+
43+
class ConstructorClass {
44+
final int? x;
45+
ConstructorClass(this.x);
46+
ConstructorClass.regular(this.x);
47+
ConstructorClass.named({this.x});
48+
ConstructorClass.optional([this.x]);
49+
50+
const ConstructorClass.constRegular(this.x);
51+
const ConstructorClass.constNamed({this.x});
52+
const ConstructorClass.constOptional([this.x]);
53+
}
54+
55+
class UnnamedConstructor {}
56+
57+
class UnnamedConstructorTypeParameters<T> {}
58+
59+
extension type ConstructorExt(int? x) {
60+
ConstructorExt.regular(this.x);
61+
ConstructorExt.named({this.x});
62+
ConstructorExt.optional([this.x]);
63+
64+
const ConstructorExt.constRegular(this.x);
65+
const ConstructorExt.constNamed({this.x});
66+
const ConstructorExt.constOptional([this.x]);
67+
}
68+
69+
class StaticMember<T> {
70+
static StaticMember<int> member() => StaticMember(1);
71+
static StaticMember<U> memberType<U, V>(U u) => StaticMember(u);
72+
73+
final T t;
74+
StaticMember(this.t);
75+
}
76+
77+
extension type StaticMemberExt<T>(T x) {
78+
static StaticMemberExt<int> member() => StaticMemberExt(1);
79+
static StaticMemberExt<U> memberType<U, V>(U u) => StaticMemberExt(u);
80+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2024, 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+
// Basic usages of enum shorthands with static members in classes and extension
6+
// types.
7+
8+
// SharedOptions=--enable-experiment=enum-shorthands
9+
10+
import '../enum_shorthand_helper.dart';
11+
12+
class StaticMemberContext {
13+
final StaticMember? clas;
14+
StaticMemberContext(this.clas);
15+
StaticMemberContext.named({this.clas});
16+
StaticMemberContext.optional([this.clas]);
17+
}
18+
19+
class StaticMemberExtContext {
20+
final StaticMemberExt? ext;
21+
StaticMemberExtContext(this.ext);
22+
StaticMemberExtContext.named({this.ext});
23+
StaticMemberExtContext.optional([this.ext]);
24+
}
25+
26+
void main() {
27+
StaticMember<int> s = .member();
28+
StaticMemberContext(.member());
29+
StaticMemberContext.named(clas: .member());
30+
StaticMemberContext.optional(.member());
31+
32+
StaticMember<String> sTypeParameters = .memberType("s");
33+
StaticMemberContext(.memberType("s"));
34+
StaticMemberContext.named(clas: .memberType("s"));
35+
StaticMemberContext.optional(.memberType("s"));
36+
37+
StaticMemberExt<int> sExt = .member();
38+
StaticMemberExtContext(.member());
39+
StaticMemberExtContext.named(ext: .member());
40+
StaticMemberExtContext.optional(.member());
41+
42+
StaticMemberExt<String> sTypeParametersExt = .memberType("s");
43+
StaticMemberExtContext(.memberType("s"));
44+
StaticMemberExtContext.named(ext: .memberType("s"));
45+
StaticMemberExtContext.optional(.memberType("s"));
46+
47+
<StaticMember>[.member(), .memberType('s')];
48+
<StaticMember?>[.member(), .memberType('s')];
49+
<StaticMemberExt>[.member(), .memberType('s')];
50+
<StaticMemberExt?>[.member(), .memberType('s')];
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2024, 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+
// Errors when the type parameters of the shorthand methods don't match the
6+
// context type.
7+
8+
// SharedOptions=--enable-experiment=enum-shorthands
9+
10+
import '../enum_shorthand_helper.dart';
11+
12+
void main() {
13+
StaticMember<bool> s = .member();
14+
// ^
15+
// [analyzer] unspecified
16+
// [cfe] unspecified
17+
18+
StaticMember<int> sTypeParameters = .memberType("s");
19+
// ^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
23+
StaticMemberExt<bool> sExt = .member();
24+
// ^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
28+
StaticMemberExt<int> sTypeParametersExt = .memberType("s");
29+
// ^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2024, 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+
// Type aliases with enum shorthands.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
typedef ClassAlias = A<int>;
10+
typedef ExtensionAlias = AExt<int>;
11+
12+
class A<T> {
13+
static ClassAlias get alias => A(1);
14+
15+
static const ClassAlias constAlias = const A._(1);
16+
17+
static ClassAlias method() => A(1);
18+
19+
final T t;
20+
21+
A(this.t);
22+
23+
const A._(this.t);
24+
25+
ClassAlias get aliasGetter => A(1);
26+
}
27+
28+
extension type AExt<T>(T t) {
29+
static ExtensionAlias get alias => AExt(1);
30+
31+
static const ExtensionAlias constAlias = const AExt._(1);
32+
33+
static ExtensionAlias method() => AExt(1);
34+
35+
const AExt._(this.t);
36+
37+
ExtensionAlias get aliasGetter => AExt(1);
38+
}
39+
40+
41+
void main() {
42+
// Class
43+
ClassAlias classAlias = .alias;
44+
ClassAlias classAlias2 = .new('s').aliasGetter;
45+
ClassAlias classAliasMethod = .method();
46+
const ClassAlias constClassAlias = .constAlias;
47+
48+
// Extension type
49+
ExtensionAlias extensionAlias = .alias;
50+
ExtensionAlias extensionAliasCtor = .new('s').aliasGetter;
51+
ExtensionAlias extensionAliasMethod = .method();
52+
const ExtensionAlias constExtensionAlias = .constAlias;
53+
}

0 commit comments

Comments
 (0)