Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5dca2f3

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[tests] Primary constructors - Tests new shorter syntax and updating old tests.
Tests the following: - `factory() => C();` is a factory constructor whose name is the name of the enclosing class, and not a method. - `factory` named and unnamed shorter syntax - `new` named and unnamed shorter syntax I've also updated the empty body test with an extra case and changed the comment of another test to remove the mention of in-body declaring constructors. Bug: #61687 Change-Id: I8416f413e44a8503168ed3fdf2262523ee1deabf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/460882 Reviewed-by: Erik Ernst <eernst@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
1 parent 83b47a8 commit 5dca2f3

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// `factory() => C();` is a factory constructor whose name is the name of the
6+
// enclosing class, and not a method.
7+
8+
// SharedOptions=--enable-experiment=declaring-constructors
9+
10+
class C {
11+
final int x;
12+
C.named(this.x);
13+
factory() => C.named(1); // Equivalent to `factory C() => C.named();`
14+
}
15+
16+
void main() {
17+
var c = C.named(1);
18+
c.factory();
19+
//^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// Using `factory name()` to declare named factory constructors with new shorter
6+
// syntax.
7+
8+
// SharedOptions=--enable-experiment=declaring-constructors
9+
10+
import "package:expect/expect.dart";
11+
12+
class C1 {
13+
final int x;
14+
C1._(this.x);
15+
16+
// Equivalent to `factory C1.named() => C1._(1);`
17+
factory named() => C1._(1);
18+
}
19+
20+
class C2 {
21+
final int x;
22+
const C2._() : x = 1;
23+
24+
// Equivalent to `const factory C2.named() = C2._;`
25+
const factory named() = C2._;
26+
}
27+
28+
void main() {
29+
Expect.equals(1, C1.named().x);
30+
Expect.equals(1, C2.named().x);
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// `factory() => C();` is a factory constructor whose name is the name of the
6+
// enclosing class, and not a method.
7+
8+
// SharedOptions=--enable-experiment=declaring-constructors
9+
10+
import "package:expect/expect.dart";
11+
12+
class C1 {
13+
final int x;
14+
C1._(this.x);
15+
factory() => C1._(1); // Equivalent to `factory C1() => C1._(1);`
16+
}
17+
18+
class C2 {
19+
final int x;
20+
const C2._() : x = 1;
21+
const factory() = C2._; // Equivalent to `const factory C2() = C2._;`
22+
}
23+
24+
void main() {
25+
Expect.equals(1, C1().x);
26+
Expect.equals(1, C2().x);
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// Using `new` to declare constructors.
6+
7+
// SharedOptions=--enable-experiment=declaring-constructors
8+
9+
import "package:expect/expect.dart";
10+
11+
class C1 {
12+
int x;
13+
new() : x = 1; // Equivalent to `C1() : x = 1;`
14+
new named() : x = 1; // Equivalent to `C1.named() : x = 1;`
15+
}
16+
17+
class C2 {
18+
int x;
19+
new() : x = 1 {} // Equivalent to `C1() : x = 1 {}`
20+
new named() : x = 1 {}// Equivalent to `C1.named() : x = 1 {}`
21+
}
22+
23+
class C3 {
24+
final int x;
25+
const new(this.x); // Equivalent to `const C2(this.x);`
26+
const new named(this.x); // Equivalent to `const C2.named(this.x);`
27+
}
28+
29+
class C4 {
30+
final int x;
31+
const new(this.x) {} // Equivalent to `const C2(this.x) {}`
32+
const new named(this.x) {} // Equivalent to `const C2.named(this.x) {}`
33+
}
34+
35+
void main() {
36+
Expect.equals(1, C1().x);
37+
Expect.equals(1, C1.named().x);
38+
Expect.equals(1, C2().x);
39+
Expect.equals(1, C2.named().x);
40+
Expect.equals(1, C3(1).x);
41+
Expect.equals(1, C3.named(1).x);
42+
Expect.equals(1, C4(1).x);
43+
Expect.equals(1, C4.named(1).x);
44+
}

‎tests/language/declaring_constructors/syntax/empty_body_error_test.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ enum const E4(final int x);
4343
// ^
4444
// [analyzer] unspecified
4545
// [cfe] unspecified
46+
47+
class B = C1 with M1;
48+
// ^
49+
// [analyzer] unspecified
50+
// [cfe] unspecified

‎tests/language/declaring_constructors/syntax/no_parameter_list_error_test.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// It is an error to have a declaring constructor in the class body, but
6-
// no declaring parameter list, neither in the header nor in the body.
5+
// It is an error if a class does not have a primary constructor, but the body
6+
// of the class contains a primary constructor body.
77

88
// SharedOptions=--enable-experiment=declaring-constructors
99

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /