Skip to content

[IDE][Sema] Fix code completion unreachable/hang in TypeBase::getContextSubtitutions #26799

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
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: 3 additions & 1 deletion lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,9 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
continue;
}

llvm_unreachable("Bad base type");
// Assert and break to avoid hanging if we get an unexpected baseTy.
assert(0 && "Bad base type");
break;
}

while (n > 0) {
Expand Down
12 changes: 9 additions & 3 deletions lib/Sema/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,14 @@ static void lookupDeclsFromProtocolsBeingConformedTo(
continue;

// Skip unsatisfied conditional conformances.
if (Conformance->getConditionalRequirementsIfAvailable() &&
!Module->conformsToProtocol(BaseTy, Proto))
continue;
// We can't check them if this type has an UnboundGenericType or if they
// couldn't be computed, so assume they conform in such cases.
if (!BaseTy->hasUnboundGenericType()) {
if (auto res = Conformance->getConditionalRequirementsIfAvailable()) {
if (!res->empty() && !Module->conformsToProtocol(BaseTy, Proto))
continue;
}
}

DeclVisibilityKind ReasonForThisProtocol;
if (Reason == DeclVisibilityKind::MemberOfCurrentNominal)
Expand Down Expand Up @@ -782,6 +787,7 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
// don't substitute either.
bool shouldSubst = (Reason != DeclVisibilityKind::DynamicLookup &&
!BaseTy->isAnyObject() && !BaseTy->hasTypeVariable() &&
!BaseTy->hasUnboundGenericType() &&
(BaseTy->getNominalOrBoundGenericNominal() ||
BaseTy->is<ArchetypeType>()) &&
VD->getDeclContext()->isTypeContext());
Expand Down
36 changes: 36 additions & 0 deletions test/IDE/complete_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_LOCAL_RETURN | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_DOT_1 | %FileCheck %s -check-prefix=PROTOCOL_DOT_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=UNBOUND_DOT | %FileCheck %s -check-prefix=UNBOUND_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=UNBOUND_DOT_2 | %FileCheck %s -check-prefix=UNBOUND_DOT_2

//===--- Helper types that are used in this test

Expand Down Expand Up @@ -1093,3 +1095,37 @@ func testProtocol() {
// PROTOCOL_DOT_1-DAG: Keyword/None: Type[#FooProtocol.Type#]; name=Type
// PROTOCOL_DOT_1: End completions
}

//===---
//===--- Test we can complete unbound generic types
//===---

public final class Task<Success> {
public enum Inner {
public typealias Failure = Int
case success(Success)
case failure(Failure)
}
}
extension Task.Inner {
public init(left error: Failure) {
fatalError()
}
}
extension Task.Inner.#^UNBOUND_DOT^# {}
func testUnbound(x: Task.Inner.#^UNBOUND_DOT^#) {}
// UNBOUND_DOT: Begin completions
// UNBOUND_DOT-DAG: Decl[TypeAlias]/CurrNominal: Failure[#Int#]; name=Failure
// UNBOUND_DOT-DAG: Keyword/None: Type[#Task.Inner.Type#]; name=Type
// UNBOUND_DOT: End completions


protocol MyProtocol {}
struct OuterStruct<U> {
class Inner<V>: MyProtocol {}
}

func testUnbound2(x: OuterStruct<Int>.Inner.#^UNBOUND_DOT_2^#) {}
// UNBOUND_DOT_2: Begin completions
// UNBOUND_DOT_2-DAG: Keyword/None: Type[#OuterStruct<Int>.Inner.Type#]; name=Type
// UNBOUND_DOT_2: End completions