Skip to content

Commit c6c1573

Browse files
authored
Merge pull request swiftlang#27287 from slavapestov/typealias-sugar
Preserve TypeAliasType sugar when substituting types
2 parents 698bcad + 38416cf commit c6c1573

25 files changed

+168
-81
lines changed

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,18 +2177,11 @@ TypeAliasType *TypeAliasType::get(TypeAliasDecl *typealias, Type parent,
21772177
// Compute the recursive properties.
21782178
//
21792179
auto properties = underlying->getRecursiveProperties();
2180-
auto storedProperties = properties;
2181-
if (parent) {
2180+
if (parent)
21822181
properties |= parent->getRecursiveProperties();
2183-
if (parent->hasTypeVariable())
2184-
storedProperties |= RecursiveTypeProperties::HasTypeVariable;
2185-
}
21862182

2187-
for (auto substGP : substitutions.getReplacementTypes()) {
2183+
for (auto substGP : substitutions.getReplacementTypes())
21882184
properties |= substGP->getRecursiveProperties();
2189-
if (substGP->hasTypeVariable())
2190-
storedProperties |= RecursiveTypeProperties::HasTypeVariable;
2191-
}
21922185

21932186
// Figure out which arena this type will go into.
21942187
auto &ctx = underlying->getASTContext();
@@ -2210,7 +2203,7 @@ TypeAliasType *TypeAliasType::get(TypeAliasDecl *typealias, Type parent,
22102203
genericSig ? 1 : 0);
22112204
auto mem = ctx.Allocate(size, alignof(TypeAliasType), arena);
22122205
auto result = new (mem) TypeAliasType(typealias, parent, substitutions,
2213-
underlying, storedProperties);
2206+
underlying, properties);
22142207
types.InsertNode(result, insertPos);
22152208
return result;
22162209
}

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ Type ASTBuilder::createTypeAliasType(GenericTypeDecl *decl, Type parent) {
190190
auto *dc = aliasDecl->getDeclContext();
191191
auto subs = parent->getContextSubstitutionMap(dc->getParentModule(), dc);
192192

193-
// FIXME: subst() should build the sugar for us
194-
declaredType = declaredType.subst(subs);
195-
return TypeAliasType::get(aliasDecl, parent, subs, declaredType);
193+
return declaredType.subst(subs);
196194
}
197195

198196
static SubstitutionMap
@@ -323,9 +321,7 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
323321
if (!subMap)
324322
return Type();
325323

326-
// FIXME: subst() should build the sugar for us
327-
auto declaredType = aliasDecl->getDeclaredInterfaceType().subst(subMap);
328-
return TypeAliasType::get(aliasDecl, parent, subMap, declaredType);
324+
return aliasDecl->getDeclaredInterfaceType().subst(subMap);
329325
}
330326

