Skip to content

Commit 1119b91

Browse files
authored
Merge pull request #24204 from xymus/structural-type-request
GSB: use a request for the structural type of type aliases
2 parents 59a1198 + bddb40d commit 1119b91

10 files changed

+80
-10
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ SWIFT_TYPEID(Type)
2020
SWIFT_TYPEID(PropertyDelegateBackingPropertyInfo)
2121
SWIFT_TYPEID(PropertyDelegateTypeInfo)
2222
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
23+
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct PropertyDelegateBackingPropertyInfo;
2626
struct PropertyDelegateTypeInfo;
2727
class Type;
2828
class VarDecl;
29+
class TypeAliasDecl;
2930

3031
#define SWIFT_AST_TYPEID_ZONE 1
3132

include/swift/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,8 @@ class TypeAliasDecl : public GenericTypeDecl {
28962896
/// For generic typealiases, return the unbound generic type.
28972897
UnboundGenericType *getUnboundGenericType() const;
28982898

2899+
Type getStructuralType() const;
2900+
28992901
bool isCompatibilityAlias() const {
29002902
return Bits.TypeAliasDecl.IsCompatibilityAlias;
29012903
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class GenericParamList;
3333
struct PropertyDelegateBackingPropertyInfo;
3434
class RequirementRepr;
3535
class SpecializeAttr;
36+
class TypeAliasDecl;
3637
struct TypeLoc;
3738

3839
/// Display a nominal type or extension thereof.
@@ -547,6 +548,30 @@ class PropertyDelegateBackingPropertyInfoRequest :
547548
void noteCycleStep(DiagnosticEngine &diags) const;
548549
};
549550

551+
/// Retrieve the structural type of an alias type.
552+
class StructuralTypeRequest :
553+
public SimpleRequest<StructuralTypeRequest,
554+
CacheKind::Cached,
555+
Type,
556+
TypeAliasDecl*> {
557+
public:
558+
using SimpleRequest::SimpleRequest;
559+
560+
private:
561+
friend SimpleRequest;
562+
563+
// Evaluation.
564+
llvm::Expected<Type> evaluate(Evaluator &eval, TypeAliasDecl *d) const;
565+
566+
public:
567+
// Cycle handling.
568+
void diagnoseCycle(DiagnosticEngine &diags) const;
569+
void noteCycleStep(DiagnosticEngine &diags) const;
570+
571+
// Caching.
572+
bool isCached() const { return true; }
573+
};
574+
550575
// Allow AnyValue to compare two Type values, even though Type doesn't
551576
// support ==.
552577
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SWIFT_TYPEID(IsFinalRequest)
2323
SWIFT_TYPEID(IsDynamicRequest)
2424
SWIFT_TYPEID(RequirementRequest)
2525
SWIFT_TYPEID(USRGenerationRequest)
26+
SWIFT_TYPEID(StructuralTypeRequest)
2627
SWIFT_TYPEID(DefaultTypeRequest)
2728
SWIFT_TYPEID(MangleLocalTypeDeclRequest)
2829
SWIFT_TYPEID(PropertyDelegateTypeInfoRequest)

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,6 +3451,15 @@ UnboundGenericType *TypeAliasDecl::getUnboundGenericType() const {
34513451
parentTy, getASTContext());
34523452
}
34533453

