Skip to content

Sema: Fix crash on circular reference in checkContextualRequirements() #32776

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 2 commits into from
Jul 9, 2020
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
11 changes: 10 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ static Type checkContextualRequirements(Type type,
return type;
}

auto &ctx = dc->getASTContext();

SourceLoc noteLoc;
{
// We are interested in either a contextual where clause or
Expand All @@ -637,6 +639,13 @@ static Type checkContextualRequirements(Type type,

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

const auto result =
TypeChecker::checkGenericArguments(
dc, loc, noteLoc, type,
Expand All @@ -647,7 +656,7 @@ static Type checkContextualRequirements(Type type,
switch (result) {
case RequirementCheckResult::Failure:
case RequirementCheckResult::SubstitutionFailure:
return ErrorType::get(dc->getASTContext());
return ErrorType::get(ctx);
case RequirementCheckResult::Success:
return type;
}
Expand Down
15 changes: 15 additions & 0 deletions validation-test/compiler_crashers_2_fixed/rdar64759168.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -emit-ir %s

public class Clazz {}

public protocol SelectableFieldValueProtocol {}

public protocol FieldProtocol {
associatedtype SelectableValue : SelectableFieldValueProtocol
}

public protocol SelectFieldValueCoordinatorDelegate {
associatedtype Field : Clazz, FieldProtocol
}

public class SelectFieldValueCoordinator<Field, Delegate : SelectFieldValueCoordinatorDelegate> where Field == Delegate.Field {}
12 changes: 12 additions & 0 deletions validation-test/compiler_crashers_2_fixed/rdar64992293.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: not %target-swift-frontend -typecheck %s

public protocol SomeProtocol {}

public struct Impl<Param>: SomeProtocol where Param: SomeProtocol {}

public struct Wrapper<Content> where Content: SomeProtocol {}

public extension Wrapper where Content == Impl<WrapperParam> {
typealias WrapperParam = SomeProtocol
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly more reduced:

protocol SomeProtocol {}
struct G<T> {}

struct Wrapper<Content: SomeProtocol> {}

extension Wrapper where Content == G<Wrapper.WrapperParam> {
  typealias WrapperParam = Never
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original test case came from a radar. I like to keep those mostly as-is, in case they uncover more bugs in the future.