Skip to content

Commit dbd34a4

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2js] Remove nullability on type declaration for IR type variables.
The only nullable call to the JTypeVariable constructor was if the K-model had a Local as the typeDeclaration. However, the K-model creates LocalTypeVariable entities for these instead of TypeVariable entities. The former is never added to the K- element map so they aren't copied over to the J-model up front: https://github.com/dart-lang/sdk/blob/main/pkg/compiler/lib/src/kernel/element_map_impl.dart#L1531 Instead the corresponding JTypeVariable entities are added later when the closures are created: https://github.com/dart-lang/sdk/blob/main/pkg/compiler/lib/src/js_model/element_map_impl.dart#L1871 Change-Id: If018fe8ca666695989b0f2075d837f8e6994f0fb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/311060 Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 30b9595 commit dbd34a4

File tree

7 files changed

+25
-31
lines changed

7 files changed

+25
-31
lines changed

pkg/compiler/lib/src/elements/entities.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ abstract class ClassEntity extends Entity {
8787

8888
abstract class TypeVariableEntity extends Entity {
8989
/// The class or generic method that declared this type variable.
90-
/// Is `null` for some generic functions and closures.
91-
// TODO(sra): Figure out how to always have a [typeDeclaration].
92-
Entity? get typeDeclaration;
90+
Entity get typeDeclaration;
9391

9492
/// The index of this type variable in the type variables of its
9593
/// [typeDeclaration].

pkg/compiler/lib/src/elements/types.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ class _DartTypeToStringVisitor extends DartTypeVisitor<void, void> {
16601660

16611661
@override
16621662
void visitTypeVariableType(covariant TypeVariableType type, _) {
1663-
_identifier(type.element.typeDeclaration!.name!);
1663+
_identifier(type.element.typeDeclaration.name!);
16641664
_token('.');
16651665
_identifier(type.element.name!);
16661666
}

pkg/compiler/lib/src/js_backend/runtime_types.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class RuntimeTypesImpl
533533
.classInstantiationsOf(declaration)
534534
.map((InterfaceType interface) => interface.typeArguments[index]);
535535
} else {
536-
return typeVariableTests.instantiationsOf(declaration!).map(
536+
return typeVariableTests.instantiationsOf(declaration).map(
537537
(GenericInstantiation instantiation) =>
538538
instantiation.typeArguments[index]);
539539
}

pkg/compiler/lib/src/js_backend/runtime_types_resolution.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class TypeVariableTests {
309309
///
310310
void registerDependencies(RtiNode node, DartType type) {
311311
type.forEachTypeVariable((TypeVariableType typeVariable) {
312-
final typeDeclaration = typeVariable.element.typeDeclaration!;
312+
final typeDeclaration = typeVariable.element.typeDeclaration;
313313
if (typeDeclaration is ClassEntity) {
314314
node.addDependency(_getClassNode(typeDeclaration));
315315
} else {
@@ -326,7 +326,7 @@ class TypeVariableTests {
326326
}
327327

328328
void onTypeVariable(TypeVariableType type) {
329-
final declaration = type.element.typeDeclaration!;
329+
final declaration = type.element.typeDeclaration;
330330
if (declaration is ClassEntity) {
331331
node.addDependency(_getClassNode(declaration));
332332
} else {
@@ -470,7 +470,7 @@ class TypeVariableTests {
470470
void _propagateTests() {
471471
void processTypeVariableType(TypeVariableType type) {
472472
TypeVariableEntity variable = type.element;
473-
final typeDeclaration = variable.typeDeclaration!;
473+
final typeDeclaration = variable.typeDeclaration;
474474
if (typeDeclaration is ClassEntity) {
475475
_getClassNode(typeDeclaration).markTest();
476476
} else {
@@ -552,7 +552,7 @@ class TypeVariableTests {
552552

553553
void _addImplicitChecksViaInstantiation(TypeVariableType variable) {
554554
TypeVariableEntity entity = variable.element;
555-
final declaration = entity.typeDeclaration!;
555+
final declaration = entity.typeDeclaration;
556556
if (declaration is ClassEntity) {
557557
classInstantiationsOf(declaration).forEach((InterfaceType type) {
558558
_addImplicitCheck(type.typeArguments[entity.index]);
@@ -965,7 +965,6 @@ class RuntimeTypesNeedBuilderImpl implements RuntimeTypesNeedBuilder {
965965
@override
966966
void registerTypeVariableLiteral(TypeVariableType variable) {
967967
final typeDeclaration = variable.element.typeDeclaration;
968-
assert(typeDeclaration != null);
969968
if (typeDeclaration is ClassEntity) {
970969
registerClassUsingTypeVariableLiteral(typeDeclaration);
971970
} else if (typeDeclaration is FunctionEntity) {
@@ -1060,7 +1059,7 @@ class RuntimeTypesNeedBuilderImpl implements RuntimeTypesNeedBuilder {
10601059
potentiallyNeedTypeArguments(function);
10611060
}
10621061
functionType.forEachTypeVariable((TypeVariableType typeVariable) {
1063-
final typeDeclaration = typeVariable.element.typeDeclaration!;
1062+
final typeDeclaration = typeVariable.element.typeDeclaration;
10641063
if (!processedEntities.contains(typeDeclaration)) {
10651064
potentiallyNeedTypeArguments(typeDeclaration);
10661065
}
@@ -1100,7 +1099,7 @@ class RuntimeTypesNeedBuilderImpl implements RuntimeTypesNeedBuilder {
11001099
type.forEachTypeVariable((TypeVariableType typeVariable) {
11011100
// This handles checks against type variables and function types
11021101
// containing type variables.
1103-
final typeDeclaration = typeVariable.element.typeDeclaration!;
1102+
final typeDeclaration = typeVariable.element.typeDeclaration;
11041103
potentiallyNeedTypeArguments(typeDeclaration);
11051104
});
11061105
if (type is FunctionType) {
@@ -1295,7 +1294,7 @@ class RuntimeTypesNeedBuilderImpl implements RuntimeTypesNeedBuilder {
12951294
FunctionType functionType =
12961295
_elementEnvironment.getLocalFunctionType(function);
12971296
functionType.forEachTypeVariable((TypeVariableType typeVariable) {
1298-
final typeDeclaration = typeVariable.element.typeDeclaration!;
1297+
final typeDeclaration = typeVariable.element.typeDeclaration;
12991298
if (!processedEntities.contains(typeDeclaration)) {
13001299
potentiallyNeedTypeArguments(typeDeclaration);
13011300
}

pkg/compiler/lib/src/js_backend/type_reference.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ class _RecipeToIdentifier extends DartTypeVisitor<void, Null> {
667667

668668
@override
669669
void visitTypeVariableType(covariant TypeVariableType type, _) {
670-
_identifier(type.element.typeDeclaration!.name!);
670+
_identifier(type.element.typeDeclaration.name!);
671671
_identifier(type.element.name!);
672672
}
673673

pkg/compiler/lib/src/js_model/element_map_impl.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,17 @@ class JsKernelToElementMap implements JsToElementMap, IrToElementMap {
270270
final cls = oldTypeVariable.typeDeclaration as IndexedClass;
271271
newTypeDeclaration = classes.getEntity(cls.classIndex);
272272
// TODO(johnniwinther): Skip type variables of unused classes.
273-
} else if (oldTypeVariable.typeDeclaration is MemberEntity) {
273+
} else {
274+
assert(oldTypeVariable.typeDeclaration is MemberEntity);
274275
final member = oldTypeVariable.typeDeclaration as IndexedMember;
275276
newTypeDeclaration = members.getEntity(member.memberIndex);
276277
if (newTypeDeclaration == null) {
277278
typeVariables.skipIndex(typeVariableIndex);
278279
continue;
279280
}
280-
} else {
281-
assert(oldTypeVariable.typeDeclaration is Local);
282281
}
283282
IndexedTypeVariable newTypeVariable = createTypeVariable(
284-
newTypeDeclaration, oldTypeVariable.name!, oldTypeVariable.index)
283+
newTypeDeclaration!, oldTypeVariable.name!, oldTypeVariable.index)
285284
as IndexedTypeVariable;
286285
typeVariableMap[oldTypeVariableData.node] =
287286
typeVariables.register<IndexedTypeVariable, JTypeVariableData>(
@@ -1552,7 +1551,7 @@ class JsKernelToElementMap implements JsToElementMap, IrToElementMap {
15521551
}
15531552

15541553
TypeVariableEntity createTypeVariable(
1555-
Entity? typeDeclaration, String name, int index) {
1554+
Entity typeDeclaration, String name, int index) {
15561555
return JTypeVariable(typeDeclaration, name, index);
15571556
}
15581557

pkg/compiler/lib/src/js_model/elements.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -753,21 +753,27 @@ class JSignatureMethod extends JMethod {
753753
}
754754

755755
/// Enum used for identifying [JTypeVariable] variants in serialization.
756-
enum JTypeVariableKind { cls, member, local }
756+
///
757+
/// [JLocalTypeVariable] in the K-world can contain a [Local] as its
758+
/// `typeDeclaration` but those are never serialized. When converted to the
759+
/// J-world they use a [JClosureCallMethod] as [JTypeVariable.typeDeclaration].
760+
enum JTypeVariableKind { cls, member }
757761

758762
class JTypeVariable extends IndexedTypeVariable {
759763
/// Tag used for identifying serialized [JTypeVariable] objects in a
760764
/// debugging data stream.
761765
static const String tag = 'type-variable';
762766

763767
@override
764-
final Entity? typeDeclaration;
768+
final Entity typeDeclaration;
765769
@override
766770
final String name;
767771
@override
768772
final int index;
769773

770-
JTypeVariable(this.typeDeclaration, this.name, this.index);
774+
JTypeVariable(this.typeDeclaration, this.name, this.index) {
775+
assert(typeDeclaration is ClassEntity || typeDeclaration is MemberEntity);
776+
}
771777

772778
/// Deserializes a [JTypeVariable] object from [source].
773779
factory JTypeVariable.readFromDataSource(DataSourceReader source) {
@@ -781,12 +787,6 @@ class JTypeVariable extends IndexedTypeVariable {
781787
case JTypeVariableKind.member:
782788
typeDeclaration = source.readMember();
783789
break;
784-
case JTypeVariableKind.local:
785-
// Type variables declared by local functions don't point to their
786-
// declaration, since the corresponding closure call methods is created
787-
// after the type variable.
788-
// TODO(johnniwinther): Fix this.
789-
break;
790790
}
791791
String name = source.readString();
792792
int index = source.readInt();
@@ -804,8 +804,6 @@ class JTypeVariable extends IndexedTypeVariable {
804804
} else if (declaration is MemberEntity) {
805805
sink.writeEnum(JTypeVariableKind.member);
806806
sink.writeMember(declaration);
807-
} else if (declaration == null) {
808-
sink.writeEnum(JTypeVariableKind.local);
809807
} else {
810808
throw UnsupportedError(
811809
"Unexpected type variable declarer $typeDeclaration.");
@@ -817,7 +815,7 @@ class JTypeVariable extends IndexedTypeVariable {
817815

818816
@override
819817
String toString() =>
820-
'${jsElementPrefix}type_variable(${typeDeclaration?.name}.$name)';
818+
'${jsElementPrefix}type_variable(${typeDeclaration.name}.$name)';
821819
}
822820

823821
class JLocalFunction implements Local {

0 commit comments

Comments
 (0)