Skip to content

Commit 47a9ebe

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[cfe] Make API of as-instance-of and legacy LUB/GLB library-specific
Change-Id: I1991c66e654ef537243d9f25f17070acbfa13af9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126380 Commit-Queue: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent ae14121 commit 47a9ebe

File tree

109 files changed

+869
-538
lines changed

Some content is hidden

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

109 files changed

+869
-538
lines changed

pkg/compiler/lib/src/ir/static_type.dart

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
5454
final ir.ClassHierarchy hierarchy;
5555

5656
ThisInterfaceType _thisType;
57+
ir.Library _currentLibrary;
5758

5859
StaticTypeVisitor(ir.TypeEnvironment typeEnvironment, this.hierarchy)
5960
: super(typeEnvironment);
@@ -83,6 +84,16 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
8384
_thisType = value;
8485
}
8586

87+
ir.Library get currentLibrary {
88+
assert(_currentLibrary != null);
89+
return _currentLibrary;
90+
}
91+
92+
void set currentLibrary(ir.Library value) {
93+
assert(value == null || _currentLibrary == null);
94+
_currentLibrary = value;
95+
}
96+
8697
bool completes(ir.DartType type) => type != const DoesNotCompleteType();
8798

8899
Set<ir.VariableDeclaration> _currentVariables;
@@ -173,7 +184,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
173184
/// If this is not the case the raw type of [superclass] is returned.
174185
///
175186
/// This method is derived from `ir.Expression.getStaticTypeAsInstanceOf`.
176-
ir.InterfaceType getTypeAsInstanceOf(ir.DartType type, ir.Class superclass) {
187+
ir.InterfaceType getTypeAsInstanceOf(
188+
ir.DartType type, ir.Class superclass, ir.Library clientLibrary) {
177189
// This method assumes the program is correctly typed, so if the superclass
178190
// is not generic, we can just return its raw type without computing the
179191
// type of this expression. It also ensures that all types are considered
@@ -186,14 +198,16 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
186198
type = (type as ir.TypeParameterType).parameter.bound;
187199
}
188200
if (type == typeEnvironment.nullType) {
189-
return superclass.bottomType;
201+
return typeEnvironment.coreTypes
202+
.bottomInterfaceType(superclass, ir.Nullability.legacy);
190203
}
191204
if (type is ir.InterfaceType) {
192-
ir.InterfaceType upcastType =
193-
typeEnvironment.getTypeAsInstanceOf(type, superclass);
205+
ir.InterfaceType upcastType = typeEnvironment.getTypeAsInstanceOf(
206+
type, superclass, clientLibrary, typeEnvironment.coreTypes);
194207
if (upcastType != null) return upcastType;
195208
} else if (type is ir.BottomType) {
196-
return superclass.bottomType;
209+
return typeEnvironment.coreTypes
210+
.bottomInterfaceType(superclass, ir.Nullability.legacy);
197211
}
198212
// TODO(johnniwinther): Should we assert that this doesn't happen?
199213
return typeEnvironment.coreTypes.legacyRawType(superclass);
@@ -213,7 +227,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
213227
}
214228
if (interfaceTarget != null) {
215229
ir.Class superclass = interfaceTarget.enclosingClass;
216-
receiverType = getTypeAsInstanceOf(receiverType, superclass);
230+
receiverType =
231+
getTypeAsInstanceOf(receiverType, superclass, currentLibrary);
217232
return ir.Substitution.fromInterfaceType(receiverType)
218233
.substituteType(interfaceTarget.getterType);
219234
}
@@ -283,7 +298,7 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
283298
ir.Class superclass = interfaceTarget.enclosingClass;
284299
ir.Substitution receiverSubstitution =
285300
ir.Substitution.fromInterfaceType(
286-
getTypeAsInstanceOf(receiverType, superclass));
301+
getTypeAsInstanceOf(receiverType, superclass, currentLibrary));
287302
ir.DartType setterType =
288303
receiverSubstitution.substituteType(interfaceTarget.setterType);
289304
if (!typeEnvironment.isSubtypeOf(
@@ -316,7 +331,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
316331
ir.DartType visitDirectPropertyGet(ir.DirectPropertyGet node) {
317332
ir.DartType receiverType = visitNode(node.receiver);
318333
ir.Class superclass = node.target.enclosingClass;
319-
receiverType = getTypeAsInstanceOf(receiverType, superclass);
334+
receiverType =
335+
getTypeAsInstanceOf(receiverType, superclass, currentLibrary);
320336
ir.DartType resultType = ir.Substitution.fromInterfaceType(receiverType)
321337
.substituteType(node.target.getterType);
322338
_expressionTypeCache[node] = resultType;
@@ -341,7 +357,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
341357
receiverType, argumentType);
342358
} else {
343359
ir.Class superclass = node.target.enclosingClass;
344-
receiverType = getTypeAsInstanceOf(receiverType, superclass);
360+
receiverType =
361+
getTypeAsInstanceOf(receiverType, superclass, currentLibrary);
345362
ir.DartType returnType = ir.Substitution.fromInterfaceType(receiverType)
346363
.substituteType(node.target.function.returnType);
347364
returnType = ir.Substitution.fromPairs(
@@ -597,7 +614,7 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
597614
if (interfaceTarget != null) {
598615
ir.Class superclass = interfaceTarget.enclosingClass;
599616
ir.Substitution receiverSubstitution = ir.Substitution.fromInterfaceType(
600-
getTypeAsInstanceOf(receiverType, superclass));
617+
getTypeAsInstanceOf(receiverType, superclass, currentLibrary));
601618
ir.DartType getterType =
602619
receiverSubstitution.substituteType(interfaceTarget.getterType);
603620
if (getterType is ir.FunctionType) {
@@ -810,8 +827,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
810827
if (declaringClass.typeParameters.isEmpty) {
811828
resultType = node.interfaceTarget.getterType;
812829
} else {
813-
ir.DartType receiver =
814-
typeEnvironment.getTypeAsInstanceOf(thisType, declaringClass);
830+
ir.DartType receiver = typeEnvironment.getTypeAsInstanceOf(thisType,
831+
declaringClass, currentLibrary, typeEnvironment.coreTypes);
815832
resultType = ir.Substitution.fromInterfaceType(receiver)
816833
.substituteType(node.interfaceTarget.getterType);
817834
}
@@ -843,8 +860,8 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
843860
returnType = const ir.DynamicType();
844861
} else {
845862
ir.Class superclass = node.interfaceTarget.enclosingClass;
846-
ir.InterfaceType receiverType =
847-
typeEnvironment.getTypeAsInstanceOf(thisType, superclass);
863+
ir.InterfaceType receiverType = typeEnvironment.getTypeAsInstanceOf(
864+
thisType, superclass, currentLibrary, typeEnvironment.coreTypes);
848865
returnType = ir.Substitution.fromInterfaceType(receiverType)
849866
.substituteType(node.interfaceTarget.function.returnType);
850867
returnType = ir.Substitution.fromPairs(
@@ -1216,7 +1233,10 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
12161233
if (node.isAsync) {
12171234
ir.InterfaceType streamInterfaceType = getInterfaceTypeOf(iterableType);
12181235
ir.InterfaceType streamType = typeEnvironment.getTypeAsInstanceOf(
1219-
streamInterfaceType, typeEnvironment.coreTypes.streamClass);
1236+
streamInterfaceType,
1237+
typeEnvironment.coreTypes.streamClass,
1238+
currentLibrary,
1239+
typeEnvironment.coreTypes);
12201240
if (streamType != null) {
12211241
iteratorType = new ir.InterfaceType(
12221242
typeEnvironment.coreTypes.streamIteratorClass,
@@ -1230,7 +1250,10 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
12301250
if (member != null) {
12311251
iteratorType = ir.Substitution.fromInterfaceType(
12321252
typeEnvironment.getTypeAsInstanceOf(
1233-
iterableInterfaceType, member.enclosingClass))
1253+
iterableInterfaceType,
1254+
member.enclosingClass,
1255+
currentLibrary,
1256+
typeEnvironment.coreTypes))
12341257
.substituteType(member.getterType);
12351258
}
12361259
}
@@ -1382,12 +1405,14 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
13821405
thisType = new ThisInterfaceType.from(node.enclosingClass?.getThisType(
13831406
typeEnvironment.coreTypes, node.enclosingLibrary.nonNullable));
13841407
_currentVariables = {};
1408+
currentLibrary = node.enclosingLibrary;
13851409
visitSignature(node.function);
13861410
visitNode(node.function.body);
13871411
handleProcedure(node);
13881412
_invalidatedVariables.removeAll(_currentVariables);
13891413
_currentVariables = null;
13901414
thisType = null;
1415+
currentLibrary = null;
13911416
}
13921417

13931418
void handleConstructor(ir.Constructor node) {}
@@ -1397,13 +1422,15 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
13971422
thisType = new ThisInterfaceType.from(node.enclosingClass.getThisType(
13981423
typeEnvironment.coreTypes, node.enclosingLibrary.nonNullable));
13991424
_currentVariables = {};
1425+
currentLibrary = node.enclosingLibrary;
14001426
visitSignature(node.function);
14011427
visitNodes(node.initializers);
14021428
visitNode(node.function.body);
14031429
handleConstructor(node);
14041430
_invalidatedVariables.removeAll(_currentVariables);
14051431
_currentVariables = null;
14061432
thisType = null;
1433+
currentLibrary = null;
14071434
}
14081435

14091436
void handleField(ir.Field node) {}
@@ -1413,11 +1440,13 @@ abstract class StaticTypeVisitor extends StaticTypeBase {
14131440
thisType = new ThisInterfaceType.from(node.enclosingClass?.getThisType(
14141441
typeEnvironment.coreTypes, node.enclosingLibrary.nonNullable));
14151442
_currentVariables = {};
1443+
currentLibrary = node.enclosingLibrary;
14161444
visitNode(node.initializer);
14171445
handleField(node);
14181446
_invalidatedVariables.removeAll(_currentVariables);
14191447
_currentVariables = null;
14201448
thisType = null;
1449+
currentLibrary = null;
14211450
}
14221451

14231452
void handleVariableDeclaration(ir.VariableDeclaration node) {}

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,8 +2987,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
29872987
DartType _expectedReturnType(FunctionNode f, Class expected) {
29882988
var type = f.thisFunctionType.returnType;
29892989
if (type is InterfaceType) {
2990-
var match = _hierarchy.getTypeAsInstanceOf(type, expected);
2991-
if (match != null) return match.typeArguments[0];
2990+
var matchArguments =
2991+
_hierarchy.getTypeArgumentsAsInstanceOf(type, expected);
2992+
if (matchArguments != null) return matchArguments[0];
29922993
}
29932994
return const DynamicType();
29942995
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ abstract class ClassBuilder implements DeclarationBuilder {
274274
Message message, int fileOffset, int length,
275275
{List<LocatedMessage> context});
276276

277-
void checkMixinApplication(ClassHierarchy hierarchy);
277+
void checkMixinApplication(ClassHierarchy hierarchy, CoreTypes coreTypes);
278278

279279
// Computes the function type of a given redirection target. Returns [null] if
280280
// the type of the target could not be computed.
@@ -1094,8 +1094,11 @@ abstract class ClassBuilderImpl extends DeclarationBuilderImpl
10941094
bool isInterfaceCheck) {
10951095
Substitution interfaceSubstitution = Substitution.empty;
10961096
if (interfaceMember.enclosingClass.typeParameters.isNotEmpty) {
1097-
interfaceSubstitution = Substitution.fromInterfaceType(types.hierarchy
1098-
.getKernelTypeAsInstanceOf(thisType, interfaceMember.enclosingClass));
1097+
Class enclosingClass = interfaceMember.enclosingClass;
1098+
interfaceSubstitution = Substitution.fromPairs(
1099+
enclosingClass.typeParameters,
1100+
types.hierarchy
1101+
.getKernelTypeArgumentsAsInstanceOf(thisType, enclosingClass));
10991102
}
11001103
if (declaredFunction?.typeParameters?.length !=
11011104
interfaceFunction?.typeParameters?.length) {
@@ -1166,8 +1169,11 @@ abstract class ClassBuilderImpl extends DeclarationBuilderImpl
11661169
Types types, Member declaredMember) {
11671170
Substitution declaredSubstitution = Substitution.empty;
11681171
if (declaredMember.enclosingClass.typeParameters.isNotEmpty) {
1169-
declaredSubstitution = Substitution.fromInterfaceType(types.hierarchy
1170-
.getKernelTypeAsInstanceOf(thisType, declaredMember.enclosingClass));
1172+
Class enclosingClass = declaredMember.enclosingClass;
1173+
declaredSubstitution = Substitution.fromPairs(
1174+
enclosingClass.typeParameters,
1175+
types.hierarchy
1176+
.getKernelTypeArgumentsAsInstanceOf(thisType, enclosingClass));
11711177
}
11721178
return declaredSubstitution;
11731179
}
@@ -1534,15 +1540,16 @@ abstract class ClassBuilderImpl extends DeclarationBuilderImpl
15341540
}
15351541

15361542
@override
1537-
void checkMixinApplication(ClassHierarchy hierarchy) {
1543+
void checkMixinApplication(ClassHierarchy hierarchy, CoreTypes coreTypes) {
15381544
// A mixin declaration can only be applied to a class that implements all
15391545
// the declaration's superclass constraints.
15401546
InterfaceType supertype = cls.supertype.asInterfaceType;
15411547
Substitution substitution = Substitution.fromSupertype(cls.mixedInType);
15421548
for (Supertype constraint in cls.mixedInClass.superclassConstraints()) {
15431549
InterfaceType interface =
15441550
substitution.substituteSupertype(constraint).asInterfaceType;
1545-
if (hierarchy.getTypeAsInstanceOf(supertype, interface.classNode) !=
1551+
if (hierarchy.getTypeAsInstanceOf(
1552+
supertype, interface.classNode, library.library, coreTypes) !=
15461553
interface) {
15471554
library.addProblem(
15481555
templateMixinApplicationIncompatibleSupertype.withArguments(

0 commit comments

Comments
 (0)