Skip to content

Sema: Improved recovery from circular generic signature construction #39174

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 1 commit into from
Sep 4, 2021
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,10 @@ ERROR(requires_generic_param_made_equal_to_concrete,none,
(Type))
ERROR(recursive_decl_reference,none,
"%0 %1 references itself", (DescriptiveDeclKind, DeclBaseName))
ERROR(recursive_generic_signature,none,
"%0 %1 has self-referential generic requirements", (DescriptiveDeclKind, DeclBaseName))
ERROR(recursive_generic_signature_extension,none,
"extension of %0 %1 has self-referential generic requirements", (DescriptiveDeclKind, DeclBaseName))
ERROR(recursive_same_type_constraint,none,
"same-type constraint %0 == %1 is recursive", (Type, Type))
ERROR(recursive_superclass_constraint,none,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,8 @@ class GenericSignatureRequest :
bool isCached() const { return true; }
Optional<GenericSignature> getCachedResult() const;
void cacheResult(GenericSignature value) const;

void diagnoseCycle(DiagnosticEngine &diags) const;
};

/// Compute the underlying interface type of a typealias.
Expand Down
43 changes: 40 additions & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,47 @@ bool GenericContext::isComputingGenericSignature() const {
GenericSignatureRequest{const_cast<GenericContext*>(this)});
}

/// If we hit a cycle while building the generic signature, we can't return
/// nullptr, since this breaks invariants elsewhere. Instead, build a dummy
/// signature with no requirements.
static GenericSignature getPlaceholderGenericSignature(
const DeclContext *DC) {
SmallVector<GenericParamList *, 2> gpLists;
DC->forEachGenericContext([&](GenericParamList *genericParams) {
gpLists.push_back(genericParams);
});

if (gpLists.empty())
return nullptr;

std::reverse(gpLists.begin(), gpLists.end());
for (unsigned i : indices(gpLists))
gpLists[i]->setDepth(i);

SmallVector<GenericTypeParamType *, 2> result;
for (auto *genericParams : gpLists) {
for (auto *genericParam : *genericParams) {
result.push_back(genericParam->getDeclaredInterfaceType()
->castTo<GenericTypeParamType>());
}
}

return GenericSignature::get(result, {});
}

GenericSignature GenericContext::getGenericSignature() const {
return evaluateOrDefault(
getASTContext().evaluator,
GenericSignatureRequest{const_cast<GenericContext *>(this)}, nullptr);
// Don't use evaluateOrDefault() here, because getting the 'default value'
// is slightly expensive here so we don't want to do it eagerly.
auto result = getASTContext().evaluator(
GenericSignatureRequest{const_cast<GenericContext *>(this)});
if (auto err = result.takeError()) {
llvm::handleAllErrors(std::move(err),
[](const CyclicalRequestError<GenericSignatureRequest> &E) {
// cycle detected
});
return getPlaceholderGenericSignature(this);
}
return *result;
}

GenericEnvironment *GenericContext::getGenericEnvironment() const {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void DeclContext::forEachGenericContext(
if (auto *gpList = genericCtx->getGenericParams())
fn(gpList);
}
} while ((dc = dc->getParent()));
} while ((dc = dc->getParentForLookup()));
}

