Skip to content

Commit 68dca61

Browse files
committed
AST: Use TypeTransform::transformGenericTypeParam() to transform GenericTypeParamType
1 parent d7ac12a commit 68dca61

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

include/swift/AST/TypeTransform.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ case TypeKind::Id:
112112
case TypeKind::Unresolved:
113113
case TypeKind::TypeVariable:
114114
case TypeKind::Placeholder:
115-
case TypeKind::GenericTypeParam:
116115
case TypeKind::SILToken:
117116
case TypeKind::Module:
118117
case TypeKind::BuiltinTuple:
119118
return t;
120119

120+
case TypeKind::GenericTypeParam: {
121+
auto *param = cast<GenericTypeParamType>(base);
122+
return asDerived().transformGenericTypeParam(param, pos);
123+
}
124+
121125
case TypeKind::Enum:
122126
case TypeKind::Struct:
123127
case TypeKind::Class:
@@ -966,6 +970,10 @@ case TypeKind::Id:
966970
return doIt(fieldTy, pos)->getCanonicalType();
967971
}
968972

973+
Type transformGenericTypeParam(GenericTypeParamType *param, TypePosition pos) {
974+
return param;
975+
}
976+
969977
Type transformPackExpansion(PackExpansionType *expand, TypePosition pos) {
970978
// Substitution completely replaces this.
971979

lib/AST/TypeSubstitution.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ class TypeSubstituter : public TypeTransform<TypeSubstituter> {
490490

491491
std::optional<Type> transform(TypeBase *type, TypePosition pos);
492492

493+
Type transformGenericTypeParam(GenericTypeParamType *param, TypePosition pos);
494+
493495
Type transformPackExpansion(PackExpansionType *expand, TypePosition pos);
494496

495497
Type transformPackElement(PackElementType *element, TypePosition pos);
@@ -512,7 +514,7 @@ TypeSubstituter::transform(TypeBase *type, TypePosition position) {
512514
"should not be doing AST type-substitution on a lowered SIL type;"
513515
"use SILType::subst");
514516

515-
auto substOrig = dyn_cast<SubstitutableType>(type);
517+
auto substOrig = dyn_cast<ArchetypeType>(type);
516518
if (!substOrig)
517519
return std::nullopt;
518520

@@ -532,41 +534,46 @@ TypeSubstituter::transform(TypeBase *type, TypePosition position) {
532534
return known;
533535
}
534536

535-
// If we failed to substitute a generic type parameter, give up.
536-
if (isa<GenericTypeParamType>(substOrig))
537-
return ErrorType::get(type);
538-
539-
auto origArchetype = cast<ArchetypeType>(substOrig);
540-
if (origArchetype->isRoot()) {
537+
if (substOrig->isRoot()) {
541538
// Root opened archetypes are not required to be substituted. Other root
542539
// archetypes must already have been substituted above.
543-
if (isa<LocalArchetypeType>(origArchetype)) {
540+
if (isa<LocalArchetypeType>(substOrig)) {
544541
return Type(type);
545542
} else {
546543
return ErrorType::get(type);
547544
}
548545
}
549546

550547
// For nested archetypes, we can substitute the parent.
551-
Type origParent = origArchetype->getParent();
548+
Type origParent = substOrig->getParent();
552549
assert(origParent && "Not a nested archetype");
553550

554551
// Substitute into the parent type.
555552
Type substParent = doIt(origParent, TypePosition::Invariant);
556553

557554
// If the parent didn't change, we won't change.
558-
if (substParent.getPointer() == origArchetype->getParent())
555+
if (substParent.getPointer() == substOrig->getParent())
559556
return Type(type);
560557

561558
// Get the associated type reference from a child archetype.
562-
AssociatedTypeDecl *assocType = origArchetype->getInterfaceType()
559+
AssociatedTypeDecl *assocType = substOrig->getInterfaceType()
563560
->castTo<DependentMemberType>()->getAssocType();
564561

565-
return getMemberForBaseType(IFS, origArchetype->getParent(), substParent,
562+
return getMemberForBaseType(IFS, substOrig->getParent(), substParent,
566563
assocType, assocType->getName(),
567564
level);
568565
}
569566

567+
Type TypeSubstituter::transformGenericTypeParam(GenericTypeParamType *param,
568+
TypePosition pos) {
569+
// If we have a substitution for this type, use it.
570+
if (auto known = IFS.substType(param, level))
571+
return known;
572+
573+
// If we failed to substitute a generic type parameter, give up.
574+
return ErrorType::get(param);
575+
}
576+
570577
Type TypeSubstituter::transformPackExpansion(PackExpansionType *expand,
571578
TypePosition pos) {
572579
auto eltTys = IFS.expandPackExpansionType(expand);

0 commit comments

Comments
 (0)