Skip to content

Commit 6fc4854

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
Dmitry Stefantsov
authored andcommitted
[fasta] Run instantiate-to-bound on functions
Change-Id: Icff76f2e8f309b6d96059839f05b3ec4cc61510d Reviewed-on: https://dart-review.googlesource.com/50945 Reviewed-by: Peter von der Ahé <[email protected]>
1 parent 23b4a50 commit 6fc4854

File tree

5 files changed

+74
-43
lines changed

5 files changed

+74
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ abstract class LibraryBuilder<T extends TypeBuilder, R>
176176
/// where they were omitted by the programmer and not provided by the type
177177
/// inference. The method returns the number of distinct type variables
178178
/// that were instantiated in this library.
179-
int instantiateToBound(TypeBuilder dynamicType, TypeBuilder bottomType,
179+
int computeDefaultTypes(TypeBuilder dynamicType, TypeBuilder bottomType,
180180
ClassBuilder objectClass) {
181181
return 0;
182182
}

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

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ import 'kernel_ast_api.dart' as kernel show Expression, Statement;
122122

123123
import 'kernel_builder.dart';
124124

125+
import 'type_algorithms.dart' show calculateBounds;
126+
125127
// TODO(ahe): Remove this and ensure all nodes have a location.
126128
const noLocation = null;
127129

@@ -3641,9 +3643,25 @@ abstract class BodyBuilder<Expression, Statement, Arguments>
36413643
void beginTypeVariables(Token token) {
36423644
debugEvent("beginTypeVariables");
36433645
OutlineBuilder listener = new OutlineBuilder(library);
3646+
// TODO(dmitryas): [ClassMemberParser] shouldn't be used to parse and build
3647+
// the type variables for the local function. It also causes the unresolved
3648+
// types from the bounds of the type variables to appear in [library.types].
3649+
// See the code that resolves them below.
36443650
new ClassMemberParser(listener)
36453651
.parseTypeVariablesOpt(new Token.eof(-1)..next = token);
36463652
enterFunctionTypeScope(listener.pop());
3653+
3654+
// The invocation of [enterFunctionTypeScope] above has put the type
3655+
// variables into the scope, and now the possibly unresolved types from
3656+
// the bounds of the variables can be resolved. This is needed to apply
3657+
// instantiate-to-bound later.
3658+
// TODO(dmitryas): Move the resolution to the appropriate place once
3659+
// [ClassMemberParser] is not used to build the type variables for the local
3660+
// function. See the comment above.
3661+
for (UnresolvedType t in library.types) {
3662+
t.resolveIn(scope);
3663+
}
3664+
library.types.clear();
36473665
}
36483666

36493667
@override
@@ -3676,15 +3694,40 @@ abstract class BodyBuilder<Expression, Statement, Arguments>
36763694
variable.parameter.addAnnotation(toKernelExpression(annotation));
36773695
}
36783696
}
3679-
push(variable
3680-
..finish(library, library.loader.target.objectClassBuilder,
3681-
library.loader.target.dynamicType));
3697+
push(variable);
36823698
}
36833699

36843700
@override
36853701
void endTypeVariables(int count, Token beginToken, Token endToken) {
36863702
debugEvent("TypeVariables");
3687-
push(popList(count) ?? NullValue.TypeVariables);
3703+
List<KernelTypeVariableBuilder> typeVariables = popList(count);
3704+
if (typeVariables != null) {
3705+
if (library.loader.target.strongMode) {
3706+
List<KernelTypeBuilder> calculatedBounds = calculateBounds(
3707+
typeVariables,
3708+
library.loader.target.dynamicType,
3709+
library.loader.target.bottomType,
3710+
library.loader.target.objectClassBuilder);
3711+
for (int i = 0; i < typeVariables.length; ++i) {
3712+
typeVariables[i].defaultType = calculatedBounds[i];
3713+
typeVariables[i].defaultType.resolveIn(
3714+
scope, typeVariables[i].charOffset, typeVariables[i].fileUri);
3715+
typeVariables[i].finish(
3716+
library,
3717+
library.loader.target.objectClassBuilder,
3718+
library.loader.target.dynamicType);
3719+
}
3720+
} else {
3721+
for (int i = 0; i < typeVariables.length; ++i) {
3722+
typeVariables[i].defaultType = library.loader.target.dynamicType;
3723+
typeVariables[i].finish(
3724+
library,
3725+
library.loader.target.objectClassBuilder,
3726+
library.loader.target.dynamicType);
3727+
}
3728+
}
3729+
}
3730+
push(typeVariables ?? NullValue.TypeVariables);
36883731
}
36893732

36903733
List<TypeParameter> typeVariableBuildersToKernel(List typeVariableBuilders) {

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -990,47 +990,35 @@ class KernelLibraryBuilder
990990
return count;
991991
}
992992

