Skip to content

Commit 2246f0a

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Compute constant dependencies for constructor elements in canBeConst().
[email protected], [email protected] Change-Id: I92871e880e669aca5447f6fe7d7a8249421e7c44 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115613 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 981be87 commit 2246f0a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,16 @@ class ConstructorElementImpl extends ExecutableElementImpl
27862786
appendToWithName(buffer, name);
27872787
}
27882788

2789+
/// Ensures that dependencies of this constructor, such as default values
2790+
/// of formal parameters, are evaluated.
2791+
void computeConstantDependencies() {
2792+
if (!isConstantEvaluated) {
2793+
AnalysisOptionsImpl analysisOptions = context.analysisOptions;
2794+
computeConstants(context.typeProvider, context.typeSystem,
2795+
context.declaredVariables, [this], analysisOptions.experimentStatus);
2796+
}
2797+
}
2798+
27892799
@deprecated
27902800
@override
27912801
ConstructorDeclaration computeNode() =>

pkg/analyzer/lib/src/lint/linter.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import 'package:analyzer/file_system/file_system.dart' as file_system;
1515
import 'package:analyzer/src/dart/ast/ast.dart';
1616
import 'package:analyzer/src/dart/ast/token.dart';
1717
import 'package:analyzer/src/dart/constant/potentially_constant.dart';
18+
import 'package:analyzer/src/dart/element/element.dart';
1819
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
20+
import 'package:analyzer/src/dart/element/member.dart';
1921
import 'package:analyzer/src/dart/error/lint_codes.dart';
2022
import 'package:analyzer/src/generated/engine.dart'
2123
show AnalysisErrorInfo, AnalysisErrorInfoImpl, AnalysisOptions, Logger;
@@ -280,6 +282,14 @@ class LinterContextImpl implements LinterContext {
280282
if (element == null || !element.isConst) {
281283
return false;
282284
}
285+
286+
// Ensure that dependencies (e.g. default parameter values) are computed.
287+
var implElement = element;
288+
if (element is ConstructorMember) {
289+
implElement = element.baseElement;
290+
}
291+
(implElement as ConstructorElementImpl).computeConstantDependencies();
292+
283293
//
284294
// Verify that the evaluation of the constructor would not produce an
285295
// exception.

pkg/analyzer/test/src/lint/linter/linter_context_impl_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,20 @@ A f() => A([1, 2, 3]);
206206
''');
207207
assertCanBeConst("A([", true);
208208
}
209+
210+
void test_true_importedClass_defaultValue() async {
211+
var aPath = convertPath('/test/lib/a.dart');
212+
newFile(aPath, content: r'''
213+
class A {
214+
final int a;
215+
const A({int b = 1}) : a = b * 2;
216+
}
217+
''');
218+
await resolve('''
219+
import 'a.dart';
220+
221+
A f() => A();
222+
''');
223+
assertCanBeConst("A();", true);
224+
}
209225
}

0 commit comments

Comments
 (0)