[wildcard-variables] Add a test on unnamed optional parameters with no default value.

Bug: https://github.com/dart-lang/language/issues/3807
Change-Id: Ibeb29d3702b74379b64e8965c3ef9709c7bf2f41
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369780
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
This commit is contained in:
Kallen Tu 2024-06-07 22:02:17 +00:00 committed by Commit Queue
parent cb64e64ccc
commit 07d2288908
2 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,65 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests that we still need default values for `this._` and `super._` and that
// we can't add default values to a `_` named parameter where you can't add
// default values.
// SharedOptions=--enable-experiment=wildcard-variables
class SuperClass {
SuperClass([int _]);
// ^
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
SuperClass.nullable([int? _]);
}
class SubClass extends SuperClass {
final int _;
SubClass([
this._,
// ^
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
super._,
// ^
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
// [cfe] Duplicated parameter name '_'.
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
// [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.
]);
}
class TypedSubClass extends SuperClass {
final int? _;
TypedSubClass([
int this._,
// ^
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
int super._,
// ^
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
// [cfe] Duplicated parameter name '_'.
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
// [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.
]) : super.nullable();
}
// Function type parameters cannot have default values.
typedef F = void Function([int _ = 1]);
// ^
// [analyzer] SYNTACTIC_ERROR.DEFAULT_VALUE_IN_FUNCTION_TYPE
// [cfe] Can't have a default value in a function type.
// Redirecting factory constructors cannot have default values.
class ReClass {
ReClass([int x = 0]);
factory ReClass.redir([int _ = 0]) = ReClass;
// ^
// [analyzer] COMPILE_TIME_ERROR.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
// ^
// [cfe] Can't have a default value here because any default values of 'ReClass' would be used instead.
}

View file

@ -0,0 +1,61 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Unnamed optional parameters, which are not initializing or super parameters,
// that have no default values, have no errors.
// SharedOptions=--enable-experiment=wildcard-variables
import 'package:expect/expect.dart';
class C {
C([int _]) {}
C.otherParams([int _, bool x = false, bool _ = true]) {}
C.otherParams2([int _ = 1, bool x = false, bool _]) {}
int foo([int _]) => 1;
int foo2([bool x = false, bool _, int _]) => 1;
int foo3([bool? x, bool _ = false, int _]) => 1;
int foo4([int _, bool? x, bool _ = false]) => 1;
static int fn([int _]) => 1;
static int fn2([bool x = false, bool _, int _]) => 1;
static int fn3([bool? x, bool _ = false, int _]) => 1;
static int fn4([int _, bool? x, bool _ = false]) => 1;
}
int _([bool _]) => 1;
int topFoo2([bool x = false, bool _, int _]) => 1;
int topFoo3([bool? x, bool _ = false, int _]) => 1;
int topFoo4([int _, bool? x, bool _ = false]) => 1;
void main() {
Expect.equals(1, _());
Expect.equals(1, topFoo2());
Expect.equals(1, topFoo3());
Expect.equals(1, topFoo4());
int foo([int _]) => 1;
int foo2([bool x = false, bool _, int _]) => 1;
int foo3([bool? x, bool _ = false, int _]) => 1;
int foo4([int _, bool? x, bool _ = false]) => 1;
Expect.equals(1, foo());
Expect.equals(1, foo2());
Expect.equals(1, foo3());
Expect.equals(1, foo4());
var c = C();
Expect.equals(1, c.foo());
Expect.equals(1, c.foo2());
Expect.equals(1, c.foo3());
Expect.equals(1, c.foo4());
Expect.equals(1, C.otherParams().foo());
Expect.equals(1, C.otherParams2().foo());
Expect.equals(1, C.fn());
Expect.equals(1, C.fn2());
Expect.equals(1, C.fn3());
Expect.equals(1, C.fn4());
}