Skip to content

[CodeCompletion] Update for SE-0293 Property Wrappers for func params #37280

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 1 commit into from
May 7, 2021
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: 1 addition & 3 deletions include/swift/AST/NameLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,7 @@ class FindLocalVal : public StmtVisitor<FindLocalVal> {
VisibleDeclConsumer &Consumer)
: SM(SM), Loc(Loc), Consumer(Consumer) {}

void checkValueDecl(ValueDecl *D, DeclVisibilityKind Reason) {
Consumer.foundDecl(D, Reason);
}
void checkValueDecl(ValueDecl *D, DeclVisibilityKind Reason);

void checkPattern(const Pattern *Pat, DeclVisibilityKind Reason);

Expand Down
30 changes: 29 additions & 1 deletion lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2755,7 +2755,35 @@ void FindLocalVal::checkPattern(const Pattern *Pat, DeclVisibilityKind Reason) {
return;
}
}

void FindLocalVal::checkValueDecl(ValueDecl *D, DeclVisibilityKind Reason) {
if (!D)
return;
if (auto var = dyn_cast<VarDecl>(D)) {
auto dc = var->getDeclContext();
if ((isa<AbstractFunctionDecl>(dc) || isa<ClosureExpr>(dc)) &&
var->hasAttachedPropertyWrapper()) {
// FIXME: This is currently required to set the interface type of the
// auxiliary variables (unless 'var' is a closure param).
(void)var->getPropertyWrapperBackingPropertyType();

auto vars = var->getPropertyWrapperAuxiliaryVariables();
if (vars.backingVar) {
Consumer.foundDecl(vars.backingVar, Reason);
}
if (vars.projectionVar) {
Consumer.foundDecl(vars.projectionVar, Reason);
}
if (vars.localWrappedValueVar) {
Consumer.foundDecl(vars.localWrappedValueVar, Reason);

// If 'localWrappedValueVar' exists, the original var is shadowed.
return;
}
}
}
Consumer.foundDecl(D, Reason);
}

void FindLocalVal::checkParameterList(const ParameterList *params) {
for (auto param : *params) {
checkValueDecl(param, DeclVisibilityKind::FunctionParameter);
Expand Down
43 changes: 42 additions & 1 deletion test/IDE/complete_property_delegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SELF_VARNAME | %FileCheck %s -check-prefix=CONTEXT_VARNAME
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SELF_STORAGE_VARNAME | %FileCheck %s -check-prefix=CONTEXT_STORAGE_VARNAME

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PARAM | %FileCheck %s -check-prefix=PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PARAM_CLOSURE | %FileCheck %s -check-prefix=PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LOCAL | %FileCheck %s -check-prefix=LOCAL

@propertyWrapper
struct Lazzzy<T> {
var wrappedValue: T
var projectedValue: String { "" }

init(wrappedValue: T) { fatalError() }

func delegateOperation() -> Int {}
}

Expand All @@ -26,6 +34,7 @@ class MyClass {
let _ = #^CONTEXT^#
// CONTEXT: Begin completions
// CONTEXT-DAG: Decl[InstanceVar]/CurrNominal: foo[#MyMember#];
// CONTEXT-DAG: Decl[InstanceVar]/CurrNominal: $foo[#String#];
// CONTEXT-DAG: Decl[InstanceVar]/CurrNominal: _foo[#Lazzzy<MyMember>#];
// CONTEXT: End completions

Expand All @@ -38,9 +47,10 @@ class MyClass {
// CONTEXT_VARNAME-DAG: End completions

let _ = _foo.#^CONTEXT_STORAGE_VARNAME^#
// CONTEXT_STORAGE_VARNAME: Begin completions, 3 items
// CONTEXT_STORAGE_VARNAME: Begin completions, 4 items
// CONTEXT_STORAGE_VARNAME-DAG: Keyword[self]/CurrNominal: self[#Lazzzy<MyMember>#]; name=self
// CONTEXT_STORAGE_VARNAME-DAG: Decl[InstanceVar]/CurrNominal: wrappedValue[#MyMember#]; name=wrappedValue
// CONTEXT_STORAGE_VARNAME-DAG: Decl[InstanceVar]/CurrNominal: projectedValue[#String#]; name=projectedValue
// CONTEXT_STORAGE_VARNAME-DAG: Decl[InstanceMethod]/CurrNominal: delegateOperation()[#Int#]; name=delegateOperation()
// CONTEXT_STORAGE_VARNAME-NOT: _
// CONTEXT_STORAGE_VARNAME: End completions
Expand All @@ -55,3 +65,34 @@ class MyClass {
// Same as CONTEXT_STORAGE_VARNAME.
}
}

func paramTest(@Lazzzy arg: MyMember) {
#^PARAM^#
// PARAM: Begin completions
// PARAM-DAG: Decl[LocalVar]/Local: arg[#MyMember#]; name=arg
// PARAM-DAG: Decl[LocalVar]/Local: $arg[#String#]; name=$arg
// PARAM-DAG: Decl[LocalVar]/Local: _arg[#Lazzzy<MyMember>#]; name=_arg
// PARAM-DAG: Decl[FreeFunction]/CurrModule: paramTest({#arg: MyMember#})[#Void#]; name=paramTest(arg: MyMember)
// PARAM: End completions
}
func closureTest() {
func receive(fn: (MyMember) -> Void) {}

receive { (@Lazzzy arg: MyMember) in
#^PARAM_CLOSURE^#
// Same as PARAM
}
}

func localTest() {
@Lazzzy var local: MyMember = .zero

#^LOCAL^#
// LOCAL: Begin completions
// LOCAL-DAG: Decl[LocalVar]/Local: local[#MyMember#]; name=local
// LOCAL-DAG: Decl[LocalVar]/Local: $local[#String#]; name=$local
// LOCAL-DAG: Decl[LocalVar]/Local: _local[#Lazzzy<MyMember>#]; name=_local
// LOCAL-DAG: Decl[FreeFunction]/CurrModule: paramTest({#arg: MyMember#})[#Void#]; name=paramTest(arg: MyMember)
// LOCAL: End completions
}