Skip to content

Commit d31d0f9

Browse files
chloestefantsovaCommit Queue
authored and
Commit Queue
committed
[cfe] Account for compilation stage in invocations post-processing
Closes #55849 Closes #55755 Change-Id: I471830cafed76ec5c503fe726ade857c007ed712 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369063 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent c5cd8ff commit d31d0f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1003
-288
lines changed

pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,11 @@ class FormalParameterBuilder extends ModifierBuilderImpl
273273
final DeclarationBuilder declarationBuilder =
274274
parent!.parent as DeclarationBuilder;
275275
Scope scope = declarationBuilder.scope;
276-
BodyBuilderContext bodyBuilderContext =
277-
new ParameterBodyBuilderContext(this);
276+
BodyBuilderContext bodyBuilderContext = new ParameterBodyBuilderContext(
277+
this,
278+
inOutlineBuildingPhase: true,
279+
inMetadata: false,
280+
inConstFields: false);
278281
BodyBuilder bodyBuilder = libraryBuilder.loader
279282
.createBodyBuilderForOutlineExpression(
280283
libraryBuilder, bodyBuilderContext, scope, fileUri);

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ class BodyBuilder extends StackListenerImpl
252252
return _defaultValueNestingLevel > 0;
253253
}
254254

255+
/// True if the parser is between [beginMetadata] and [endMetadata].
256+
bool _insideMetadataParsing = false;
257+
255258
/// Numeric nestedness of formal parameter default values.
256259
///
257260
/// The value of 0 means that the currently built part is not within a default
@@ -292,10 +295,36 @@ class BodyBuilder extends StackListenerImpl
292295
/// values or inside the default values that aren't built as outline
293296
/// expressions need to be added during the second pass.
294297
bool get _createdStaticInvocationsNeedPostProcessing {
295-
return _context.hasFormalParameters &&
296-
!_insideOfFormalParameterDefaultValue ||
297-
!_context.hasImmediateOutlineExpressionsBuilt ||
298-
!_context.needsImmediateValuesBuiltAsOutlineExpressions;
298+
return
299+
// All invocations in outline building phase will be type-inferred, and
300+
// they all should be added to the post-processing.
301+
_context.inOutlineBuildingPhase ||
302+
303+
// Here we aren't in the outline mode, but rather in the
304+
// body-building mode. If the current context has formal parameters,
305+
// their default values should be skipped because in the
306+
// body-building mode they aren't passed through type inference. An
307+
// exception here is the default values of instance methods: they
308+
// are actually inferred in body-building phase, and they aren't
309+
// built at all during the outline phase.
310+
(!_context.hasFormalParameters ||
311+
!_insideOfFormalParameterDefaultValue ||
312+
!isDeclarationInstanceContext) &&
313+
314+
// The invocations in the metadata should also be skipped in the
315+
// body-building phase, since they aren't type-inferred. An
316+
// exception here are the annotations within method bodies,
317+
// field initializers, and on formal parameters.
318+
!(_context.inMetadata ||
319+
_insideMetadataParsing &&
320+
!_inBody &&
321+
!inFormals &&
322+
!inFieldInitializer) &&
323+
324+
// Finally, the const fields in body-building phase aren't
325+
// inferred and the invocations in them should be skipped during
326+
// post-processing.
327+
!_context.inConstFields;
299328
}
300329

301330
bool get inFunctionType =>
@@ -315,6 +344,10 @@ class BodyBuilder extends StackListenerImpl
315344

316345
int functionNestingLevel = 0;
317346

347+
int _inBodyCount = 0;
348+
349+
bool get _inBody => _inBodyCount > 0;
350+
318351
Statement? problemInLoopOrSwitch;
319352

320353
Scope? switchScope;
@@ -475,6 +508,9 @@ class BodyBuilder extends StackListenerImpl
475508
void enterLocalScope(Scope localScope) {
476509
push(scope);
477510
scope = localScope;
511+
if (scope.kind == ScopeKind.functionBody) {
512+
_inBodyCount++;
513+
}
478514
assert(checkState(null, [
479515
ValueKinds.Scope,
480516
]));
@@ -484,6 +520,9 @@ class BodyBuilder extends StackListenerImpl
484520
{required String debugName, required ScopeKind kind}) {
485521
push(scope);
486522
scope = scope.createNestedScope(debugName: debugName, kind: kind);
523+
if (kind == ScopeKind.functionBody) {
524+
_inBodyCount++;
525+
}
487526
assert(checkState(null, [
488527
ValueKinds.Scope,
489528
]));
@@ -508,6 +547,9 @@ class BodyBuilder extends StackListenerImpl
508547
declaredInCurrentGuard = null;
509548
}
510549
}
550+
if (scope.kind == ScopeKind.functionBody) {
551+
_inBodyCount--;
552+
}
511553
scope = pop() as Scope;
512554
}
513555

@@ -867,6 +909,7 @@ class BodyBuilder extends StackListenerImpl
867909
super.push(constantContext);
868910
constantContext = ConstantContext.inferred;
869911
assert(checkState(token, [ValueKinds.ConstantContext]));
912+
_insideMetadataParsing = true;
870913
}
871914

872915
@override
@@ -941,6 +984,7 @@ class BodyBuilder extends StackListenerImpl
941984
}
942985
constantContext = savedConstantContext;
943986
}
987+
_insideMetadataParsing = false;
944988
assert(checkState(beginToken, [ValueKinds.Expression]));
945989
}
946990

0 commit comments

Comments
 (0)