Skip to content

Commit 78b2708

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthand: Update constant verification for default parameter values.
The constant visitor was missing an extra check that we had for instance creation expressions, but not for dot shorthand constructor invocations. It's definitely a little funky because of how we've chained up the ConstantVisitor + ConstantVerifier, but this is how we currently handle this error. Added a unit test. Fixes: #60962 Bug: #59835 Change-Id: I76489a425c5e01c4d9edb34ac6dc62f109e6a32a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436821 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent c7765a8 commit 78b2708

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,14 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
896896
Constant visitDotShorthandConstructorInvocation(
897897
covariant DotShorthandConstructorInvocationImpl node,
898898
) {
899+
// This check is used by the [ConstantVerifier] to check for constant
900+
// default parameters and other instances where the invocation must be
901+
// constant.
902+
if (!node.isConst) {
903+
// TODO(kallentu): Use a specific error code.
904+
// https://github.com/dart-lang/sdk/issues/47061
905+
return InvalidConstant.genericError(node: node);
906+
}
899907
var constructor = node.constructorName.element;
900908
if (constructor is ConstructorElementMixin2) {
901909
return _evaluationEngine.evaluateAndFormatErrorsInConstructorCall(

pkg/analyzer/test/src/diagnostics/non_constant_default_value_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ class A {
3939
);
4040
}
4141

42+
test_dotShorthand_issue60962() async {
43+
await assertErrorsInCode(
44+
r'''
45+
class A {
46+
const A();
47+
}
48+
49+
void f([A a = .new()]) {}
50+
''',
51+
[error(CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE, 40, 6)],
52+
);
53+
}
54+
4255
test_enum_issue49097() async {
4356
newFile('$testPackageLibPath/a.dart', r'''
4457
class A {

0 commit comments

Comments
 (0)