Skip to content

Commit 007485f

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthands: Issue 61209 - Constant parameter mismatch error fix.
In the specific case where a dot shorthand is referencing a const constructor invocation of a imported class and the constructor has an optional parameter with a default value, the analyzer currently fails to compute the the constant result from that default value. With the changes in this CL, we're properly calculating the dependencies we need to do that computation and prevent the `const_constructor_param_type_mismatch` error. We're missing a couple of visitors for the `DotShorthandConstructorInvocation` AST that tell the constant evaluator to evaluate its dependencies. This should work exactly like visiting `InstanceCreationExpression`s, in the same class. Fixes: #61209 Bug: #59835 Change-Id: I6544e11e19f814b62108a736d1c778f8c4eda0cc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446181 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 2e8c2a5 commit 007485f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

pkg/analyzer/lib/src/dart/constant/utilities.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class ConstantExpressionsDependenciesFinder extends RecursiveAstVisitor {
2626
_find(node.expression);
2727
}
2828

29+
@override
30+
void visitDotShorthandConstructorInvocation(
31+
DotShorthandConstructorInvocation node,
32+
) {
33+
if (node.isConst) {
34+
_find(node);
35+
} else {
36+
super.visitDotShorthandConstructorInvocation(node);
37+
}
38+
}
39+
2940
@override
3041
void visitInstanceCreationExpression(InstanceCreationExpression node) {
3142
if (node.isConst) {
@@ -221,6 +232,19 @@ class ReferenceFinder extends RecursiveAstVisitor<void> {
221232
/// graph. The [_callback] will be invoked for every dependency found.
222233
ReferenceFinder(this._callback);
223234

235+
@override
236+
void visitDotShorthandConstructorInvocation(
237+
covariant DotShorthandConstructorInvocationImpl node,
238+
) {
239+
if (node.isConst) {
240+
var constructor = node.constructorName.element?.baseElement;
241+
if (constructor is ConstructorElementImpl && constructor.isConst) {
242+
_callback(constructor);
243+
}
244+
}
245+
super.visitDotShorthandConstructorInvocation(node);
246+
}
247+
224248
@override
225249
void visitInstanceCreationExpression(
226250
covariant InstanceCreationExpressionImpl node,

pkg/analyzer/test/src/dart/constant/evaluation_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,68 @@ A
64366436
''');
64376437
}
64386438

6439+
test_dotShorthand_constructor_import_namedParameter() async {
6440+
newFile('$testPackageLibPath/a.dart', '''
6441+
class C {
6442+
final int one;
6443+
const C({this.one = 1});
6444+
}
6445+
''');
6446+
await assertNoErrorsInCode('''
6447+
import 'a.dart';
6448+
const C c = .new();
6449+
''');
6450+
6451+
var result = _topLevelVar('c');
6452+
assertDartObjectText(result, '''
6453+
C
6454+
one: int 1
6455+
constructorInvocation
6456+
constructor: package:test/a.dart::@class::C::@constructor::new
6457+
variable: <testLibrary>::@topLevelVariable::c
6458+
''');
6459+
}
6460+
6461+
test_dotShorthand_constructor_import_namedParameter_positional() async {
6462+
newFile('$testPackageLibPath/a.dart', '''
6463+
class C {
6464+
final int one;
6465+
const C(int x, {this.one = 1});
6466+
}
6467+
''');
6468+
await assertNoErrorsInCode('''
6469+
import 'a.dart';
6470+
const C c = .new(1);
6471+
''');
6472+
6473+
var result = _topLevelVar('c');
6474+
assertDartObjectText(result, '''
6475+
C
6476+
one: int 1
6477+
constructorInvocation
6478+
constructor: package:test/a.dart::@class::C::@constructor::new
6479+
positionalArguments
6480+
0: int 1
6481+
variable: <testLibrary>::@topLevelVariable::c
6482+
''');
6483+
}
6484+
6485+
test_dotShorthand_constructor_import_namedParameter_required() async {
6486+
newFile('$testPackageLibPath/a.dart', '''
6487+
class C {
6488+
final int one;
6489+
const C({required this.one});
6490+
}
6491+
''');
6492+
await assertErrorsInCode(
6493+
'''
6494+
import 'a.dart';
6495+
const C c = .new();
6496+
''',
6497+
[error(CompileTimeErrorCode.missingRequiredArgument, 30, 3)],
6498+
);
6499+
}
6500+
64396501
test_dotShorthand_nonConstantArgument_issue60963() async {
64406502
await assertErrorsInCode(
64416503
'''

0 commit comments

Comments
 (0)