Skip to content

[code-completion] Fix assertion failure in getValueExprCompletions #15471

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
98 changes: 55 additions & 43 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
std::vector<Type> ExpectedTypes;

bool HaveDot = false;
bool IsUnwrappedOptional = false;
SourceLoc DotLoc;
bool NeedLeadingDot = false;

Expand Down Expand Up @@ -1734,6 +1735,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
this->DotLoc = DotLoc;
}

void setIsUnwrappedOptional(bool value) {
IsUnwrappedOptional = value;
}

void setIsStaticMetatype(bool value) {
IsStaticMetatype = value;
}
Expand Down Expand Up @@ -3119,9 +3124,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return false;
}

void getTupleExprCompletions(TupleType *ExprType) {
bool tryTupleExprCompletions(Type ExprType) {
auto *TT = ExprType->getAs<TupleType>();
if (!TT)
return false;

unsigned Index = 0;
for (auto TupleElt : ExprType->getElements()) {
for (auto TupleElt : TT->getElements()) {
CodeCompletionResultBuilder Builder(
Sink,
CodeCompletionResult::ResultKind::Pattern,
Expand All @@ -3140,6 +3149,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
addTypeAnnotation(Builder, TupleElt.getType());
Index++;
}
return true;
}

bool tryFunctionCallCompletions(Type ExprType, const ValueDecl *VD) {
Expand All @@ -3155,7 +3165,24 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return false;
}

bool tryStdlibOptionalCompletions(Type ExprType, bool isIUO) {
bool tryModuleCompletions(Type ExprType) {
if (auto MT = ExprType->getAs<ModuleType>()) {
ModuleDecl *M = MT->getModule();
if (CurrDeclContext->getParentModule() != M) {
// Only use the cache if it is not the current module.
RequestedCachedResults = RequestedResultsTy::fromModule(M)
.needLeadingDot(needDot());
return true;
}
}
return false;
}

/// If the given ExprType is optional, this adds completions for the unwrapped
/// type.
///
/// \return true if the given type was Optional .
bool tryUnwrappedCompletions(Type ExprType, bool isIUO) {
// FIXME: consider types convertible to T?.

ExprType = ExprType->getRValueType();
Expand All @@ -3165,10 +3192,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
if (Type Unwrapped = ExprType->getOptionalObjectType()) {
lookupVisibleMemberDecls(*this, Unwrapped, CurrDeclContext,
TypeResolver.get(), IncludeInstanceMembers);
} else {
llvm_unreachable("IUOs should always be optionals.");
return true;
}
} else if (Type Unwrapped = ExprType->getOptionalObjectType()) {
assert(IsUnwrappedOptional && "IUOs should be optional if not bound/forced");
return false;
}

if (Type Unwrapped = ExprType->getOptionalObjectType()) {
llvm::SaveAndRestore<bool> ChangeNeedOptionalUnwrap(NeedOptionalUnwrap,
true);
if (DotLoc.isValid()) {
Expand All @@ -3179,26 +3209,15 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}
if (NumBytesToEraseForOptionalUnwrap <=
CodeCompletionResult::MaxNumBytesToErase) {
if (auto *TT = Unwrapped->getAs<TupleType>()) {
getTupleExprCompletions(TT);
} else {
if (!tryTupleExprCompletions(Unwrapped)) {
lookupVisibleMemberDecls(*this, Unwrapped, CurrDeclContext,
TypeResolver.get(),
IncludeInstanceMembers);
}
}
} else {
return false;
return true;
}

// Ignore the members of Optional, like getLogicValue(), map(), and
// flatMap().
//
// These are not commonly used and cause noise and confusion when showing
// among the members of the underlying type. If someone really wants to
// use them they can write them directly.

return true;
return false;
}

void getValueExprCompletions(Type ExprType, ValueDecl *VD = nullptr) {
Expand All @@ -3219,30 +3238,20 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}
}

bool Done = false;
// Handle special cases
bool isIUO = VD && VD->getAttrs()
.hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
if (tryFunctionCallCompletions(ExprType, VD))
Done = true;
if (auto MT = ExprType->getAs<ModuleType>()) {
ModuleDecl *M = MT->getModule();
if (CurrDeclContext->getParentModule() != M) {
// Only use the cache if it is not the current module.
RequestedCachedResults = RequestedResultsTy::fromModule(M)
.needLeadingDot(needDot());
Done = true;
}
}
if (auto *TT = ExprType->getAs<TupleType>()) {
getTupleExprCompletions(TT);
Done = true;
}
bool isIUO =
VD && VD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
tryStdlibOptionalCompletions(ExprType, isIUO);
if (!Done) {
lookupVisibleMemberDecls(*this, ExprType, CurrDeclContext,
TypeResolver.get(),
IncludeInstanceMembers);
}
return;
if (tryModuleCompletions(ExprType))
return;
if (tryTupleExprCompletions(ExprType))
return;
// Don't check/return so we still add the members of Optional itself below
tryUnwrappedCompletions(ExprType, isIUO);

lookupVisibleMemberDecls(*this, ExprType, CurrDeclContext,
TypeResolver.get(), IncludeInstanceMembers);
}

template <typename T>
Expand Down Expand Up @@ -5282,6 +5291,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
if (isDynamicLookup(*ExprType))
Lookup.setIsDynamicLookup();

if (isa<BindOptionalExpr>(ParsedExpr) || isa<ForceValueExpr>(ParsedExpr))
Lookup.setIsUnwrappedOptional(true);

::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext, ParsedExpr);
llvm::SmallVector<Type, 2> PossibleTypes;
if (TypeAnalyzer.Analyze(PossibleTypes)) {
Expand Down
24 changes: 24 additions & 0 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EMPTY_OVERLOAD_1 | %FileCheck %s -check-prefix=EMPTY_OVERLOAD
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EMPTY_OVERLOAD_2 | %FileCheck %s -check-prefix=EMPTY_OVERLOAD

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CALLARG_IUO | %FileCheck %s -check-prefix=CALLARG_IUO
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=BOUND_IUO | %FileCheck %s -check-prefix=MEMBEROF_IUO
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=FORCED_IUO | %FileCheck %s -check-prefix=MEMBEROF_IUO

var i1 = 1
var i2 = 2
var oi1 : Int?
Expand Down Expand Up @@ -383,3 +387,23 @@ _ = EmptyOverload(foo: #^EMPTY_OVERLOAD_2^#)
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local{{.*}}: i2[#Int#];
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local{{.*}}: i1[#Int#];
// EMPTY_OVERLOAD: End completions

public func fopen() -> TestBoundGeneric1! { fatalError() }
func other() {
_ = fopen(#^CALLARG_IUO^#)
// CALLARG_IUO-NOT: Begin completions
// CALLARG_IUO-NOT: End completions
}

class Foo { let x: Int }
class Bar {
var collectionView: Foo!

func foo() {
self.collectionView? .#^BOUND_IUO^#x
self.collectionView! .#^FORCED_IUO^#x
}
// MEMBEROF_IUO: Begin completions, 1 items
// MEMBEROF_IUO: Decl[InstanceVar]/CurrNominal: x[#Int#]; name=x
// MEMBEROF_IUO: End completions
}