Skip to content

Commit 222947d

Browse files
kallentuCommit Queue
authored andcommitted
[tests] Primary constructors: Optional parameters, constructor body, extension type optional parameters.
Tests the following: - Optional/named parameters for extension type primary constructors. - We should still produce an error when there's a body on a const constructor. - Inferring `Object?` as the representation type when the type isn't specified. - We can omit the type of an optional parameter with a default value, in which case, the type is inferred from the default value. Bug: #61687 Change-Id: Id10fb3f3a14cf683601f52b0b3182a10d0730927 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465801 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent e6228e3 commit 222947d

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed

tests/language/primary_constructors/header/extension_type_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@ extension type ET3(i);
1717

1818
extension type ET4(final i);
1919

20+
extension type ET5({required int i});
21+
22+
extension type ET6({int i = 0});
23+
24+
extension type ET7({int? i});
25+
26+
extension type ET8([int? i]);
27+
2028
void main() {
2129
Expect.equals(1, ET1(1).i);
2230
Expect.equals(1, ET2(1).i);
2331
Expect.equals(1, ET3(1).i);
2432
Expect.equals(1, ET4(1).i);
33+
Expect.equals(1, ET5(i: 1).i);
34+
Expect.equals(0, ET6().i);
35+
Expect.equals(1, ET6(i: 1).i);
36+
Expect.equals(null, ET7().i);
37+
Expect.equals(1, ET7(i: 1).i);
38+
Expect.equals(null, ET8().i);
39+
Expect.equals(1, ET8(1).i);
2540
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// We emit an error if a const constructor has a body, even with the new,
6+
// shorter syntax.
7+
8+
// SharedOptions=--enable-experiment=primary-constructors
9+
10+
class C {
11+
final int x;
12+
const new(this.x) {}
13+
// ^
14+
// [analyzer] unspecified
15+
// [cfe] unspecified
16+
17+
const new named(this.x) {}
18+
// ^
19+
// [analyzer] unspecified
20+
// [cfe] unspecified
21+
}

tests/language/primary_constructors/syntax/header_syntax_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ enum EnumOptionalParameters([final int x = 1, var int y = 2]) {
4444
e(3, 4), f(3), g();
4545
}
4646

47-
// TODO(kallentu): Add tests for the type being inferred from the default value.
48-
4947
void main() {
5048
var p1 = Point(1, 2);
5149
Expect.equals(1, p1.x);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// Tests that we infer `Object?` for an extension type's representation type if
6+
// the type isn't specified.
7+
8+
// SharedOptions=--enable-experiment=primary-constructors
9+
10+
extension type ET(i);
11+
12+
void main() {
13+
var et3 = ET3(1);
14+
if (1 > 2) et3.i.arglebargle;
15+
// ^
16+
// [analyzer] unspecified
17+
// [cfe] unspecified
18+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// We can omit the type of an optional parameter with a default value, in which
6+
// case the type is inferred from the default value.
7+
8+
// SharedOptions=--enable-experiment=primary-constructors
9+
10+
import 'package:expect/expect.dart';
11+
import "package:expect/static_type_helper.dart";
12+
13+
// Named parameters.
14+
class Named({final x = 1, var y = 2}) {
15+
final Object o;
16+
17+
this : o = (x, y).expectStaticType<Exactly<(int, int)>> {
18+
(x, y).expectStatictype<Exactly<(int, int)>>;
19+
}
20+
}
21+
22+
enum NamedEnum({final x = 1}) {
23+
e(x: 2), f();
24+
25+
final Object o;
26+
27+
this : o = x.expectStatictype<Exactly<int>>;
28+
29+
void expectIntStaticType() {
30+
x.expectStatictype<Exactly<int>>;
31+
}
32+
}
33+
34+
// Optional parameters.
35+
class Optional([final x = 1, var y = 2]) {
36+
final Object o;
37+
38+
this : o = (x, y).expectStaticType<Exactly<(int, int)>> {
39+
(x, y).expectStatictype<Exactly<(int, int)>>;
40+
}
41+
}
42+
43+
enum OptionalEnum([final x = 1]) {
44+
e(2), f();
45+
46+
final Object o;
47+
48+
this : o = x.expectStatictype<Exactly<int>>;
49+
50+
void expectIntStaticType() {
51+
x.expectStatictype<Exactly<int>>;
52+
}
53+
}
54+
55+
void main() {
56+
var named = Named();
57+
named.x.expectStaticType<Exactly<int>>;
58+
named.y.expectStaticType<Exactly<int>>;
59+
Expect.equals(1, named.x);
60+
Expect.equals(2, named.y);
61+
62+
var namedEnumE = NamedEnum.e;
63+
namedEnumE.x.expectStaticType<Exactly<int>>;
64+
Expect.equals(2, namedEnumE.x);
65+
namedEnumE.expectIntStaticType();
66+
67+
var namedEnumF = NamedEnum.f;
68+
namedEnumF.x.expectStaticType<Exactly<int>>;
69+
Expect.equals(1, namedEnumF.x);
70+
namedEnumF.expectIntStaticType();
71+
72+
var optional = Optional();
73+
optional.x.expectStaticType<Exactly<int>>;
74+
optional.y.expectStaticType<Exactly<int>>;
75+
Expect.equals(1, optional.x);
76+
Expect.equals(2, optional.y);
77+
78+
var optionalEnumE = OptionalEnum.e;
79+
optionalEnumE.x.expectStaticType<Exactly<int>>;
80+
Expect.equals(2, optionalEnumE.x);
81+
optionalEnumE.expectIntStaticType();
82+
83+
var optionalEnumF = OptionalEnum.f;
84+
optionalEnumF.x.expectStaticType<Exactly<int>>;
85+
Expect.equals(1, optionalEnumF.x);
86+
optionalEnumF.expectIntStaticType();
87+
}

0 commit comments

Comments
 (0)