Skip to content

Commit 53e5723

Browse files
authored
Merge pull request #65572 from ahoppen/ahoppen/macro-arg-completion
[CodeComplete] Offer code completion for attached macro attributes
2 parents a39a77e + 01cebd6 commit 53e5723

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,17 +3258,17 @@ void simple_display(llvm::raw_ostream &out,
32583258
class ResolveMacroRequest
32593259
: public SimpleRequest<ResolveMacroRequest,
32603260
ConcreteDeclRef(UnresolvedMacroReference,
3261-
const Decl *),
3261+
DeclContext *),
32623262
RequestFlags::Cached> {
32633263
public:
32643264
using SimpleRequest::SimpleRequest;
32653265

32663266
private:
32673267
friend SimpleRequest;
32683268

3269-
ConcreteDeclRef
3270-
evaluate(Evaluator &evaluator, UnresolvedMacroReference macroRef,
3271-
const Decl *decl) const;
3269+
ConcreteDeclRef evaluate(Evaluator &evaluator,
3270+
UnresolvedMacroReference macroRef,
3271+
DeclContext *decl) const;
32723272

32733273
public:
32743274
bool isCached() const { return true; }

lib/AST/Decl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ void Decl::forEachAttachedMacro(MacroRole role,
445445
MacroDecl *Decl::getResolvedMacro(CustomAttr *customAttr) const {
446446
auto declRef = evaluateOrDefault(
447447
getASTContext().evaluator,
448-
ResolveMacroRequest{customAttr, this},
449-
ConcreteDeclRef());
448+
ResolveMacroRequest{customAttr, getDeclContext()}, ConcreteDeclRef());
450449

451450
return dyn_cast_or_null<MacroDecl>(declRef.getDecl());
452451
}

lib/IDE/CodeCompletion.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,14 +1419,25 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
14191419
llvm::SaveAndRestore<TypeCheckCompletionCallback*>
14201420
CompletionCollector(Context.CompletionCallback, &Lookup);
14211421
if (AttrWithCompletion) {
1422-
/// The attribute might not be attached to the AST if there is no var decl
1423-
/// it could be attached to. Type check it standalone.
1424-
ASTNode Call = CallExpr::create(
1425-
CurDeclContext->getASTContext(), AttrWithCompletion->getTypeExpr(),
1426-
AttrWithCompletion->getArgs(), /*implicit=*/true);
1427-
typeCheckContextAt(
1428-
TypeCheckASTNodeAtLocContext::node(CurDeclContext, Call),
1429-
CompletionLoc);
1422+
/// The attribute might not be attached to the AST if there is no var
1423+
/// decl it could be attached to. Type check it standalone.
1424+
1425+
// First try to check it as an attached macro.
1426+
auto resolvedMacro = evaluateOrDefault(
1427+
CurDeclContext->getASTContext().evaluator,
1428+
ResolveMacroRequest{AttrWithCompletion, CurDeclContext},
1429+
ConcreteDeclRef());
1430+
1431+
// If that fails, type check as a call to the attribute's type. This is
1432+
// how, e.g., property wrappers are modelled.
1433+
if (!resolvedMacro) {
1434+
ASTNode Call = CallExpr::create(
1435+
CurDeclContext->getASTContext(), AttrWithCompletion->getTypeExpr(),
1436+
AttrWithCompletion->getArgs(), /*implicit=*/true);
1437+
typeCheckContextAt(
1438+
TypeCheckASTNodeAtLocContext::node(CurDeclContext, Call),
1439+
CompletionLoc);
1440+
}
14301441
} else {
14311442
typeCheckContextAt(
14321443
TypeCheckASTNodeAtLocContext::declContext(CurDeclContext),

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,9 +3798,8 @@ ExpandMacroExpansionDeclRequest::evaluate(Evaluator &evaluator,
37983798
auto *dc = MED->getDeclContext();
37993799

38003800
// Resolve macro candidates.
3801-
auto macro = evaluateOrDefault(
3802-
ctx.evaluator, ResolveMacroRequest{MED, MED},
3803-
ConcreteDeclRef());
3801+
auto macro = evaluateOrDefault(ctx.evaluator, ResolveMacroRequest{MED, dc},
3802+
ConcreteDeclRef());
38043803
if (!macro)
38053804
return None;
38063805
MED->setMacroRef(macro);

lib/Sema/TypeCheckMacros.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,12 +1502,9 @@ swift::expandConformances(CustomAttr *attr, MacroDecl *macro,
15021502
return macroSourceFile->getBufferID();
15031503
}
15041504

1505-
ConcreteDeclRef
1506-
ResolveMacroRequest::evaluate(Evaluator &evaluator,
1507-
UnresolvedMacroReference macroRef,
1508-
const Decl *decl) const {
1509-
auto dc = decl->getDeclContext();
1510-
1505+
ConcreteDeclRef ResolveMacroRequest::evaluate(Evaluator &evaluator,
1506+
UnresolvedMacroReference macroRef,
1507+
DeclContext *dc) const {
15111508
// Macro expressions and declarations have their own stored macro
15121509
// reference. Use it if it's there.
15131510
if (auto *expr = macroRef.getExpr()) {

test/IDE/complete_macros.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public macro AttachedMemberMacro()
3636
@attached(member)
3737
public macro AttachedMemberMacroWithArgs(arg1: Int)
3838

39+
public enum Direction {
40+
case up, down
41+
}
42+
43+
@attached(member)
44+
public macro AttachedMemberMacroWithEnumArgs(_ direction: Direction)
45+
46+
@attached(member)
47+
public macro AttachedMemberMacroWithMultipleArgs(first: Int, second: Int)
48+
3949
@attached(memberAttribute)
4050
public macro AttachedMemberAttributeMacro()
4151

@@ -157,6 +167,19 @@ struct NestedFreestanding {
157167
// ITEM_FREESTANDING-DAG: Decl[Macro]/{{.*}}: freestandingDeclMacro[#Void#]; name=freestandingDeclMacro
158168
// ITEM_FREESTANDING-DAG: Decl[Macro]/{{.*}}: EverythingMacro[#Void#]; name=EverythingMacro
159169

170+
171+
@AttachedMemberMacroWithEnumArgs(.#^ATTACHED_MACRO_ARG^#)
172+
struct AttachedMacroArg {}
173+
174+
// ATTACHED_MACRO_ARG-DAG: Decl[EnumElement]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Convertible]: up[#Direction#]; name=up
175+
// ATTACHED_MACRO_ARG-DAG: Decl[EnumElement]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Convertible]: down[#Direction#]; name=down
176+
177+
@AttachedMemberMacroWithMultipleArgs(first: 1, #^ATTACHED_MACRO_SECOND_ARG_LABEL^#)
178+
struct AttachedMacroSecondArgLabel {}
179+
180+
// ATTACHED_MACRO_SECOND_ARG_LABEL: Pattern/Local/Flair[ArgLabels]: {#second: Int#}[#Int#]; name=second:
181+
182+
160183
struct LastMember {
161184
@#^LAST_MEMBER_ATTR?check=INDEPENDENT_ATTR^#
162185
}

0 commit comments

Comments
 (0)