Skip to content

Commit edd5105

Browse files
committed
AST: Clean up usages of TypeAliasDecl::getStructuralType()
This also gets rid of a usage of TypeAliasType::get().
1 parent 645fc20 commit edd5105

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,16 +3784,17 @@ PotentialArchetype *GenericSignatureBuilder::realizePotentialArchetype(
37843784
return pa;
37853785
}
37863786

3787-
static Type getStructuralType(TypeDecl *typeDecl) {
3787+
static Type getStructuralType(TypeDecl *typeDecl, bool keepSugar) {
37883788
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
3789-
// When we're computing requirement signatures, the structural type
3790-
// suffices. Otherwise we'll potentially try to validate incomplete
3791-
// requirements.
3792-
auto *proto = dyn_cast_or_null<ProtocolDecl>(
3793-
typealias->getDeclContext()->getAsDecl());
3794-
if (proto && proto->isComputingRequirementSignature())
3795-
return typealias->getStructuralType();
3796-
return typealias->getUnderlyingType();
3789+
if (typealias->getUnderlyingTypeRepr() != nullptr) {
3790+
auto type = typealias->getStructuralType();
3791+
if (!keepSugar)
3792+
if (auto *aliasTy = cast<TypeAliasType>(type.getPointer()))
3793+
return aliasTy->getSinglyDesugaredType();
3794+
return type;
3795+
}
3796+
if (!keepSugar)
3797+
return typealias->getUnderlyingType();
37973798
}
37983799

37993800
return typeDecl->getDeclaredInterfaceType();
@@ -3804,43 +3805,35 @@ static Type substituteConcreteType(GenericSignatureBuilder &builder,
38043805
TypeDecl *concreteDecl) {
38053806
assert(concreteDecl);
38063807

3807-
auto *proto = concreteDecl->getDeclContext()->getSelfProtocolDecl();
3808+
auto *dc = concreteDecl->getDeclContext();
3809+
auto *proto = dc->getSelfProtocolDecl();
38083810

38093811
// Form an unsubstituted type referring to the given type declaration,
38103812
// for use in an inferred same-type requirement.
3811-
auto type = getStructuralType(concreteDecl);
3813+
auto type = getStructuralType(concreteDecl, /*keepSugar=*/true);
38123814
if (!type)
38133815
return Type();
38143816

3815-
Type parentType;
38163817
SubstitutionMap subMap;
38173818
if (proto) {
38183819
// Substitute in the type of the current PotentialArchetype in
38193820
// place of 'Self' here.
3820-
parentType = basePA->getDependentType(builder.getGenericParams());
3821+
auto parentType = basePA->getDependentType(builder.getGenericParams());
38213822

38223823
subMap = SubstitutionMap::getProtocolSubstitutions(
38233824
proto, parentType, ProtocolConformanceRef(proto));
3824-
3825-
type = type.subst(subMap);
38263825
} else {
38273826
// Substitute in the superclass type.
38283827
auto parentPA = basePA->getEquivalenceClassIfPresent();
3829-
parentType =
3828+
auto parentType =
38303829
parentPA->concreteType ? parentPA->concreteType : parentPA->superclass;
38313830
auto parentDecl = parentType->getAnyNominal();
38323831

3833-
subMap = parentType->getMemberSubstitutionMap(parentDecl->getParentModule(),
3834-
concreteDecl);
3835-
type = type.subst(subMap);
3836-
}
3837-
3838-
// If we had a typealias, form a sugared type.
3839-
if (auto *typealias = dyn_cast<TypeAliasDecl>(concreteDecl)) {
3840-
type = TypeAliasType::get(typealias, parentType, subMap, type);
3832+
subMap = parentType->getContextSubstitutionMap(
3833+
parentDecl->getParentModule(), dc);
38413834
}
38423835

3843-
return type;
3836+
return type.subst(subMap);
38443837
};
38453838

38463839
ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
@@ -4215,10 +4208,10 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42154208
// An inferred same-type requirement between the two type declarations
42164209
// within this protocol or a protocol it inherits.
42174210
auto addInferredSameTypeReq = [&](TypeDecl *first, TypeDecl *second) {
4218-
Type firstType = getStructuralType(first);
4211+
Type firstType = getStructuralType(first, /*keepSugar=*/false);
42194212
if (!firstType) return;
42204213

4221-
Type secondType = getStructuralType(second);
4214+
Type secondType = getStructuralType(second, /*keepSugar=*/false);
42224215
if (!secondType) return;
42234216

42244217
auto inferredSameTypeSource =

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,8 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
534534
dyn_cast<TypeAliasDecl>(unboundGeneric->getAnyGeneric())) {
535535
if (ugAliasDecl == aliasDecl) {
536536
if (resolution.getStage() == TypeResolutionStage::Structural &&
537-
!aliasDecl->hasInterfaceType()) {
538-
return resolution.mapTypeIntoContext(
539-
aliasDecl->getStructuralType());
537+
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
538+
return aliasDecl->getStructuralType();
540539
}
541540
return resolution.mapTypeIntoContext(
542541
aliasDecl->getDeclaredInterfaceType());
@@ -550,9 +549,8 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
550549
dyn_cast<TypeAliasType>(extendedType.getPointer())) {
551550
if (aliasType->getDecl() == aliasDecl) {
552551
if (resolution.getStage() == TypeResolutionStage::Structural &&
553-
!aliasDecl->hasInterfaceType()) {
554-
return resolution.mapTypeIntoContext(
555-
aliasDecl->getStructuralType());
552+
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
553+
return aliasDecl->getStructuralType();
556554
}
557555
return resolution.mapTypeIntoContext(
558556
aliasDecl->getDeclaredInterfaceType());
@@ -577,8 +575,8 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
577575

578576
// Otherwise, return the appropriate type.
579577
if (resolution.getStage() == TypeResolutionStage::Structural &&
580-
!aliasDecl->hasInterfaceType()) {
581-
return resolution.mapTypeIntoContext(aliasDecl->getStructuralType());
578+
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
579+
return aliasDecl->getStructuralType();
582580
}
583581
return resolution.mapTypeIntoContext(
584582
aliasDecl->getDeclaredInterfaceType());

test/Generics/requirement_inference.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ struct X28 : P2 {
383383
}
384384

385385
// CHECK-LABEL: .P28@
386-
// CHECK-NEXT: Requirement signature: <Self where Self : P3, Self.P3Assoc == Self.P3Assoc>
386+
// CHECK-NEXT: Requirement signature: <Self where Self : P3, Self.P3Assoc == X28>
387387
// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0 : P3, τ_0_0.P3Assoc == X28>
388388
protocol P28: P3 {
389389
typealias P3Assoc = X28 // expected-warning{{typealias overriding associated type}}

0 commit comments

Comments
 (0)