Skip to content

Commit

Permalink
Reland [cfe] Account for compilation stage in invocations post-proces…
Browse files Browse the repository at this point in the history
…sing

Closes #55849
Closes #55755

This is a reland of https://dart-review.googlesource.com/c/sdk/+/369063

Change-Id: I9033935b3b352108c8c28b7ae09eaa9673b0a6fd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370560
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Chloe Stefantsova <[email protected]>
  • Loading branch information
chloestefantsova authored and Commit Queue committed Jun 11, 2024
1 parent fb3016a commit 55a1275
Show file tree
Hide file tree
Showing 50 changed files with 1,377 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,11 @@ class FormalParameterBuilder extends ModifierBuilderImpl
final DeclarationBuilder declarationBuilder =
parent!.parent as DeclarationBuilder;
Scope scope = declarationBuilder.scope;
BodyBuilderContext bodyBuilderContext =
new ParameterBodyBuilderContext(this);
BodyBuilderContext bodyBuilderContext = new ParameterBodyBuilderContext(
this,
inOutlineBuildingPhase: true,
inMetadata: false,
inConstFields: false);
BodyBuilder bodyBuilder = libraryBuilder.loader
.createBodyBuilderForOutlineExpression(
libraryBuilder, bodyBuilderContext, scope, fileUri);
Expand Down
50 changes: 46 additions & 4 deletions pkg/front_end/lib/src/fasta/kernel/body_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class BodyBuilder extends StackListenerImpl
return _defaultValueNestingLevel > 0;
}

/// True if the parser is between [beginMetadata] and [endMetadata].
bool _insideMetadataParsing = false;

/// Numeric nestedness of formal parameter default values.
///
/// The value of 0 means that the currently built part is not within a default
Expand Down Expand Up @@ -292,10 +295,34 @@ class BodyBuilder extends StackListenerImpl
/// values or inside the default values that aren't built as outline
/// expressions need to be added during the second pass.
bool get _createdStaticInvocationsNeedPostProcessing {
return _context.hasFormalParameters &&
!_insideOfFormalParameterDefaultValue ||
!_context.hasImmediateOutlineExpressionsBuilt ||
!_context.needsImmediateValuesBuiltAsOutlineExpressions;
return
// All invocations in outline building phase will be type-inferred, and
// they all should be added to the post-processing.
_context.inOutlineBuildingPhase ||

// Here we aren't in the outline mode, but rather in the
// body-building mode. If the current context has formal parameters
// and is a constructor context, their default values should be
// skipped because in the body-building mode they aren't passed
// through type inference.
(!_context.hasFormalParameters ||
!_insideOfFormalParameterDefaultValue ||
!_context.isConstructor) &&

// The invocations in the metadata should also be skipped in the
// body-building phase, since they aren't type-inferred. An
// exception here are the annotations within method bodies,
// field initializers, and on formal parameters.
!(_context.inMetadata ||
_insideMetadataParsing &&
!_inBody &&
!inFormals &&
!inFieldInitializer) &&

// Finally, the const fields in body-building phase aren't
// inferred and the invocations in them should be skipped during
// post-processing.
!_context.inConstFields;
}

bool get inFunctionType =>
Expand All @@ -315,6 +342,10 @@ class BodyBuilder extends StackListenerImpl

int functionNestingLevel = 0;

int _inBodyCount = 0;

bool get _inBody => _inBodyCount > 0;

Statement? problemInLoopOrSwitch;

Scope? switchScope;
Expand Down Expand Up @@ -475,6 +506,9 @@ class BodyBuilder extends StackListenerImpl
void enterLocalScope(Scope localScope) {
push(scope);
scope = localScope;
if (scope.kind == ScopeKind.functionBody) {
_inBodyCount++;
}
assert(checkState(null, [
ValueKinds.Scope,
]));
Expand All @@ -484,6 +518,9 @@ class BodyBuilder extends StackListenerImpl
{required String debugName, required ScopeKind kind}) {
push(scope);
scope = scope.createNestedScope(debugName: debugName, kind: kind);
if (kind == ScopeKind.functionBody) {
_inBodyCount++;
}
assert(checkState(null, [
ValueKinds.Scope,
]));
Expand All @@ -508,6 +545,9 @@ class BodyBuilder extends StackListenerImpl
declaredInCurrentGuard = null;
}
}
if (scope.kind == ScopeKind.functionBody) {
_inBodyCount--;
}
scope = pop() as Scope;
}

Expand Down Expand Up @@ -867,6 +907,7 @@ class BodyBuilder extends StackListenerImpl
super.push(constantContext);
constantContext = ConstantContext.inferred;
assert(checkState(token, [ValueKinds.ConstantContext]));
_insideMetadataParsing = true;
}

@override
Expand Down Expand Up @@ -941,6 +982,7 @@ class BodyBuilder extends StackListenerImpl
}
constantContext = savedConstantContext;
}
_insideMetadataParsing = false;
assert(checkState(beginToken, [ValueKinds.Expression]));
}

Expand Down
Loading

0 comments on commit 55a1275

Please sign in to comment.