-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CodeCompletion] Suggest synthesized declarations from macros #66821
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,8 @@ void UsableFilteringDeclConsumer::foundDecl(ValueDecl *D, | |
if (auto *contextD = DC->getAsDecl()) | ||
tmpLoc = contextD->getStartLoc(); | ||
} | ||
if (!SM.isBeforeInBuffer(D->getLoc(), Loc)) | ||
auto declLoc = DC->getParentModule()->getOriginalLocation(D->getLoc()).second; | ||
if (!SM.isBeforeInBuffer(declLoc, tmpLoc)) | ||
return; | ||
} | ||
|
||
|
@@ -3875,20 +3876,48 @@ void FindLocalVal::visitBraceStmt(BraceStmt *S, bool isTopLevelCode) { | |
return; | ||
} | ||
|
||
// Visit inner statements first before reporting local decls in the current | ||
// scope. | ||
for (auto elem : S->getElements()) { | ||
// If we have a SingleValueStmtExpr, there may be local bindings in the | ||
// wrapped statement. | ||
if (auto *E = elem.dyn_cast<Expr *>()) { | ||
if (auto *SVE = dyn_cast<SingleValueStmtExpr>(E)) | ||
visit(SVE->getStmt()); | ||
continue; | ||
} | ||
if (auto *S = elem.dyn_cast<Stmt*>()) | ||
|
||
if (auto *S = elem.dyn_cast<Stmt*>()) { | ||
visit(S); | ||
continue; | ||
} | ||
} | ||
for (auto elem : S->getElements()) { | ||
if (auto *D = elem.dyn_cast<Decl*>()) { | ||
|
||
auto visitDecl = [&](Decl *D) { | ||
if (auto *VD = dyn_cast<ValueDecl>(D)) | ||
checkValueDecl(VD, DeclVisibilityKind::LocalVariable); | ||
D->visitAuxiliaryDecls([&](Decl *D) { | ||
if (auto *VD = dyn_cast<ValueDecl>(D)) | ||
checkValueDecl(VD, DeclVisibilityKind::LocalVariable); | ||
// FIXME: Recursively call `visitDecl` to handle nested macros. | ||
}); | ||
}; | ||
|
||
for (auto elem : S->getElements()) { | ||
if (auto *E = elem.dyn_cast<Expr *>()) { | ||
// 'MacroExpansionExpr' at code-item position may introduce value decls. | ||
// NOTE: the expression must be type checked. | ||
// FIXME: In code-completion local expressions are _not_ type checked. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we get completions from macro expansion declarations if the corresponding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't. So we need to typecheck |
||
if (auto *mee = dyn_cast<MacroExpansionExpr>(E)) { | ||
if (auto *med = mee->getSubstituteDecl()) { | ||
visitDecl(med); | ||
} | ||
} | ||
continue; | ||
} | ||
if (auto *D = elem.dyn_cast<Decl*>()) { | ||
visitDecl(D); | ||
continue; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,17 +217,24 @@ static void collectVisibleMemberDecls(const DeclContext *CurrDC, LookupState LS, | |
Type BaseType, | ||
IterableDeclContext *Parent, | ||
SmallVectorImpl<ValueDecl *> &FoundDecls) { | ||
for (auto Member : Parent->getMembers()) { | ||
auto *VD = dyn_cast<ValueDecl>(Member); | ||
auto check = [&](Decl *decl) { | ||
auto *VD = dyn_cast<ValueDecl>(decl); | ||
if (!VD) | ||
continue; | ||
return; | ||
if (!isDeclVisibleInLookupMode(VD, LS, CurrDC)) | ||
continue; | ||
return; | ||
if (!evaluateOrDefault(CurrDC->getASTContext().evaluator, | ||
IsDeclApplicableRequest(DeclApplicabilityOwner(CurrDC, BaseType, VD)), | ||
false)) | ||
continue; | ||
return; | ||
FoundDecls.push_back(VD); | ||
}; | ||
|
||
for (auto Member : Parent->getMembers()) { | ||
check(Member); | ||
Member->visitAuxiliaryDecls([&](Decl *d) { | ||
check(d); | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -634,16 +641,6 @@ static void synthesizeMemberDeclsForLookup(NominalTypeDecl *NTD, | |
false); | ||
} | ||
|
||
// Expand peer macros. | ||
for (auto *member : NTD->getMembers()) { | ||
if (!ctx.evaluator.hasActiveRequest(ExpandPeerMacroRequest{member})) { | ||
(void)evaluateOrDefault( | ||
ctx.evaluator, | ||
ExpandPeerMacroRequest{member}, | ||
{}); | ||
Comment on lines
-638
to
-643
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's |
||
} | ||
} | ||
|
||
synthesizePropertyWrapperVariables(NTD); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
// REQUIRES: swift_swift_parser | ||||||
|
||||||
// RUN: %empty-directory(%t) | ||||||
// RUN: mkdir -p %t/plugins | ||||||
|
||||||
//##-- Prepare the macro plugin. | ||||||
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/plugins/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/../Macros/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath | ||||||
|
||||||
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t -plugin-path %t/plugins -parse-as-library | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently
And the macro decl becomes If we omit
Introduced values are found and type checked, but since it's I feel like we should remove unexpandedDecls.append(TopLevelArbitraryMacros.begin(),
TopLevelArbitraryMacros.end()); From There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking within a struct There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For non top level cases, there is not much different code paths between arbitrary and named, but sure. |
||||||
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne)) | ||||||
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro") | ||||||
|
||||||
@attached(peer, names: suffixed(_peer)) | ||||||
macro PeerWithSuffix() = #externalMacro(module: "MacroDefinition", type: "PeerValueWithSuffixNameMacro") | ||||||
|
||||||
@attached(peer, names: arbitrary) | ||||||
macro PeerWithSuffixAsArbitrary() = #externalMacro(module: "MacroDefinition", type: "PeerValueWithSuffixNameMacro") | ||||||
|
||||||
@freestanding(declaration, names: arbitrary) | ||||||
macro VarValueDecl() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro") | ||||||
|
||||||
|
||||||
#defineDeclsWithKnownNames | ||||||
|
||||||
@PeerWithSuffix | ||||||
func globalFunc() {} | ||||||
|
||||||
func test() { | ||||||
#^GLOBAL^# | ||||||
// GLOBAL-DAG: Decl[Struct]/CurrModule: A[#A#]; name=A | ||||||
// GLOBAL-DAG: Decl[Struct]/CurrModule: B[#B#]; name=B | ||||||
// GLOBAL-DAG: Decl[GlobalVar]/CurrModule: foo[#Int#]; name=foo | ||||||
// GLOBAL-DAG: Decl[GlobalVar]/CurrModule: addOne[#(Int) -> Int#]; name=addOne | ||||||
// GLOBAL-DAG: Decl[FreeFunction]/CurrModule: globalFunc()[#Void#]; name=globalFunc() | ||||||
// GLOBAL-DAG: Decl[GlobalVar]/CurrModule: globalFunc_peer[#Int#]; name=globalFunc_peer | ||||||
} | ||||||
|
||||||
struct MyStruct { | ||||||
@PeerWithSuffix | ||||||
func instanceMethod() {} | ||||||
|
||||||
@PeerWithSuffix | ||||||
static func staticMethod() {} | ||||||
|
||||||
@PeerWithSuffixAsArbitrary | ||||||
func forArbitrary() {} | ||||||
|
||||||
#defineDeclsWithKnownNames | ||||||
|
||||||
#VarValueDecl | ||||||
} | ||||||
|
||||||
func testMemberInstance(value: MyStruct) { | ||||||
value.#^MEMBER_INSTANCE^# | ||||||
// MEMBER_INSTANCE-DAG: Keyword[self]/CurrNominal: self[#MyStruct#]; name=self | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceMethod]/CurrNominal: instanceMethod()[#Void#]; name=instanceMethod() | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: instanceMethod_peer[#Int#]; name=instanceMethod_peer | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: staticMethod_peer[#Int#]; name=staticMethod_peer | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceMethod]/CurrNominal: forArbitrary()[#Void#]; name=forArbitrary() | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: forArbitrary_peer[#Int#]; name=forArbitrary_peer | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: foo[#Int#]; name=foo | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: addOne[#(Int) -> Int#]; name=addOne | ||||||
// MEMBER_INSTANCE-DAG: Decl[InstanceVar]/CurrNominal: value[#Int#]; name=value | ||||||
// NOTE: 'staticMethod_peer' is a instance var because the macro emits the decl without 'static' | ||||||
} | ||||||
|
||||||
func testMemberStatic() { | ||||||
MyStruct.#^MEMBER_STATIC^# | ||||||
// MEMBER_STATIC-NOT: _peer | ||||||
// MEMBER_STATIC-DAG: Keyword[self]/CurrNominal: self[#MyStruct.Type#]; name=self | ||||||
// MEMBER_STATIC-DAG: Keyword/CurrNominal: Type[#MyStruct.Type#]; name=Type | ||||||
// MEMBER_STATIC-DAG: Decl[Struct]/CurrNominal: A[#MyStruct.A#]; name=A | ||||||
// MEMBER_STATIC-DAG: Decl[Struct]/CurrNominal: B[#MyStruct.B#]; name=B | ||||||
// MEMBER_STATIC-DAG: Decl[InstanceMethod]/CurrNominal: instanceMethod({#(self): MyStruct#})[#() -> Void#]; name=instanceMethod(:) | ||||||
// MEMBER_STATIC-DAG: Decl[StaticMethod]/CurrNominal: staticMethod()[#Void#]; name=staticMethod() | ||||||
// MEMBER_STATIC-NOT: _peer | ||||||
} | ||||||
|
||||||
func testLocal() { | ||||||
#defineDeclsWithKnownNames | ||||||
|
||||||
@PeerWithSuffix func localFunc() {} | ||||||
|
||||||
do { | ||||||
#^LOCAL?skip=FIXME^# | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to an
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I deliberately make this
If we fix (1) but not (2) this test goes nondeterministic because it succeeds only when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, OK. Makes sense. |
||||||
// FIXME: macros in replace function bodies are not handled correclty. | ||||||
// FIXME: decls instroduced by #defineDeclsWithKnownNames are missing. | ||||||
// LOCAL-DAG: Decl[FreeFunction]/Local: localFunc()[#Void#]; name=localFunc() | ||||||
// LOCAL-DAG: Decl[LocalVar]/Local: localFunc_peer[#Int#]; name=localFunc_peer | ||||||
// LOCAL-DAG: Decl[Struct]/Local: A[#A#]; name=A | ||||||
// LOCAL-DAG: Decl[Struct]/Local: B[#B#]; name=B | ||||||
// LOCAL-DAG: Decl[LocalVar]/Local: foo[#Int#]; name=foo | ||||||
// LOCAL-DAG: Decl[LocalVar]/Local: addOne[#(Int) -> Int#]; name=addOne | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