Skip to content

Commit 48cadec

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[cfe] Top level function wildcard parameters are non-binding.
This CL prevents function wildcard parameter collisions and avoids adding function wildcard parameters to scope. Bug: #55655 Change-Id: I5a8b85ceef15b355f6a36a1d1d19df038980331d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371941 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent ddce8d5 commit 48cadec

10 files changed

+130
-3
lines changed

pkg/front_end/lib/src/fasta/source/outline_builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,8 @@ class OutlineBuilder extends StackListenerImpl {
28272827
if (formals.length == 2) {
28282828
// The name may be null for generalized function types.
28292829
if (formals[0].name != FormalParameterBuilder.noNameSentinel &&
2830-
formals[0].name == formals[1].name) {
2830+
formals[0].name == formals[1].name &&
2831+
!formals[0].isWildcard) {
28312832
addProblem(
28322833
templateDuplicatedParameterName.withArguments(formals[1].name),
28332834
formals[1].charOffset,
@@ -2843,6 +2844,9 @@ class OutlineBuilder extends StackListenerImpl {
28432844
Map<String, FormalParameterBuilder> seenNames =
28442845
<String, FormalParameterBuilder>{};
28452846
for (FormalParameterBuilder formal in formals) {
2847+
if (formal.isWildcard) {
2848+
continue;
2849+
}
28462850
if (formal.name == FormalParameterBuilder.noNameSentinel) continue;
28472851
if (seenNames.containsKey(formal.name)) {
28482852
addProblem(

pkg/front_end/lib/src/fasta/source/source_function_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
208208
if (formals == null) return parent;
209209
Map<String, Builder> local = <String, Builder>{};
210210
for (FormalParameterBuilder formal in formals!) {
211+
if (formal.isWildcard) {
212+
continue;
213+
}
211214
if (!isConstructor ||
212215
!formal.isInitializingFormal && !formal.isSuperInitializingFormal) {
213216
local[formal.name] = formal;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
void fn(_, _) {
6+
print(_);
7+
}
8+
9+
void fn2(_, _, _) {
10+
print(_);
11+
}
12+
13+
test() {
14+
fn(1, 2);
15+
fn2(1, 2, 3);
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
6+
// print(_);
7+
// ^
8+
//
9+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
10+
// print(_);
11+
// ^
12+
//
13+
import self as self;
14+
import "dart:core" as core;
15+
16+
static method fn(wildcard dynamic _, wildcard dynamic _) → void {
17+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
18+
print(_);
19+
^");
20+
}
21+
static method fn2(wildcard dynamic _, wildcard dynamic _, wildcard dynamic _) → void {
22+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
23+
print(_);
24+
^");
25+
}
26+
static method test() → dynamic {
27+
self::fn(1, 2);
28+
self::fn2(1, 2, 3);
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
6+
// print(_);
7+
// ^
8+
//
9+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
10+
// print(_);
11+
// ^
12+
//
13+
import self as self;
14+
import "dart:core" as core;
15+
16+
static method fn(wildcard dynamic _, wildcard dynamic _) → void {
17+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
18+
print(_);
19+
^");
20+
}
21+
static method fn2(wildcard dynamic _, wildcard dynamic _, wildcard dynamic _) → void {
22+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
23+
print(_);
24+
^");
25+
}
26+
static method test() → dynamic {
27+
self::fn(1, 2);
28+
self::fn2(1, 2, 3);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
4+
static method fn(wildcard dynamic _, wildcard dynamic _) → void
5+
;
6+
static method fn2(wildcard dynamic _, wildcard dynamic _, wildcard dynamic _) → void
7+
;
8+
static method test() → dynamic
9+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
6+
// print(_);
7+
// ^
8+
//
9+
// pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
10+
// print(_);
11+
// ^
12+
//
13+
import self as self;
14+
import "dart:core" as core;
15+
16+
static method fn(wildcard dynamic _, wildcard dynamic _) → void {
17+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:6:9: Error: Undefined name '_'.
18+
print(_);
19+
^");
20+
}
21+
static method fn2(wildcard dynamic _, wildcard dynamic _, wildcard dynamic _) → void {
22+
core::print(invalid-expression "pkg/front_end/testcases/wildcard_variables/top_level_function_no_collision.dart:10:9: Error: Undefined name '_'.
23+
print(_);
24+
^");
25+
}
26+
static method test() → dynamic {
27+
self::fn(1, 2);
28+
self::fn2(1, 2, 3);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void fn(_, _) {}
2+
3+
void fn2(_, _, _) {}
4+
5+
test() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test() {}
2+
3+
void fn(_, _) {}
4+
5+
void fn2(_, _, _) {}

tests/language/wildcard_variables/unnamed_optional/unnamed_optional_error_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class SubClass extends SuperClass {
2424
// ^
2525
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
2626
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
27-
// [cfe] Duplicated parameter name '_'.
2827
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
2928
// [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.
3029
]);
@@ -41,7 +40,6 @@ class TypedSubClass extends SuperClass {
4140
// ^
4241
// [analyzer] COMPILE_TIME_ERROR.DUPLICATE_DEFINITION
4342
// [analyzer] COMPILE_TIME_ERROR.MISSING_DEFAULT_VALUE_FOR_PARAMETER
44-
// [cfe] Duplicated parameter name '_'.
4543
// [cfe] The parameter '_' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
4644
// [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.
4745
]) : super.nullable();

0 commit comments

Comments
 (0)