Skip to content

Commit

Permalink
[cfe] Move formal inference to SourceFunctionBuilderImpl hierarchy
Browse files Browse the repository at this point in the history
This is a follow-up to
https://dart-review.googlesource.com/c/sdk/+/404822

Change-Id: Id0b41562de44a89515b65c80f082ba16c823b09b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405006
Commit-Queue: Chloe Stefantsova <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
  • Loading branch information
chloestefantsova authored and Commit Queue committed Jan 20, 2025
1 parent 7198fb9 commit 655cdcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
3 changes: 3 additions & 0 deletions pkg/front_end/lib/src/source/source_constructor_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ class DeclaredSourceConstructorBuilder
void buildOutlineExpressions(ClassHierarchy classHierarchy,
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
if (_hasBuiltOutlines) return;

inferFormals(formals, classHierarchy);

if (isConst && isAugmenting) {
origin.buildOutlineExpressions(
classHierarchy, delayedDefaultValueCloners);
Expand Down
32 changes: 6 additions & 26 deletions pkg/front_end/lib/src/source/source_factory_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
import '../builder/function_builder.dart';
import '../builder/metadata_builder.dart';
import '../builder/omitted_type_builder.dart';
import '../builder/type_builder.dart';
import '../codes/cfe_codes.dart';
import '../dill/dill_extension_type_member_builder.dart';
Expand Down Expand Up @@ -263,6 +262,9 @@ class SourceFactoryBuilder extends SourceFunctionBuilderImpl {
void buildOutlineExpressions(ClassHierarchy classHierarchy,
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
if (_hasBuiltOutlines) return;

inferFormals(formals, classHierarchy);

if (_delayedDefaultValueCloner != null) {
delayedDefaultValueCloners.add(_delayedDefaultValueCloner!);
}
Expand Down Expand Up @@ -560,6 +562,9 @@ class RedirectingFactoryBuilder extends SourceFactoryBuilder {
void buildOutlineExpressions(ClassHierarchy classHierarchy,
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
if (_hasBuiltOutlines) return;

inferFormals(formals, classHierarchy);

if (isConst && isAugmenting) {
origin.buildOutlineExpressions(
classHierarchy, delayedDefaultValueCloners);
Expand All @@ -583,31 +588,6 @@ class RedirectingFactoryBuilder extends SourceFactoryBuilder {
createBodyBuilderContext(), declarationBuilder.scope, fileUri);
Builder? targetBuilder = redirectionTarget.target;

// Inference of target's formals should happen before building of the
// outline expressions in members and before inferring target's type
// arguments.
//
// (1) The outline expressions, such as formal parameter initializers,
// need properly inferred type contexts. Among other things, it ensures
// that the required coercions, such as int-to-double conversion, are
// run.
//
// (2) Type arguments for the targets of redirecting factories can only
// be inferred if the formal parameters of the targets are inferred too.
// That may not be the case when the target's parameters are initializing
// parameters referring to fields with types that are to be inferred.
if (targetBuilder is SourceFunctionBuilderImpl) {
List<FormalParameterBuilder>? formals = targetBuilder.formals;
if (formals != null) {
for (FormalParameterBuilder formal in formals) {
TypeBuilder formalType = formal.type;
if (formalType is InferableTypeBuilder) {
formalType.inferType(classHierarchy);
}
}
}
}

if (targetBuilder is SourceMemberBuilder) {
// Ensure that target has been built.
targetBuilder.buildOutlineExpressions(
Expand Down
15 changes: 15 additions & 0 deletions pkg/front_end/lib/src/source/source_function_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
void buildOutlineExpressions(ClassHierarchy classHierarchy,
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
if (!hasBuiltOutlineExpressions) {
inferFormals(formals, classHierarchy);

DeclarationBuilder? classOrExtensionBuilder =
isClassMember || isExtensionMember || isExtensionTypeMember
? parent as DeclarationBuilder
Expand Down Expand Up @@ -572,3 +574,16 @@ void reportAugmentationMismatch(
origin.fileUri!, origin.fileOffset, noLength)
]);
}

/// Ensures the type of each of the [formals] is inferred.
void inferFormals(
List<FormalParameterBuilder>? formals, ClassHierarchy classHierarchy) {
if (formals != null) {
for (FormalParameterBuilder formal in formals) {
TypeBuilder formalType = formal.type;
if (formalType is InferableTypeBuilder) {
formalType.inferType(classHierarchy);
}
}
}
}

0 comments on commit 655cdcb

Please sign in to comment.