Skip to content

Commit 98aa47c

Browse files
authored
Merge pull request #27357 from slavapestov/circular-validation-cleanups
Circular validation cleanups, part 1
2 parents 66a2f62 + 5e49002 commit 98aa47c

10 files changed

+190
-199
lines changed

lib/AST/Decl.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,13 +3248,10 @@ Type TypeDecl::getDeclaredInterfaceType() const {
32483248
}
32493249

32503250
Type interfaceType = getInterfaceType();
3251-
if (interfaceType.isNull() || interfaceType->is<ErrorType>())
3252-
return interfaceType;
3253-
3254-
if (isa<ModuleDecl>(this))
3255-
return interfaceType;
3251+
if (!interfaceType)
3252+
return ErrorType::get(getASTContext());
32563253

3257-
return interfaceType->castTo<MetatypeType>()->getInstanceType();
3254+
return interfaceType->getMetatypeInstanceType();
32583255
}
32593256

32603257
int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
@@ -3551,9 +3548,10 @@ void TypeAliasDecl::computeType() {
35513548
}
35523549

35533550
Type TypeAliasDecl::getUnderlyingType() const {
3554-
return evaluateOrDefault(getASTContext().evaluator,
3551+
auto &ctx = getASTContext();
3552+
return evaluateOrDefault(ctx.evaluator,
35553553
UnderlyingTypeRequest{const_cast<TypeAliasDecl *>(this)},
3556-
Type());
3554+
ErrorType::get(ctx));
35573555
}
35583556

35593557
void TypeAliasDecl::setUnderlyingType(Type underlying) {
@@ -3583,10 +3581,11 @@ UnboundGenericType *TypeAliasDecl::getUnboundGenericType() const {
35833581
}
35843582

35853583
Type TypeAliasDecl::getStructuralType() const {
3586-
auto &context = getASTContext();
3584+
auto &ctx = getASTContext();
35873585
return evaluateOrDefault(
3588-
context.evaluator,
3589-
StructuralTypeRequest{const_cast<TypeAliasDecl *>(this)}, Type());
3586+
ctx.evaluator,
3587+
StructuralTypeRequest{const_cast<TypeAliasDecl *>(this)},
3588+
ErrorType::get(ctx));
35903589
}
35913590

35923591
Type AbstractTypeParamDecl::getSuperclass() const {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 20 additions & 32 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,33 @@ 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);
3812-
if (!type)
3813-
return Type();
3813+
auto type = getStructuralType(concreteDecl, /*keepSugar=*/true);
38143814

3815-
Type parentType;
38163815
SubstitutionMap subMap;
38173816
if (proto) {
38183817
// Substitute in the type of the current PotentialArchetype in
38193818
// place of 'Self' here.
3820-
parentType = basePA->getDependentType(builder.getGenericParams());
3819+
auto parentType = basePA->getDependentType(builder.getGenericParams());
38213820

38223821
subMap = SubstitutionMap::getProtocolSubstitutions(
38233822
proto, parentType, ProtocolConformanceRef(proto));
3824-
3825-
type = type.subst(subMap);
38263823
} else {
38273824
// Substitute in the superclass type.
38283825
auto parentPA = basePA->getEquivalenceClassIfPresent();
3829-
parentType =
3826+
auto parentType =
38303827
parentPA->concreteType ? parentPA->concreteType : parentPA->superclass;
38313828
auto parentDecl = parentType->getAnyNominal();
38323829

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);
3830+
subMap = parentType->getContextSubstitutionMap(
3831+
parentDecl->getParentModule(), dc);
38413832
}
38423833

3843-
return type;
3834+
return type.subst(subMap);
38443835
};
38453836

38463837
ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
@@ -4215,11 +4206,8 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42154206
// An inferred same-type requirement between the two type declarations
42164207
// within this protocol or a protocol it inherits.
42174208
auto addInferredSameTypeReq = [&](TypeDecl *first, TypeDecl *second) {
4218-
Type firstType = getStructuralType(first);
4219-
if (!firstType) return;
4220-
4221-
Type secondType = getStructuralType(second);
4222-
if (!secondType) return;
4209+
Type firstType = getStructuralType(first, /*keepSugar=*/false);
4210+
Type secondType = getStructuralType(second, /*keepSugar=*/false);
42234211

42244212
auto inferredSameTypeSource =
42254213
FloatingRequirementSource::viaProtocolRequirement(

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3961,7 +3961,7 @@ void swift::performSyntacticExprDiagnostics(TypeChecker &TC, const Expr *E,
39613961
if (!TC.Context.isSwiftVersionAtLeast(5))
39623962
diagnoseDeprecatedWritableKeyPath(TC, E, DC);
39633963
if (!TC.getLangOpts().DisableAvailabilityChecking)
3964-
diagAvailability(TC, E, const_cast<DeclContext*>(DC));
3964+
diagAvailability(E, const_cast<DeclContext*>(DC));
39653965
if (TC.Context.LangOpts.EnableObjCInterop)
39663966
diagDeprecatedObjCSelectors(TC, DC, E);
39673967
}

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
143143
TreatUsableFromInlineAsPublic).isPublic())
144144
return false;
145145

146+
auto &Context = DC->getASTContext();
147+
146148
// Dynamic declarations were mistakenly not checked in Swift 4.2.
147149
// Do enforce the restriction even in pre-Swift-5 modes if the module we're
148150
// building is resilient, though.
@@ -194,18 +196,19 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
194196
if (downgradeToWarning == DowngradeToWarning::Yes)
195197
diagID = diag::resilience_decl_unavailable_warn;
196198

197-
diagnose(loc, diagID,
199+
Context.Diags.diagnose(
200+
loc, diagID,
198201
D->getDescriptiveKind(), diagName,
199202
D->getFormalAccessScope().accessLevelForDiagnostics(),
200203
static_cast<unsigned>(Kind),
201204
isAccessor);
202205

203206
if (TreatUsableFromInlineAsPublic) {
204-
diagnose(D, diag::resilience_decl_declared_here,
205-
D->getDescriptiveKind(), diagName, isAccessor);
207+
Context.Diags.diagnose(D, diag::resilience_decl_declared_here,
208+
D->getDescriptiveKind(), diagName, isAccessor);
206209
} else {
207-
diagnose(D, diag::resilience_decl_declared_here_public,
208-
D->getDescriptiveKind(), diagName, isAccessor);
210+
Context.Diags.diagnose(D, diag::resilience_decl_declared_here_public,
211+
D->getDescriptiveKind(), diagName, isAccessor);
209212
}
210213

211214
return (downgradeToWarning == DowngradeToWarning::No);

0 commit comments

Comments
 (0)