331327
Type ASTBuilder::createTupleType(ArrayRef<Type> eltTypes,

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,16 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
814814
// It's not possible to mangle the context of the builtin module.
815815
// For the DWARF output we want to mangle the type alias + context,
816816
// unless the type alias references a builtin type.
817+
auto underlyingType = aliasTy->getSinglyDesugaredType();
817818
TypeAliasDecl *decl = aliasTy->getDecl();
818819
if (decl->getModuleContext() == decl->getASTContext().TheBuiltinModule) {
819-
return appendType(aliasTy->getSinglyDesugaredType(),
820-
forDecl);
820+
return appendType(underlyingType, forDecl);
821+
}
822+
823+
if (decl->getDeclaredInterfaceType()
824+
.subst(aliasTy->getSubstitutionMap()).getPointer()
825+
!= aliasTy) {
826+
return appendType(underlyingType, forDecl);
821827
}
822828

823829
if (aliasTy->getSubstitutionMap()) {

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3545,7 +3545,7 @@ void TypeAliasDecl::computeType() {
35453545
Type parent;
35463546
auto parentDC = getDeclContext();
35473547
if (parentDC->isTypeContext())
3548-
parent = parentDC->getDeclaredInterfaceType();
3548+
parent = parentDC->getSelfInterfaceType();
35493549
auto sugaredType = TypeAliasType::get(this, parent, subs, getUnderlyingType());
35503550
setInterfaceType(MetatypeType::get(sugaredType, ctx));
35513551
}

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static bool shouldShowAKA(Type type, StringRef typeName) {
398398
return false;
399399

400400
// Don't show generic type parameters.
401-
if (type->hasTypeParameter())
401+
if (type->getCanonicalType()->hasTypeParameter())
402402
return false;
403403

404404
// Only show 'aka' if there's a typealias involved; other kinds of sugar

lib/AST/Type.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,7 +3261,23 @@ static Type substType(Type derivedType,
32613261
boxTy->getLayout(),
32623262
newSubMap);
32633263
}
3264-
3264+
3265+
// Special-case TypeAliasType; we need to substitute conformances.
3266+
if (auto aliasTy = dyn_cast<TypeAliasType>(type)) {
3267+
Type parentTy;
3268+
if (auto origParentTy = aliasTy->getParent())
3269+
parentTy = substType(origParentTy,
3270+
substitutions, lookupConformances, options);
3271+
auto underlyingTy = substType(aliasTy->getSinglyDesugaredType(),
3272+
substitutions, lookupConformances, options);
3273+
if (parentTy && parentTy->isExistentialType())
3274+
return underlyingTy;
3275+
auto subMap = aliasTy->getSubstitutionMap()
3276+
.subst(substitutions, lookupConformances, options);
3277+
return Type(TypeAliasType::get(aliasTy->getDecl(), parentTy,
3278+
subMap, underlyingTy));
3279+
}
3280+
32653281
// We only substitute for substitutable types and dependent member types.
32663282

32673283
// For dependent member types, we may need to look up the member if the
@@ -3963,33 +3979,29 @@ case TypeKind::Id:
39633979

39643980
Type oldParentType = alias->getParent();
39653981
Type newParentType;
3966-
if (oldParentType && !oldParentType->hasTypeParameter() &&
3967-
!oldParentType->hasArchetype()) {
3982+
if (oldParentType) {
39683983
newParentType = oldParentType.transformRec(fn);
39693984
if (!newParentType) return newUnderlyingType;
39703985
}
39713986

39723987
auto subMap = alias->getSubstitutionMap();
39733988
for (Type oldReplacementType : subMap.getReplacementTypes()) {
3974-
if (oldReplacementType->hasTypeParameter() ||
3975-
oldReplacementType->hasArchetype())
3976-
return newUnderlyingType;
3977-
39783989
Type newReplacementType = oldReplacementType.transformRec(fn);
39793990
if (!newReplacementType)
39803991
return newUnderlyingType;
39813992

39823993
// If anything changed with the replacement type, we lose the sugar.
39833994
// FIXME: This is really unfortunate.
3984-
if (!newReplacementType->isEqual(oldReplacementType))
3995+
if (newReplacementType.getPointer() != oldReplacementType.getPointer())
39853996
return newUnderlyingType;
39863997
}
39873998

3988-
if (oldUnderlyingType.getPointer() == newUnderlyingType.getPointer())
3999+
if (oldParentType.getPointer() == newParentType.getPointer() &&
4000+
oldUnderlyingType.getPointer() == newUnderlyingType.getPointer())
39894001
return *this;
39904002

39914003
return TypeAliasType::get(alias->getDecl(), newParentType, subMap,
3992-
newUnderlyingType);
4004+
newUnderlyingType);
39934005
}
39944006

39954007
case TypeKind::Paren: {

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ static bool hasDependentTypeWitness(
863863
return false;
864864

865865
// RESILIENCE: this could be an opaque conformance
866-
return type->hasTypeParameter();
866+
return type->getCanonicalType()->hasTypeParameter();
867867
},
868868
/*useResolver=*/true)) {
869869
return true;

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4823,7 +4823,9 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
48234823
// to access the type with a protocol metatype base.
48244824
} else if (instanceTy->isExistentialType() &&
48254825
isa<TypeAliasDecl>(decl) &&
4826-
!cast<TypeAliasDecl>(decl)->getInterfaceType()->hasTypeParameter()) {
4826+
!cast<TypeAliasDecl>(decl)
4827+
->getUnderlyingType()->getCanonicalType()
4828+
->hasTypeParameter()) {
48274829

48284830
/* We're OK */
48294831

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static void checkNestedTypeConstraints(ConstraintSystem &cs, Type type,
622622

623623
Type ConstraintSystem::openUnboundGenericType(
624624
Type type, ConstraintLocatorBuilder locator) {
625-
assert(!type->hasTypeParameter());
625+
assert(!type->getCanonicalType()->hasTypeParameter());
626626

627627
checkNestedTypeConstraints(*this, type, locator);
628628

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,6 @@ StructuralTypeRequest::evaluate(Evaluator &evaluator,
990990
Type parent;
991991
auto parentDC = typeAlias->getDeclContext();
992992
if (parentDC->isTypeContext())
993-
parent = parentDC->getDeclaredInterfaceType();
993+
parent = parentDC->getSelfInterfaceType();
994994
return TypeAliasType::get(typeAlias, parent, subs, type);
995995
}

0 commit comments

Comments
 (0)