Skip to content

Commit d11262d

Browse files
munificentCommit Queue
authored andcommitted
Add language tests for "Private Named Parameters".
Fix #61641. Change-Id: Id523b0015074d5cb66f5d17b089bedb501e5bc46 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453646 Auto-Submit: Bob Nystrom <[email protected]> Reviewed-by: Kallen Tu <[email protected]> Commit-Queue: Bob Nystrom <[email protected]>
1 parent a135929 commit d11262d

15 files changed

+870
-19
lines changed

tests/language/parameter/initializer6_test.dart

Lines changed: 0 additions & 19 deletions
This file was deleted.
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+
/// Can call constructors with private names during const evaluation.
6+
7+
// SharedOptions=--enable-experiment=private-named-parameters
8+
9+
import 'package:expect/expect.dart';
10+
11+
class C {
12+
final String _foo;
13+
final String _bar;
14+
15+
const C({required this._foo, required this._bar});
16+
}
17+
18+
main() {
19+
const c = C(foo: 'foo', bar: 'bar');
20+
Expect.equals(c._foo, 'foo');
21+
Expect.equals(c._bar, 'bar');
22+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
/// It's an error if a private named declaring parameter collides with an
6+
/// explicit instance variable with the same name. (This is just the normal
7+
/// error for a duplicate field, but we test it explicitly to make sure
8+
/// implementations are checking that for private named parameters.)
9+
10+
// SharedOptions=--enable-experiment=private-named-parameters,primary-constructors
11+
12+
import 'package:expect/expect.dart';
13+
14+
/// Collide with explicitly declared field.
15+
class C1({required final String _foo}) {
16+
final String _foo;
17+
// ^^^^
18+
// [analyzer] unspecified
19+
// [cfe] unspecified
20+
}
21+
22+
/// Collide with previous private declaring parameter.
23+
class C2({required final String _foo, required final String _foo}) {}
24+
// ^^^^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
28+
/// Collide with previous public declaring parameter.
29+
class C3({required final String foo, required final String _foo}) {}
30+
// ^^^^
31+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
32+
// [cfe] unspecified
33+
34+
/// Collide with previous private named parameter.
35+
class C4({String? _foo, required final String _foo}) {}
36+
// ^^^^
37+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_NON_FIELD_PARAMETER
38+
// [cfe] unspecified
39+
// ^^^^
40+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
41+
// [cfe] unspecified
42+
43+
/// Collide with previous public named parameter.
44+
class C5({String? foo, required final String _foo}) {}
45+
// ^^^^
46+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
47+
// [cfe] unspecified
48+
49+
/// Collide with previous private positional parameter.
50+
class C6(String _foo, {required final String _foo}) {}
51+
// ^^^^
52+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
53+
// [cfe] unspecified
54+
55+
/// Collide with previous public positional parameter.
56+
class C7(String? foo, {required final String _foo}) {}
57+
// ^^^^
58+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
59+
// [cfe] unspecified
60+
61+
/// Collide with previous private initializing formal.
62+
class C8(this._foo, {required final String _foo}) {
63+
// ^^^^
64+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
65+
// [cfe] unspecified
66+
final String _foo;
67+
// ^^^^
68+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
69+
// [cfe] unspecified
70+
}
71+
72+
/// Collide with previous public initializing formal.
73+
class C9(this.foo, {required final String _foo}) {
74+
// ^^^^
75+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
76+
// [cfe] unspecified
77+
final String foo;
78+
}
79+
80+
/// Collide with later private named parameter.
81+
class C10({required final String _foo, String? _foo}) {}
82+
// ^^^^
83+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
84+
// [cfe] unspecified
85+
86+
/// Collide with later public named parameter.
87+
class C11({required final String _foo, String? foo}) {}
88+
// ^^^^
89+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
90+
// [cfe] unspecified
91+
92+
/// Collide with later private initializing formal.
93+
class C12({required final String _foo, required this._foo}) {
94+
// ^^^^
95+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
96+
// [cfe] unspecified
97+
final String _foo;
98+
// ^^^^
99+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
100+
// [cfe] unspecified
101+
}
102+
103+
/// Collide with later public initializing formal.
104+
class C13({required final String _foo, required this.foo}) {
105+
// ^^^^
106+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
107+
// [cfe] unspecified
108+
final String foo;
109+
}
110+
111+
void main() {}
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+
/// Private named parameters can be used with declaring private constructor
6+
/// parameters.
7+
8+
// SharedOptions=--enable-experiment=private-named-parameters,primary-constructors
9+
10+
import 'package:expect/expect.dart';
11+
12+
class C({required final String _foo, required final String _bar});
13+
14+
void main() {
15+
var c = C(foo: 'foo', bar: 'bar');
16+
Expect.equals(c._foo, 'foo');
17+
Expect.equals(c._bar, 'bar');
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
/// Private named parameters can't be used in older language versions.
6+
7+
// @dart=3.10
8+
9+
import 'package:expect/expect.dart';
10+
11+
class C {
12+
final String _foo;
13+
14+
C({required this._foo});
15+
// ^^^^
16+
// [analyzer] SYNTACTIC_ERROR.EXPERIMENT_NOT_ENABLED_OFF_BY_DEFAULT
17+
// [cfe] This requires the experimental 'private-named-parameters' language feature to be enabled.
18+
}
19+
20+
void main() {}
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+
/// The type of the parameter should be inferred from the corresponding field.
6+
7+
// SharedOptions=--enable-experiment=private-named-parameters
8+
9+
import 'package:expect/expect.dart';
10+
import 'package:expect/static_type_helper.dart';
11+
12+
class C {
13+
int _x;
14+
C({required this._x});
15+
}
16+
17+
void main() {
18+
C(x: expr(1)..expectStaticType<Exactly<int>>());
19+
}
20+
21+
T expr<T>([Object? v]) => v as T;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
/// It's an error if a private named parameter collides with another parameter
6+
/// with the same public or private name.
7+
8+
// SharedOptions=--enable-experiment=private-named-parameters
9+
10+
class C {
11+
String? foo;
12+
final String? _foo;
13+
14+
// Colliding initializing formals.
15+
C.a({required this._foo, required this._foo}) {}
16+
// ^^^^
17+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_FIELD_FORMAL_PARAMETER
18+
// [cfe] unspecified
19+
20+
// Collide with previous public initializing formal.
21+
C.b({required this.foo, required this._foo}) {}
22+
// ^^^^
23+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
24+
// [cfe] unspecified
25+
26+
// Collide with later private named.
27+
C.c({required this._foo, String? _foo}) {}
28+
// ^^^^
29+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
30+
// [cfe] unspecified
31+
// ^^^^
32+
// [analyzer] SYNTACTIC_ERROR.PRIVATE_NAMED_NON_FIELD_PARAMETER
33+
// [cfe] unspecified
34+
35+
// Collide with later public named.
36+
C.d({required this._foo, String? foo}) {}
37+
// ^^^^
38+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
39+
// [cfe] unspecified
40+
41+
// Collide with previous private named.
42+
C.e({String? _foo, required this._foo}) {}
43+
// ^^^^
44+
// [analyzer] SYNTACTIC_ERROR.PRIVATE_NAMED_NON_FIELD_PARAMETER
45+
// [cfe] unspecified
46+
// ^^^^
47+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
48+
// [cfe] unspecified
49+
50+
// Collide with previous public named.
51+
C.f({String? foo, required this._foo}) {}
52+
// ^^^^
53+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
54+
// [cfe] unspecified
55+
56+
// Collide with previous private positional.
57+
C.g(String _foo, {required this._foo}) {}
58+
// ^^^^
59+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
60+
// [cfe] unspecified
61+
62+
// Collide with previous public positional.
63+
C.h(String? foo, {required this._foo}) {}
64+
// ^^^^
65+
// [analyzer] COMPILE_TIME_ERROR.PRIVATE_NAMED_PARAMETER_DUPLICATE_PUBLIC_NAME
66+
// [cfe] unspecified
67+
}
68+
69+
void main() {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/// Private named parameters can be used on initializing formals.
6+
7+
// SharedOptions=--enable-experiment=private-named-parameters
8+
9+
import 'package:expect/expect.dart';
10+
11+
class C {
12+
String _foo;
13+
String _bar;
14+
15+
C({required this._foo, required this._bar});
16+
}
17+
18+
class Both {
19+
String? _foo;
20+
String? foo;
21+
22+
Both({this._foo});
23+
}
24+
25+
void main() {
26+
var c = C(foo: 'foo', bar: 'bar');
27+
Expect.equals(c._foo, 'foo');
28+
Expect.equals(c._bar, 'bar');
29+
30+
// If a class has both public and private instance variables, the initializing
31+
// formal always refers to the private one.
32+
var b = Both(foo: 'foo');
33+
Expect.equals(b._foo, 'foo');
34+
Expect.equals(b.foo, null);
35+
}
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+
/// Can call constructors with private names as metadata annotations.
6+
7+
// SharedOptions=--enable-experiment=private-named-parameters
8+
9+
import 'package:expect/expect.dart';
10+
11+
class C {
12+
final String _foo;
13+
final String _bar;
14+
15+
const C({required this._foo, required this._bar})
16+
: assert(_foo == 'foo'),
17+
assert(_bar == 'bar');
18+
}
19+
20+
@C(foo: 'foo', bar: 'bar')
21+
void main() {}
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+
/// A private named parameter must refer to an instance variable with the same
6+
/// private name.
7+
8+
// SharedOptions=--enable-experiment=private-named-parameters
9+
10+
class C {
11+
C({this._unknown});
12+
// ^^^^^^^^
13+
// [analyzer] unspecified
14+
// [cfe] unspecified
15+
16+
// There is a public field with the name, but that isn't what the private
17+
// name refers to.
18+
String? unknown;
19+
}
20+
21+
void main() {}

0 commit comments

Comments
 (0)