[cfe] Don't treat named parameters as wildcards

Named parameters named '_' cannot be used neither as wildcards nor private named parameters. Such parameters were wrongfully treated as wildcards, triggering name mangling and consequently call site errors.

This change avoids handling these as wildcards.

Closes #63170

Change-Id: Ice594537e09892eec5033f64122aa665ad904773
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/495301
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Johnni Winther
2026-04-16 03:20:14 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 5b0def4a87
commit 225b342d71
12 changed files with 232 additions and 8 deletions
@@ -2241,7 +2241,9 @@ class FragmentFactoryImpl implements FragmentFactory {
modifiers |= Modifiers.SuperInitializingFormal;
}
bool isWildcard =
libraryFeatures.wildcardVariables.isEnabled && name == '_';
kind.isPositional &&
libraryFeatures.wildcardVariables.isEnabled &&
name == '_';
int? wildcardIndex;
if (isWildcard) {
wildcardIndex = wildcardVariableIndex++;
@@ -26,8 +26,8 @@ class C extends core::Object {
field core::int? __extraPrivate;
field core::int? _123;
field core::int? _for;
constructor onlyUnderscore({wildcard initializing-formal core::int? _#wc0#formal = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _#wc0#formal, super core::Object::•()
constructor onlyUnderscore({initializing-formal core::int? _ = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _, super core::Object::•()
;
constructor stillPrivate({initializing-formal core::int? __extraPrivate = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::_ = null, self::C::__extraPrivate = __extraPrivate, super core::Object::•()
@@ -26,8 +26,8 @@ class C extends core::Object {
field core::int? __extraPrivate;
field core::int? _123;
field core::int? _for;
constructor onlyUnderscore({wildcard initializing-formal core::int? _#wc0#formal = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _#wc0#formal, super core::Object::•()
constructor onlyUnderscore({initializing-formal core::int? _ = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _, super core::Object::•()
;
constructor stillPrivate({initializing-formal core::int? __extraPrivate = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::_ = null, self::C::__extraPrivate = __extraPrivate, super core::Object::•()
@@ -26,7 +26,7 @@ class C extends core::Object {
field core::int? __extraPrivate;
field core::int? _123;
field core::int? _for;
constructor onlyUnderscore({wildcard initializing-formal core::int? _#wc0#formal = null}) → self::C
constructor onlyUnderscore({initializing-formal core::int? _ = null}) → self::C
;
constructor stillPrivate({initializing-formal core::int? __extraPrivate = null}) → self::C
;
@@ -26,8 +26,8 @@ class C extends core::Object {
field core::int? __extraPrivate;
field core::int? _123;
field core::int? _for;
constructor onlyUnderscore({wildcard initializing-formal core::int? _#wc0#formal = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _#wc0#formal, super core::Object::•()
constructor onlyUnderscore({initializing-formal core::int? _ = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::__extraPrivate = null, self::C::_ = _, super core::Object::•()
;
constructor stillPrivate({initializing-formal core::int? __extraPrivate = #C1}) → self::C
: self::C::_for = null, self::C::_123 = null, self::C::_ = null, self::C::__extraPrivate = __extraPrivate, super core::Object::•()
@@ -0,0 +1,17 @@
// Copyright (c) 2026, 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.
void f1({int _ = 0}) {}
void f2({int _ = 0, String _ = ''}) {}
class C {
int _;
C({this._ = 0});
}
test() {
f1(_: 1);
f2(_: 2);
C(_: 3);
}
@@ -0,0 +1,48 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:5:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f1({int _ = 0}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: Duplicated parameter name '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Context: Other parameter named '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:10:11: Error: A private named parameter must have a corresponding public name.
// C({this._ = 0});
// ^
//
import self as self;
import "dart:core" as core;
class C extends core::Object {
field core::int _;
constructor •({initializing-formal core::int _ = #C1}) → self::C
: self::C::_ = _, super core::Object::•()
;
}
static method f1({core::int _ = #C1}) → void {}
static method f2({core::int _ = #C1, core::String _ = #C2}) → void {}
static method test() → dynamic {
self::f1(_: 1);
self::f2(_: 2);
new self::C::•(_: 3);
}
constants {
#C1 = 0
#C2 = ""
}
@@ -0,0 +1,48 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:5:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f1({int _ = 0}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: Duplicated parameter name '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Context: Other parameter named '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:10:11: Error: A private named parameter must have a corresponding public name.
// C({this._ = 0});
// ^
//
import self as self;
import "dart:core" as core;
class C extends core::Object {
field core::int _;
constructor •({initializing-formal core::int _ = #C1}) → self::C
: self::C::_ = _, super core::Object::•()
;
}
static method f1({core::int _ = #C1}) → void {}
static method f2({core::int _ = #C1, core::String _ = #C2}) → void {}
static method test() → dynamic {
self::f1(_: 1);
self::f2(_: 2);
new self::C::•(_: 3);
}
constants {
#C1 = 0
#C2 = ""
}
@@ -0,0 +1,41 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:5:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f1({int _ = 0}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: Duplicated parameter name '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Context: Other parameter named '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:10:11: Error: A private named parameter must have a corresponding public name.
// C({this._ = 0});
// ^
//
import self as self;
import "dart:core" as core;
class C extends core::Object {
field core::int _;
constructor •({initializing-formal core::int _ = 0}) → self::C
;
}
static method f1({has-declared-initializer core::int _}) → void
;
static method f2({has-declared-initializer core::int _, has-declared-initializer core::String _}) → void
;
static method test() → dynamic
;
@@ -0,0 +1,48 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:5:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f1({int _ = 0}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: A named parameter that doesn't refer to an instance variable can't start with an underscore ('_').
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:28: Error: Duplicated parameter name '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:6:14: Context: Other parameter named '_'.
// void f2({int _ = 0, String _ = ''}) {}
// ^
//
// pkg/front_end/testcases/private_named_parameters/wildcard_named_parameters.dart:10:11: Error: A private named parameter must have a corresponding public name.
// C({this._ = 0});
// ^
//
import self as self;
import "dart:core" as core;
class C extends core::Object {
field core::int _;
constructor •({initializing-formal core::int _ = #C1}) → self::C
: self::C::_ = _, super core::Object::•()
;
}
static method f1({core::int _ = #C1}) → void {}
static method f2({core::int _ = #C1, core::String _ = #C2}) → void {}
static method test() → dynamic {
self::f1(_: 1);
self::f2(_: 2);
new self::C::•(_: 3);
}
constants {
#C1 = 0
#C2 = ""
}
@@ -0,0 +1,10 @@
void f1({int _ = 0}) {}
void f2({int _ = 0, String _ = ''}) {}
class C {
int _;
C({this._ = 0});
}
test() {}
@@ -0,0 +1,10 @@
class C {
C({this._ = 0});
int _;
}
test() {}
void f1({int _ = 0}) {}
void f2({int _ = 0, String _ = ''}) {}