Skip to content

[CodeCompletion] Do not include decl in own (string interp) initializer #34548

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
7 changes: 4 additions & 3 deletions include/swift/AST/NameLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,14 @@ class AccessFilteringDeclConsumer final : public VisibleDeclConsumer {
/// a decl inside its own initializer or a non-type decl before its definition.
class UsableFilteringDeclConsumer final : public VisibleDeclConsumer {
const SourceManager &SM;
const DeclContext *DC;
SourceLoc Loc;
VisibleDeclConsumer &ChainedConsumer;

public:
UsableFilteringDeclConsumer(const SourceManager &SM, SourceLoc loc,
VisibleDeclConsumer &consumer)
: SM(SM), Loc(loc), ChainedConsumer(consumer) {}
UsableFilteringDeclConsumer(const SourceManager &SM, const DeclContext *DC,
SourceLoc loc, VisibleDeclConsumer &consumer)
: SM(SM), DC(DC), Loc(loc), ChainedConsumer(consumer) {}

void foundDecl(ValueDecl *D, DeclVisibilityKind reason,
DynamicLookupInfo dynamicLookupInfo) override;
Expand Down
24 changes: 18 additions & 6 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//
//===----------------------------------------------------------------------===//

#include "clang/AST/DeclObjC.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTVisitor.h"
Expand All @@ -31,9 +30,11 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Parse/Lexer.h"
#include "clang/AST/DeclObjC.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -148,10 +149,21 @@ void UsableFilteringDeclConsumer::foundDecl(ValueDecl *D,
DeclVisibilityKind reason, DynamicLookupInfo dynamicLookupInfo) {
// Skip when Loc is within the decl's own initializer
if (auto *VD = dyn_cast<VarDecl>(D)) {
if (auto *init = VD->getParentInitializer()) {
auto initRange = init->getSourceRange();
if (initRange.isValid() && SM.rangeContainsTokenLoc(initRange, Loc))
return;
Expr *init = VD->getParentInitializer();
if (auto *PD = dyn_cast<ParamDecl>(D)) {
init = PD->getStructuralDefaultExpr();
}

// Only check if the VarDecl has the same (or parent) context to avoid
// grabbing the end location for every decl with an initializer
if (init != nullptr) {
auto *varContext = VD->getDeclContext();
if (DC == varContext || DC->isChildContextOf(varContext)) {
auto initRange = Lexer::getCharSourceRangeFromSourceRange(
SM, init->getSourceRange());
if (initRange.isValid() && initRange.contains(Loc))
return;
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4652,8 +4652,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
: LookupKind::ValueInDeclContext;
NeedLeadingDot = false;

UsableFilteringDeclConsumer UsableFilteringConsumer(Ctx.SourceMgr,
Ctx.SourceMgr.getCodeCompletionLoc(), *this);
UsableFilteringDeclConsumer UsableFilteringConsumer(
Ctx.SourceMgr, CurrDeclContext, Ctx.SourceMgr.getCodeCompletionLoc(),
*this);
AccessFilteringDeclConsumer AccessFilteringConsumer(
CurrDeclContext, UsableFilteringConsumer);

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
return;
}
UsableFilteringDeclConsumer FilteringConsumer(DC->getASTContext().SourceMgr,
Loc, Consumer);
DC, Loc, Consumer);
lookupVisibleDeclsImpl(FilteringConsumer, DC, IncludeTopLevel, Loc);
}

Expand Down
26 changes: 24 additions & 2 deletions test/IDE/complete_expr_postfix_begin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_TUPLE_1 | %FileCheck %s -check-prefix=IN_TUPLE_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_TUPLE_2 | %FileCheck %s -check-prefix=IN_TUPLE_2

// RUN-FIXME(rdar56755598): %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_1 | %FileCheck %s -check-prefix=OWN_INIT_1
// RUN-FIXME(rdar56755598): %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_2 | %FileCheck %s -check-prefix=OWN_INIT_2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_1 | %FileCheck %s -check-prefix=OWN_INIT_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_2 | %FileCheck %s -check-prefix=OWN_INIT_2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_3 | %FileCheck %s -check-prefix=OWN_INIT_3
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_4 | %FileCheck %s -check-prefix=OWN_INIT_4
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_5 | %FileCheck %s -check-prefix=OWN_INIT_5
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_6 | %FileCheck %s -check-prefix=OWN_INIT_6
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_7 | %FileCheck %s -check-prefix=OWN_INIT_7
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_8 | %FileCheck %s -check-prefix=OWN_INIT_8
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_9 | %FileCheck %s -check-prefix=OWN_INIT_9
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_10 | %FileCheck %s -check-prefix=OWN_INIT_10
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_11 | %FileCheck %s -check-prefix=OWN_INIT_11
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_12 | %FileCheck %s -check-prefix=OWN_INIT_12

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_ACCESSOR_1 | %FileCheck %s -check-prefix=OWN_ACCESSOR_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_ACCESSOR_2 | %FileCheck %s -check-prefix=OWN_ACCESSOR_2
Expand Down Expand Up @@ -584,13 +589,19 @@ func sync() {}
var ownInit2: () -> Void = { #^OWN_INIT_2^# }
// OWN_INIT_2: Begin completions
// OWN_INIT_2-NOT: ownInit2
var ownInit8: Int = "\(#^OWN_INIT_8^#)"
// OWN_INIT_8: Begin completions
// OWN_INIT_8-NOT: ownInit8
struct OwnInitTester {
var ownInit3: Int = #^OWN_INIT_3^#
// OWN_INIT_3: Begin completions
// OWN_INIT_3-NOT: ownInit3
var ownInit4: () -> Void = { #^OWN_INIT_4^# }
// OWN_INIT_4: Begin completions
// OWN_INIT_4-NOT: ownInit4
var ownInit9: String = "\(#^OWN_INIT_9^#)"
// OWN_INIT_9: Begin completions
// OWN_INIT_9-NOT: ownInit9
}
func ownInitTesting() {
var ownInit5: Int = #^OWN_INIT_5^#
Expand All @@ -599,6 +610,17 @@ func ownInitTesting() {
var ownInit6: () -> Void = { #^OWN_INIT_6^# }
// OWN_INIT_6: Begin completions
// OWN_INIT_6-NOT: ownInit6
var ownInit10: String = "\(#^OWN_INIT_10^#)"
// OWN_INIT_10: Begin completions
// OWN_INIT_10-NOT: ownInit10
}
func ownInitTestingParam(ownInit11: Int = #^OWN_INIT_11^#) {
// OWN_INIT_11: Begin completions
// OWN_INIT_11-NOT: Decl[LocalVar]{{.*}}ownInit11
}
func ownInitTestingParamInterp(ownInit12: String = "\(#^OWN_INIT_12^#)") {
// OWN_INIT_12: Begin completions
// OWN_INIT_12-NOT: Decl[LocalVar]{{.*}}ownInit12
}
func ownInitTestingShadow(ownInit7: Int) {
var ownInit7: Int = #^OWN_INIT_7^#
Expand Down