Skip to content

Commit 07d2288

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[wildcard-variables] Add a test on unnamed optional parameters with no default value.
Bug: dart-lang/language#3807 Change-Id: Ibeb29d3702b74379b64e8965c3ef9709c7bf2f41 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369780 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Lasse Nielsen <[email protected]>
1 parent cb64e64 commit 07d2288

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// Tests that we still need default values for `this._` and `super._` and that
6+
// we can't add default values to a `_` named parameter where you can't add
7+
// default values.
8+
9+
// SharedOptions=--enable-experiment=wildcard-variables
10+
11+
class SuperClass {
12+
SuperClass([int _]);
13+
// ^
14+
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
15+
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
16+
SuperClass.nullable([int? _]);
17+
}
18+
class SubClass extends SuperClass {
19+
final int _;
20+
SubClass([
21+
this._,
22+
// ^
23+
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
24+
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
25+
super._,
26+
// ^
27+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
28+
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
29+
// [cfe] Duplicated parameter name '_'.
30+
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
31+
// [cfe] Type 'int' of the optional super-initializer parameter '_' doesn't allow 'null', but the parameter doesn't have a default value, and the default value can't be copied from the corresponding parameter of the super constructor.
32+
]);
33+
}
34+
class TypedSubClass extends SuperClass {
35+
final int? _;
36+
TypedSubClass([
37+
int this._,
38+
// ^
39+
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
40+
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
41+
int super._,
42+
// ^
43+
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
44+
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
45+
// [cfe] Duplicated parameter name '_'.
46+
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
47+
// [cfe] Type 'int' of the optional super-initializer parameter '_' doesn't allow 'null', but the parameter doesn't have a default value, and the default value can't be copied from the corresponding parameter of the super constructor.
48+
]) : super.nullable();
49+
}
50+
51+
// Function type parameters cannot have default values.
52+
typedef F = void Function([int _ = 1]);
53+
// ^
54+
// [analyzer] SYNTACTIC_ERROR.DEFAULT_VALUE_IN_FUNCTION_TYPE
55+
// [cfe] Can't have a default value in a function type.
56+
57+
// Redirecting factory constructors cannot have default values.
58+
class ReClass {
59+
ReClass([int x = 0]);
60+
factory ReClass.redir([int _ = 0]) = ReClass;
61+
// ^
62+
// [analyzer] COMPILE_TIME_ERROR.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
63+
// ^
64+
// [cfe] Can't have a default value here because any default values of 'ReClass' would be used instead.
65+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// Unnamed optional parameters, which are not initializing or super parameters,
6+
// that have no default values, have no errors.
7+
8+
// SharedOptions=--enable-experiment=wildcard-variables
9+
10+
import 'package:expect/expect.dart';
11+
12+
class C {
13+
C([int _]) {}
14+
C.otherParams([int _, bool x = false, bool _ = true]) {}
15+
C.otherParams2([int _ = 1, bool x = false, bool _]) {}
16+
17+
int foo([int _]) => 1;
18+
int foo2([bool x = false, bool _, int _]) => 1;
19+
int foo3([bool? x, bool _ = false, int _]) => 1;
20+
int foo4([int _, bool? x, bool _ = false]) => 1;
21+
22+
static int fn([int _]) => 1;
23+
static int fn2([bool x = false, bool _, int _]) => 1;
24+
static int fn3([bool? x, bool _ = false, int _]) => 1;
25+
static int fn4([int _, bool? x, bool _ = false]) => 1;
26+
}
27+
28+
int _([bool _]) => 1;
29+
int topFoo2([bool x = false, bool _, int _]) => 1;
30+
int topFoo3([bool? x, bool _ = false, int _]) => 1;
31+
int topFoo4([int _, bool? x, bool _ = false]) => 1;
32+
33+
void main() {
34+
Expect.equals(1, _());
35+
Expect.equals(1, topFoo2());
36+
Expect.equals(1, topFoo3());
37+
Expect.equals(1, topFoo4());
38+
39+
int foo([int _]) => 1;
40+
int foo2([bool x = false, bool _, int _]) => 1;
41+
int foo3([bool? x, bool _ = false, int _]) => 1;
42+
int foo4([int _, bool? x, bool _ = false]) => 1;
43+
Expect.equals(1, foo());
44+
Expect.equals(1, foo2());
45+
Expect.equals(1, foo3());
46+
Expect.equals(1, foo4());
47+
48+
var c = C();
49+
Expect.equals(1, c.foo());
50+
Expect.equals(1, c.foo2());
51+
Expect.equals(1, c.foo3());
52+
Expect.equals(1, c.foo4());
53+
54+
Expect.equals(1, C.otherParams().foo());
55+
Expect.equals(1, C.otherParams2().foo());
56+
57+
Expect.equals(1, C.fn());
58+
Expect.equals(1, C.fn2());
59+
Expect.equals(1, C.fn3());
60+
Expect.equals(1, C.fn4());
61+
}

0 commit comments

Comments
 (0)