unsigned DeclContext::getGenericContextDepth() const {
Expand Down
17 changes: 12 additions & 5 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3906,11 +3906,18 @@ bool GenericSignatureBuilder::addGenericParameterRequirements(
void GenericSignatureBuilder::addGenericParameter(GenericTypeParamType *GenericParam) {
auto params = getGenericParams();
(void)params;
assert(params.empty() ||
((GenericParam->getDepth() == params.back()->getDepth() &&
GenericParam->getIndex() == params.back()->getIndex() + 1) ||
(GenericParam->getDepth() > params.back()->getDepth() &&
GenericParam->getIndex() == 0)));

#ifndef NDEBUG
if (params.empty()) {
assert(GenericParam->getDepth() == 0);
assert(GenericParam->getIndex() == 0);
} else {
assert((GenericParam->getDepth() == params.back()->getDepth() &&
GenericParam->getIndex() == params.back()->getIndex() + 1) ||
(GenericParam->getDepth() > params.back()->getDepth() &&
GenericParam->getIndex() == 0));
}
#endif

// Create a potential archetype for this type parameter.
auto PA = new (Impl->Allocator) PotentialArchetype(GenericParam);
Expand Down
16 changes: 16 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,22 @@ void GenericSignatureRequest::cacheResult(GenericSignature value) const {
GC->GenericSigAndBit.setPointerAndInt(value, true);
}

void GenericSignatureRequest::diagnoseCycle(DiagnosticEngine &diags) const {
auto *GC = std::get<0>(getStorage());
auto *D = GC->getAsDecl();

if (auto *VD = dyn_cast<ValueDecl>(D)) {
VD->diagnose(diag::recursive_generic_signature,
VD->getDescriptiveKind(), VD->getBaseName());
} else {
auto *ED = cast<ExtensionDecl>(D);
auto *NTD = ED->getExtendedNominal();

ED->diagnose(diag::recursive_generic_signature_extension,
NTD->getDescriptiveKind(), NTD->getName());
}
}

//----------------------------------------------------------------------------//
// InferredGenericSignatureRequest computation.
//----------------------------------------------------------------------------//
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ static Type formExtensionInterfaceType(
if (!nominal->isGeneric() || isa<ProtocolDecl>(nominal)) {
resultType = NominalType::get(nominal, parentType,
nominal->getASTContext());
} else {
} else if (genericParams) {
auto currentBoundType = type->getAs<BoundGenericType>();

// Form the bound generic type with the type parameters provided.
Expand Down
18 changes: 0 additions & 18 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,6 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,
return true;
}

auto &ctx = dc->getASTContext();

SourceLoc noteLoc;
{
// We are interested in either a contextual where clause or
Expand All @@ -610,14 +608,6 @@ bool TypeChecker::checkContextualRequirements(GenericTypeDecl *decl,

const auto subMap = parentTy->getContextSubstitutions(decl->getDeclContext());
const auto genericSig = decl->getGenericSignature();
if (!genericSig) {
if (loc.isValid()) {
ctx.Diags.diagnose(loc, diag::recursive_decl_reference,
decl->getDescriptiveKind(), decl->getName());
decl->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
}
return false;
}

const auto result =
TypeChecker::checkGenericArguments(
Expand Down Expand Up @@ -765,14 +755,6 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
}
}

// FIXME: More principled handling of circularity.
if (!decl->getGenericSignature()) {
diags.diagnose(loc, diag::recursive_decl_reference,
decl->getDescriptiveKind(), decl->getName());
decl->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
return ErrorType::get(ctx);
}

// Resolve the types of the generic arguments.
auto genericResolution =
resolution.withOptions(adjustOptionsForGenericArgs(options));
Expand Down
6 changes: 3 additions & 3 deletions test/Generics/generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ var y: X5<X4, Int> // expected-error{{'X5' requires the types 'X4.AssocP' (aka '
// Recursive generic signature validation.
class Top {}
class Bottom<T : Bottom<Top>> {}
// expected-error@-1 {{generic class 'Bottom' references itself}}
// expected-note@-2 {{type declared here}}
// expected-error@-3 {{circular reference}}
// expected-error@-1 {{'Bottom' requires that 'Top' inherit from 'Bottom<Top>'}}
// expected-note@-2 {{requirement specified as 'T' : 'Bottom<Top>' [with T = Top]}}
// expected-error@-3 {{generic class 'Bottom' has self-referential generic requirements}}
// expected-note@-4 {{while resolving type 'Bottom<Top>'}}
// expected-note@-5 {{through reference here}}

Expand Down
16 changes: 6 additions & 10 deletions test/decl/protocol/req/recursion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ public protocol P {
associatedtype T
}

public struct S<A: P> where A.T == S<A> { // expected-error {{circular reference}}
// expected-note@-1 {{type declared here}}
// expected-error@-2 {{generic struct 'S' references itself}}
// expected-note@-3 {{while resolving type 'S<A>'}}
public struct S<A: P> where A.T == S<A> {
// expected-error@-1 {{generic struct 'S' has self-referential generic requirements}}
// expected-note@-2 {{while resolving type 'S<A>'}}
func f(a: A.T) {
g(a: id(t: a)) // `a` has error type which is diagnosed as circular reference
// expected-error@-1 {{conflicting arguments to generic parameter 'T' ('A.T' (associated type of protocol 'P') vs. 'S<A>')}}
_ = A.T.self
}

func g(a: S<A>) {
f(a: id(t: a))
// expected-error@-1 {{conflicting arguments to generic parameter 'T' ('S<A>' vs. 'A.T' (associated type of protocol 'P'))}}
_ = S<A>.self
}

Expand All @@ -72,10 +69,9 @@ protocol PI {
associatedtype T : I
}

struct SI<A: PI> : I where A : I, A.T == SI<A> { // expected-error {{circular reference}}
// expected-note@-1 {{type declared here}}
// expected-error@-2 {{generic struct 'SI' references itself}}
// expected-note@-3 {{while resolving type 'SI<A>'}}
struct SI<A: PI> : I where A : I, A.T == SI<A> {
// expected-error@-1 {{generic struct 'SI' has self-referential generic requirements}}
// expected-note@-2 {{while resolving type 'SI<A>'}}
func ggg<T : I>(t: T.Type) -> T {
return T()
}
Expand Down
14 changes: 5 additions & 9 deletions validation-test/compiler_crashers_2_fixed/0161-sr6569.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ protocol P {

struct Type<Param> {}
extension Type: P where Param: P, Param.A == Type<Param> {
// expected-error@-1 {{circular reference}}
// expected-note@-2 {{through reference here}}
// expected-error@-3 {{circular reference}}
// expected-note@-4 {{through reference here}}
// expected-error@-5 {{circular reference}}
// expected-note@-6 {{through reference here}}
// expected-error@-7 {{type 'Type<Param>' does not conform to protocol 'P'}}
// expected-error@-1 5{{extension of generic struct 'Type' has self-referential generic requirements}}
// expected-note@-2 5{{through reference here}}
// expected-error@-3 {{type 'Type<Param>' does not conform to protocol 'P'}}
typealias A = Param
// expected-note@-1 {{through reference here}}
// expected-note@-2 {{through reference here}}
// expected-note@-1 2{{through reference here}}
// expected-note@-2 {{possibly intended match 'Type<Param>.A' (aka 'Param') does not conform to 'P'}}
}
9 changes: 5 additions & 4 deletions validation-test/compiler_crashers_2_fixed/0163-sr8033.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct Foo<T> {}
protocol P1 {
associatedtype A // expected-note {{protocol requires nested type 'A'; do you want to add it?}}
}
extension Foo: P1 where A : P1 {} // expected-error {{circular reference}}
// expected-note@-1 {{while resolving type 'A'}}
// expected-note@-2 {{through reference here}}
// expected-error@-3 {{type 'Foo<T>' does not conform to protocol 'P1'}}
extension Foo: P1 where A : P1 {}
// expected-error@-1 {{extension of generic struct 'Foo' has self-referential generic requirements}}
// expected-note@-2 {{while resolving type 'A'}}
// expected-note@-3 {{through reference here}}
// expected-error@-4 {{type 'Foo<T>' does not conform to protocol 'P1'}}