Skip to content

Circular validation cleanups, part 1 #27357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3248,13 +3248,10 @@ Type TypeDecl::getDeclaredInterfaceType() const {
}

Type interfaceType = getInterfaceType();
if (interfaceType.isNull() || interfaceType->is<ErrorType>())
return interfaceType;

if (isa<ModuleDecl>(this))
return interfaceType;
if (!interfaceType)
return ErrorType::get(getASTContext());

return interfaceType->castTo<MetatypeType>()->getInstanceType();
return interfaceType->getMetatypeInstanceType();
}

int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
Expand Down Expand Up @@ -3551,9 +3548,10 @@ void TypeAliasDecl::computeType() {
}

Type TypeAliasDecl::getUnderlyingType() const {
return evaluateOrDefault(getASTContext().evaluator,
auto &ctx = getASTContext();
return evaluateOrDefault(ctx.evaluator,
UnderlyingTypeRequest{const_cast<TypeAliasDecl *>(this)},
Type());
ErrorType::get(ctx));
}

void TypeAliasDecl::setUnderlyingType(Type underlying) {
Expand Down Expand Up @@ -3583,10 +3581,11 @@ UnboundGenericType *TypeAliasDecl::getUnboundGenericType() const {
}

Type TypeAliasDecl::getStructuralType() const {
auto &context = getASTContext();
auto &ctx = getASTContext();
return evaluateOrDefault(
context.evaluator,
StructuralTypeRequest{const_cast<TypeAliasDecl *>(this)}, Type());
ctx.evaluator,
StructuralTypeRequest{const_cast<TypeAliasDecl *>(this)},
ErrorType::get(ctx));
}

Type AbstractTypeParamDecl::getSuperclass() const {
Expand Down
52 changes: 20 additions & 32 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3784,16 +3784,17 @@ PotentialArchetype *GenericSignatureBuilder::realizePotentialArchetype(
return pa;
}

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

return typeDecl->getDeclaredInterfaceType();
Expand All @@ -3804,43 +3805,33 @@ static Type substituteConcreteType(GenericSignatureBuilder &builder,
TypeDecl *concreteDecl) {
assert(concreteDecl);

auto *proto = concreteDecl->getDeclContext()->getSelfProtocolDecl();
auto *dc = concreteDecl->getDeclContext();
auto *proto = dc->getSelfProtocolDecl();

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

Type parentType;
SubstitutionMap subMap;
if (proto) {
// Substitute in the type of the current PotentialArchetype in
// place of 'Self' here.
parentType = basePA->getDependentType(builder.getGenericParams());
auto parentType = basePA->getDependentType(builder.getGenericParams());

subMap = SubstitutionMap::getProtocolSubstitutions(
proto, parentType, ProtocolConformanceRef(proto));

type = type.subst(subMap);
} else {
// Substitute in the superclass type.
auto parentPA = basePA->getEquivalenceClassIfPresent();
parentType =
auto parentType =
parentPA->concreteType ? parentPA->concreteType : parentPA->superclass;
auto parentDecl = parentType->getAnyNominal();

subMap = parentType->getMemberSubstitutionMap(parentDecl->getParentModule(),
concreteDecl);
type = type.subst(subMap);
}

// If we had a typealias, form a sugared type.
if (auto *typealias = dyn_cast<TypeAliasDecl>(concreteDecl)) {
type = TypeAliasType::get(typealias, parentType, subMap, type);
subMap = parentType->getContextSubstitutionMap(
parentDecl->getParentModule(), dc);
}

return type;
return type.subst(subMap);
};

ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
Expand Down Expand Up @@ -4215,11 +4206,8 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
// An inferred same-type requirement between the two type declarations
// within this protocol or a protocol it inherits.
auto addInferredSameTypeReq = [&](TypeDecl *first, TypeDecl *second) {
Type firstType = getStructuralType(first);
if (!firstType) return;

Type secondType = getStructuralType(second);
if (!secondType) return;
Type firstType = getStructuralType(first, /*keepSugar=*/false);
Type secondType = getStructuralType(second, /*keepSugar=*/false);

auto inferredSameTypeSource =
FloatingRequirementSource::viaProtocolRequirement(
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3961,7 +3961,7 @@ void swift::performSyntacticExprDiagnostics(TypeChecker &TC, const Expr *E,
if (!TC.Context.isSwiftVersionAtLeast(5))
diagnoseDeprecatedWritableKeyPath(TC, E, DC);
if (!TC.getLangOpts().DisableAvailabilityChecking)
diagAvailability(TC, E, const_cast<DeclContext*>(DC));
diagAvailability(E, const_cast<DeclContext*>(DC));
if (TC.Context.LangOpts.EnableObjCInterop)
diagDeprecatedObjCSelectors(TC, DC, E);
}
Expand Down
13 changes: 8 additions & 5 deletions lib/Sema/ResilienceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
TreatUsableFromInlineAsPublic).isPublic())
return false;

auto &Context = DC->getASTContext();

// Dynamic declarations were mistakenly not checked in Swift 4.2.
// Do enforce the restriction even in pre-Swift-5 modes if the module we're
// building is resilient, though.
Expand Down Expand Up @@ -194,18 +196,19 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
if (downgradeToWarning == DowngradeToWarning::Yes)
diagID = diag::resilience_decl_unavailable_warn;

diagnose(loc, diagID,
Context.Diags.diagnose(
loc, diagID,
D->getDescriptiveKind(), diagName,
D->getFormalAccessScope().accessLevelForDiagnostics(),
static_cast<unsigned>(Kind),
isAccessor);

if (TreatUsableFromInlineAsPublic) {
diagnose(D, diag::resilience_decl_declared_here,
D->getDescriptiveKind(), diagName, isAccessor);
Context.Diags.diagnose(D, diag::resilience_decl_declared_here,
D->getDescriptiveKind(), diagName, isAccessor);
} else {
diagnose(D, diag::resilience_decl_declared_here_public,
D->getDescriptiveKind(), diagName, isAccessor);
Context.Diags.diagnose(D, diag::resilience_decl_declared_here_public,
D->getDescriptiveKind(), diagName, isAccessor);
}

return (downgradeToWarning == DowngradeToWarning::No);
Expand Down
Loading