3454+
Type TypeAliasDecl::getStructuralType() const {
3455+
assert(!getGenericParams());
3456+
3457+
auto &context = getASTContext();
3458+
return evaluateOrDefault(context.evaluator,
3459+
StructuralTypeRequest { const_cast<TypeAliasDecl *>(this) },
3460+
Type());
3461+
}
3462+
34543463
Type AbstractTypeParamDecl::getSuperclass() const {
34553464
auto *genericEnv = getDeclContext()->getGenericEnvironmentOfContext();
34563465
assert(genericEnv != nullptr && "Too much circularity");

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,13 +3805,12 @@ PotentialArchetype *GenericSignatureBuilder::realizePotentialArchetype(
38053805
return pa;
38063806
}
38073807

3808-
static Type getStructuralType(TypeDecl *typeDecl, LazyResolver *resolver) {
3808+
static Type getStructuralType(TypeDecl *typeDecl) {
38093809
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
3810-
// Resolve the underlying type, if we haven't done so yet.
3811-
if (!typealias->hasInterfaceType())
3812-
resolver->resolveDeclSignature(typealias);
3810+
if (auto resolved = typealias->getUnderlyingTypeLoc().getType())
3811+
return resolved;
38133812

3814-
return typealias->getUnderlyingTypeLoc().getType();
3813+
return typealias->getStructuralType();
38153814
}
38163815

38173816
return typeDecl->getDeclaredInterfaceType();
@@ -3826,7 +3825,7 @@ static Type substituteConcreteType(GenericSignatureBuilder &builder,
38263825

38273826
// Form an unsubstituted type referring to the given type declaration,
38283827
// for use in an inferred same-type requirement.
3829-
auto type = getStructuralType(concreteDecl, builder.getLazyResolver());
3828+
auto type = getStructuralType(concreteDecl);
38303829
if (!type)
38313830
return Type();
38323831

@@ -4164,8 +4163,14 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
41644163
if (inheritedProto == proto) return TypeWalker::Action::Continue;
41654164

41664165
for (auto req : inheritedProto->getMembers()) {
4167-
if (auto typeReq = dyn_cast<TypeDecl>(req))
4166+
if (auto typeReq = dyn_cast<TypeDecl>(req)) {
4167+
// Ignore generic types
4168+
if (auto genReq = dyn_cast<GenericTypeDecl>(req))
4169+
if (genReq->getGenericParams())
4170+
continue;
4171+
41684172
inheritedTypeDecls[typeReq->getFullName()].push_back(typeReq);
4173+
}
41694174
}
41704175
return TypeWalker::Action::Continue;
41714176
});
@@ -4228,10 +4233,10 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42284233
// An inferred same-type requirement between the two type declarations
42294234
// within this protocol or a protocol it inherits.
42304235
auto addInferredSameTypeReq = [&](TypeDecl *first, TypeDecl *second) {
4231-
Type firstType = getStructuralType(first, getLazyResolver());
4236+
Type firstType = getStructuralType(first);
42324237
if (!firstType) return;
42334238

4234-
Type secondType = getStructuralType(second, getLazyResolver());
4239+
Type secondType = getStructuralType(second);
42354240
if (!secondType) return;
42364241

42374242
auto inferredSameTypeSource =

lib/AST/TypeCheckRequests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,15 @@ void swift::simple_display(llvm::raw_ostream &out, const Type &type) {
652652
else
653653
out << "null";
654654
}
655+
656+
//----------------------------------------------------------------------------//
657+
// StructuralTypeRequest.
658+
//----------------------------------------------------------------------------//
659+
660+
void StructuralTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
661+
diags.diagnose(SourceLoc(), diag::circular_reference);
662+
}
663+
664+
void StructuralTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
665+
diags.diagnose(SourceLoc(), diag::circular_reference_through);
666+
}

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,17 @@ RequirementRequest::evaluate(Evaluator &evaluator,
10081008
}
10091009
llvm_unreachable("unhandled kind");
10101010
}
1011+
1012+
llvm::Expected<Type>
1013+
swift::StructuralTypeRequest::evaluate(Evaluator &evaluator,
1014+
TypeAliasDecl *D) const {
1015+
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
1016+
if (!D->getDeclContext()->isCascadingContextForLookup(
1017+
/*functionsAreNonCascading*/true)) {
1018+
options |= TypeResolutionFlags::KnownNonCascadingDependency;
1019+
}
1020+
1021+
auto typeRepr = D->getUnderlyingTypeLoc().getTypeRepr();
1022+
auto resolution = TypeResolution::forStructural(D);
1023+
return resolution.resolveType(typeRepr, options);
1024+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol
1111
P{protocol A:P{{}}typealias e:A{}class a<a{}class a

0 commit comments

Comments
 (0)