993-
int instantiateToBound(TypeBuilder dynamicType, TypeBuilder bottomType,
993+
int computeDefaultTypes(TypeBuilder dynamicType, TypeBuilder bottomType,
994994
ClassBuilder objectClass) {
995995
int count = 0;
996+
bool strongMode = loader.target.strongMode;
997+
998+
int computeDefaultTypesForVariables(List<TypeVariableBuilder> variables) {
999+
if (variables == null) return 0;
1000+
List<KernelTypeBuilder> calculatedBounds = strongMode
1001+
? calculateBounds(variables, dynamicType, bottomType, objectClass)
1002+
: null;
1003+
for (int i = 0; i < variables.length; ++i) {
1004+
variables[i].defaultType =
1005+
strongMode ? calculatedBounds[i] : dynamicType;
1006+
}
1007+
return variables.length;
1008+
}
9961009

9971010
for (var declaration in libraryDeclaration.members.values) {
9981011
if (declaration is KernelClassBuilder) {
999-
if (declaration.typeVariables != null) {
1000-
List<KernelTypeBuilder> calculatedBounds;
1001-
if (loader.target.strongMode) {
1002-
calculatedBounds = calculateBounds(declaration.typeVariables,
1003-
dynamicType, bottomType, objectClass);
1004-
} else {
1005-
calculatedBounds =
1006-
new List<KernelTypeBuilder>(declaration.typeVariables.length);
1007-
for (int i = 0; i < calculatedBounds.length; ++i) {
1008-
calculatedBounds[i] = dynamicType;
1009-
}
1012+
count += computeDefaultTypesForVariables(declaration.typeVariables);
1013+
declaration.forEach((String name, Builder member) {
1014+
if (member is KernelProcedureBuilder) {
1015+
count += computeDefaultTypesForVariables(member.typeVariables);
10101016
}
1011-
for (int i = 0; i < calculatedBounds.length; ++i) {
1012-
declaration.typeVariables[i].defaultType = calculatedBounds[i];
1013-
}
1014-
count += calculatedBounds.length;
1015-
}
1017+
});
10161018
} else if (declaration is KernelFunctionTypeAliasBuilder) {
1017-
if (declaration.typeVariables != null) {
1018-
List<KernelTypeBuilder> calculatedBounds;
1019-
if (loader.target.strongMode) {
1020-
calculatedBounds = calculateBounds(declaration.typeVariables,
1021-
dynamicType, bottomType, objectClass);
1022-
} else {
1023-
calculatedBounds =
1024-
new List<KernelTypeBuilder>(declaration.typeVariables.length);
1025-
for (int i = 0; i < calculatedBounds.length; ++i) {
1026-
calculatedBounds[i] = dynamicType;
1027-
}
1028-
}
1029-
for (int i = 0; i < calculatedBounds.length; ++i) {
1030-
declaration.typeVariables[i].defaultType = calculatedBounds[i];
1031-
}
1032-
count += calculatedBounds.length;
1033-
}
1019+
count += computeDefaultTypesForVariables(declaration.typeVariables);
1020+
} else if (declaration is KernelFunctionBuilder) {
1021+
count += computeDefaultTypesForVariables(declaration.typeVariables);
10341022
}
10351023
}
10361024

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class KernelTarget extends TargetImplementation {
242242
objectType.bind(loader.coreLibrary["Object"]);
243243
bottomType.bind(loader.coreLibrary["Null"]);
244244
loader.resolveTypes();
245-
loader.instantiateToBound(dynamicType, bottomType, objectClassBuilder);
245+
loader.computeDefaultTypes(dynamicType, bottomType, objectClassBuilder);
246246
List<SourceClassBuilder> myClasses = collectMyClasses();
247247
loader.checkSemantics(myClasses);
248248
loader.finishTypeVariables(objectClassBuilder, dynamicType);

pkg/front_end/lib/src/fasta/source/source_loader.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,16 @@ class SourceLoader<L> extends Loader<L> {
396396
ticker.logMs("Resolved $count type-variable bounds");
397397
}
398398

399-
void instantiateToBound(TypeBuilder dynamicType, TypeBuilder bottomType,
399+
void computeDefaultTypes(TypeBuilder dynamicType, TypeBuilder bottomType,
400400
ClassBuilder objectClass) {
401401
int count = 0;
402402
builders.forEach((Uri uri, LibraryBuilder library) {
403403
if (library.loader == this) {
404404
count +=
405-
library.instantiateToBound(dynamicType, bottomType, objectClass);
405+
library.computeDefaultTypes(dynamicType, bottomType, objectClass);
406406
}
407407
});
408-
ticker.logMs("Instantiated $count type variables to their bounds");
408+
ticker.logMs("Computed default types for $count type variables");
409409
}
410410

411411
void finishNativeMethods() {

0 commit comments

Comments
 (0)