Skip to content

[CodeCompletion] Only include results from other modules once #42529

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
35 changes: 34 additions & 1 deletion include/swift/IDE/CompletionLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
static RequestedResultsTy toplevelResults() {
return {nullptr, false, false, false, true};
}

friend bool operator==(const RequestedResultsTy &LHS,
const RequestedResultsTy &RHS) {
return LHS.TheModule == RHS.TheModule && LHS.OnlyTypes == RHS.OnlyTypes &&
LHS.OnlyPrecedenceGroups == RHS.OnlyPrecedenceGroups &&
LHS.NeedLeadingDot == RHS.NeedLeadingDot &&
LHS.IncludeModuleQualifier == RHS.IncludeModuleQualifier;
}
};

std::vector<RequestedResultsTy> RequestedCachedResults;
llvm::SetVector<RequestedResultsTy> RequestedCachedResults;

public:
CompletionLookup(CodeCompletionResultSink &Sink, ASTContext &Ctx,
Expand Down Expand Up @@ -598,4 +606,29 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
} // end namespace ide
} // end namespace swift

namespace llvm {
using RequestedResultsTy = swift::ide::CompletionLookup::RequestedResultsTy;
template <>
struct DenseMapInfo<RequestedResultsTy> {
static inline RequestedResultsTy getEmptyKey() {
return {DenseMapInfo<swift::ModuleDecl *>::getEmptyKey(), false, false,
false, false};
}
static inline RequestedResultsTy getTombstoneKey() {
return {DenseMapInfo<swift::ModuleDecl *>::getTombstoneKey(), false, false,
false, false};
}
static unsigned getHashValue(const RequestedResultsTy &Val) {
return hash_combine(
DenseMapInfo<swift::ModuleDecl *>::getHashValue(Val.TheModule),
Val.OnlyTypes, Val.OnlyPrecedenceGroups, Val.NeedLeadingDot,
Val.IncludeModuleQualifier);
}
static bool isEqual(const RequestedResultsTy &LHS,
const RequestedResultsTy &RHS) {
return LHS == RHS;
}
};
} // namespace llvm

#endif // SWIFT_IDE_COMPLETIONLOOKUP_H
12 changes: 6 additions & 6 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,7 @@ bool CompletionLookup::tryModuleCompletions(Type ExprType, bool TypesOnly) {
.withModuleQualifier(false);
if (TypesOnly)
Request = Request.onlyTypes();
RequestedCachedResults.push_back(Request);
RequestedCachedResults.insert(Request);
}
return true;
}
Expand Down Expand Up @@ -2709,7 +2709,7 @@ void CompletionLookup::getValueCompletionsInDeclContext(SourceLoc Loc,

lookupVisibleDecls(FilteringConsumer, CurrDeclContext,
/*IncludeTopLevel=*/false, Loc);
RequestedCachedResults.push_back(
RequestedCachedResults.insert(
RequestedResultsTy::toplevelResults().withModuleQualifier(
ModuleQualifier));

Expand Down Expand Up @@ -3013,9 +3013,9 @@ void CompletionLookup::collectPrecedenceGroups() {
if (Module == CurrModule)
continue;

RequestedCachedResults.push_back(RequestedResultsTy::fromModule(Module)
.onlyPrecedenceGroups()
.withModuleQualifier(false));
RequestedCachedResults.insert(RequestedResultsTy::fromModule(Module)
.onlyPrecedenceGroups()
.withModuleQualifier(false));
}
}

Expand Down Expand Up @@ -3105,7 +3105,7 @@ void CompletionLookup::getTypeCompletionsInDeclContext(SourceLoc Loc,
lookupVisibleDecls(AccessFilteringConsumer, CurrDeclContext,
/*IncludeTopLevel=*/false, Loc);

RequestedCachedResults.push_back(
RequestedCachedResults.insert(
RequestedResultsTy::toplevelResults().onlyTypes().withModuleQualifier(
ModuleQualifier));
}
Expand Down
28 changes: 28 additions & 0 deletions test/IDE/complete_from_swift_module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
// rdar://15305873 Code completion: implement proper shadowing of declarations represented by cached results
// FIXME: %FileCheck %s -check-prefix=TOP_LEVEL_1_NEGATIVE < %t.compl.txt

// RUN: %target-swift-ide-test -code-completion -source-filename %s -I %t -code-completion-token=AMBIGOUS_RESULT_BUILER > %t.compl.txt
// RUN: %FileCheck %s -check-prefix=AMBIGOUS_RESULT_BUILER < %t.compl.txt

// ERROR_COMMON: found code completion token
// ERROR_COMMON-NOT: Begin completions

Expand Down Expand Up @@ -161,3 +164,28 @@ func foo() -> foo_swift_module.#^MODULE_TYPE_QUALIFIED^# {}
// MODULE_TYPE_QUALIFIED: Decl[Struct]/OtherModule[foo_swift_module]: FooSwiftStruct[#FooSwiftStruct#]; name=FooSwiftStruct
// MODULE_TYPE_QUALIFIED: Decl[Struct]/OtherModule[foo_swift_module]: BarGenericSwiftStruct2[#BarGenericSwiftStruct2#]; name=BarGenericSwiftStruct2
// MODULE_TYPE_QUALIFIED: End completions

// rdar://92048610
func testAmbiguousResultBuilder() {
@resultBuilder
struct MyBuilder {
static func buildBlock(_ x: Int) -> Int {}
}

struct Foo {
init(arg: Int = 1, @MyBuilder content: () -> Int) {}
init(arg: Int = 1) {}
}

func test() {
Foo {
#^AMBIGOUS_RESULT_BUILER^#
}
// Results should only contain globalVar once
// AMBIGOUS_RESULT_BUILER: Begin completions
// AMBIGOUS_RESULT_BUILER-NOT: globalVar
// AMBIGOUS_RESULT_BUILER-DAG: Decl[GlobalVar]/OtherModule[foo_swift_module]: globalVar[#Int#]; name=globalVar
// AMBIGOUS_RESULT_BUILER-NOT: globalVar
// AMBIGOUS_RESULT_BUILER: End completions
}
}