Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 506ac7f

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Rework linker type inference using ExprBuilder.
This CL changes the way linker type inference works. Instead of walking the operations in an UnlinkedExpr in order and inferring types directly, it first builds an AST for the expression using ExprBuilder, then uses ResolverVisitor to resolve the AST and perform type inference. This helps ensure that summary type inference and ordinary AST-based type inference produce the same result, since they now use the same core algorithm. In particular, this means that summary type inference now does downwards inference properly. Since the ResolverVisitor makes greater use of the element model than the rest of the linker, this required implementing additional methods in the linker's element model. This CL includes some minor fixes to ExprBuilder that were uncovered during testing. It also contains some minor changes to ResolverVisitor to make it work during linking. Fixes #32525. Fixes #32394. Change-Id: I5ec9b2bf5565ad30b8cc856475334323dc118da8 Reviewed-on: https://dart-review.googlesource.com/48741 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
1 parent 494b861 commit 506ac7f

File tree

9 files changed

+683
-896
lines changed

9 files changed

+683
-896
lines changed

pkg/analysis_server/test/services/correction/fix_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7043,15 +7043,16 @@ set speed2(int ms) {}
70437043
}
70447044

70457045
test_removeTypeAnnotation_avoidTypesOnClosureParameters_FunctionTypedFormalParameter() async {
7046+
// Note: explicit type `Function` to work around dartbug.com/32708.
70467047
String src = '''
7047-
var functionWithFunction = (/*LINT*/int f(int x)) => f(0);
7048+
Function functionWithFunction = (/*LINT*/int f(int x)) => f(0);
70487049
''';
70497050
await findLint(src, LintNames.avoid_types_on_closure_parameters);
70507051

70517052
await applyFix(DartFixKind.REPLACE_WITH_IDENTIFIER);
70527053

70537054
verifyResult('''
7054-
var functionWithFunction = (f) => f(0);
7055+
Function functionWithFunction = (f) => f(0);
70557056
''');
70567057
}
70577058

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4999,7 +4999,8 @@ class ResolverVisitor extends ScopedVisitor {
49994999
}
50005000
this.inferenceContext = new InferenceContext._(
50015001
typeProvider, typeSystem, strongModeHints, errorReporter);
5002-
this.typeAnalyzer = new StaticTypeAnalyzer(this);
5002+
this.typeAnalyzer =
5003+
new StaticTypeAnalyzer(this, propagateTypes: propagateTypes);
50035004
}
50045005

50055006
/**
@@ -5723,7 +5724,8 @@ class ResolverVisitor extends ScopedVisitor {
57235724
}
57245725
// Clone the ASTs for default formal parameters, so that we can use them
57255726
// during constant evaluation.
5726-
if (!_hasSerializedConstantInitializer(element)) {
5727+
if (element is ConstVariableElement &&
5728+
!_hasSerializedConstantInitializer(element)) {
57275729
(element as ConstVariableElement).constantInitializer =
57285730
_createCloner().cloneNode(node.defaultValue);
57295731
}

pkg/analyzer/lib/src/summary/expr_builder.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ class ExprBuilder {
432432
simpleParam.identifier.staticElement = param;
433433
simpleParam.element = param;
434434
var unlinkedParam = param.unlinkedParam;
435-
FormalParameter paramAst;
436435
if (unlinkedParam.kind == UnlinkedParamKind.positional) {
437436
return AstTestFactory.positionalFormalParameter(simpleParam, null);
438437
} else if (unlinkedParam.kind == UnlinkedParamKind.named) {
@@ -570,13 +569,17 @@ class ExprBuilder {
570569
ReferenceInfo info = resynthesizer.getReferenceInfo(ref.reference);
571570
Expression node = _buildIdentifierSequence(info);
572571
TypeArgumentList typeArguments = _buildTypeArguments();
572+
var period = TokenFactory.tokenFromType(TokenType.PERIOD);
573+
var argumentList = AstTestFactory.argumentList(arguments);
573574
if (node is SimpleIdentifier) {
574575
_push(astFactory.methodInvocation(
575-
null,
576-
TokenFactory.tokenFromType(TokenType.PERIOD),
577-
node,
578-
typeArguments,
579-
AstTestFactory.argumentList(arguments)));
576+
null, period, node, typeArguments, argumentList));
577+
} else if (node is PropertyAccess) {
578+
_push(astFactory.methodInvocation(
579+
node.target, period, node.propertyName, typeArguments, argumentList));
580+
} else if (node is PrefixedIdentifier) {
581+
_push(astFactory.methodInvocation(
582+
node.prefix, period, node.identifier, typeArguments, argumentList));
580583
} else {
581584
throw new UnimplementedError('For ${node?.runtimeType}: $node');
582585
}

0 commit comments

Comments
 (0)