Skip to content

Commit a39a77e

Browse files
authored
Merge pull request #65526 from ahoppen/ahoppen/complete-in-macro-default-arg
[CodeComplete] Offer completion suggestions for default argument values inside the macro declaration + argument label inside `#externalMacro`
2 parents e1f2a20 + 7e4b88b commit a39a77e

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8876,6 +8876,10 @@ const ParamDecl *getParameterAt(ConcreteDeclRef declRef, unsigned index);
88768876
/// nullptr if the source does not have a parameter list.
88778877
const ParamDecl *getParameterAt(const ValueDecl *source, unsigned index);
88788878

8879+
/// Retrieve parameter declaration from the given source at given index, or
8880+
/// nullptr if the source does not have a parameter list.
8881+
const ParamDecl *getParameterAt(const DeclContext *source, unsigned index);
8882+
88798883
void simple_display(llvm::raw_ostream &out,
88808884
OptionSet<NominalTypeDecl::LookupDirectFlags> options);
88818885

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8363,6 +8363,14 @@ const ParamDecl *swift::getParameterAt(const ValueDecl *source,
83638363
return nullptr;
83648364
}
83658365

8366+
const ParamDecl *swift::getParameterAt(const DeclContext *source,
8367+
unsigned index) {
8368+
if (auto *params = getParameterList(const_cast<DeclContext *>(source))) {
8369+
return index < params->size() ? params->get(index) : nullptr;
8370+
}
8371+
return nullptr;
8372+
}
8373+
83668374
Type AbstractFunctionDecl::getMethodInterfaceType() const {
83678375
assert(getDeclContext()->isTypeContext());
83688376
auto Ty = getInterfaceType();

lib/SIL/IR/TypeLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3959,7 +3959,8 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
39593959
fn.getAsRegularLocation(), Context);
39603960

39613961
if (auto *afd = dyn_cast<AbstractFunctionDecl>(curFn.getDecl())) {
3962-
auto *param = getParameterAt(afd, curFn.defaultArgIndex);
3962+
auto *param = getParameterAt(static_cast<ValueDecl *>(afd),
3963+
curFn.defaultArgIndex);
39633964
if (param->hasDefaultExpr()) {
39643965
auto dc = afd->getInnermostDeclContext();
39653966
collectCaptures(param->getDefaultArgumentCaptureInfo(), dc);

lib/Sema/TypeCheckMacros.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ MacroDefinition MacroDefinitionRequest::evaluate(
193193
free(replacements);
194194
};
195195

196+
if (checkResult < 0 && ctx.CompletionCallback) {
197+
// If the macro failed to check and we are in code completion mode, pretend
198+
// it's an arbitrary macro. This allows us to get call argument completions
199+
// inside `#externalMacro`.
200+
checkResult = BridgedMacroDefinitionKind::BridgedExpandedMacro;
201+
}
202+
196203
if (checkResult < 0)
197204
return MacroDefinition::forInvalid();
198205

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,13 +2376,8 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
23762376
}
23772377
}
23782378
} else if (auto *defaultArg = dyn_cast<DefaultArgumentInitializer>(DC)) {
2379-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(defaultArg->getParent())) {
2380-
auto *Param = AFD->getParameters()->get(defaultArg->getIndex());
2381-
(void)Param->getTypeCheckedDefaultExpr();
2382-
return false;
2383-
}
2384-
if (auto *SD = dyn_cast<SubscriptDecl>(defaultArg->getParent())) {
2385-
auto *Param = SD->getIndices()->get(defaultArg->getIndex());
2379+
if (const ParamDecl *Param =
2380+
getParameterAt(defaultArg->getParent(), defaultArg->getIndex())) {
23862381
(void)Param->getTypeCheckedDefaultExpr();
23872382
return false;
23882383
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: swift_swift_parser
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
4+
5+
let globalVar = 1
6+
macro expect(file: Int = #^DEFAULT_ARG^#) = #externalMacro(module: "MyModule", type: "MyMacro")
7+
// DEFAULT_ARG: Decl[GlobalVar]/CurrModule/TypeRelation[Convertible]: globalVar[#Int#]; name=globalVar
8+
9+
@freestanding(expression)
10+
macro externalMacro() = ##^EXTERNAL_MACRO^#
11+
// EXTERNAL_MACRO: Decl[Macro]/OtherModule[Swift]/IsSystem: externalMacro({#module: String#}, {#type: String#})[#T#]; name=externalMacro(module:type:)
12+
13+
@freestanding(expression)
14+
macro externalMacroWithTrailing() = ##^EXTERNAL_MACRO_WITH_TRAILING?check=EXTERNAL_MACRO^#externalMacro
15+
16+
@freestanding(expression)
17+
macro externalMacroCallPattern() = #externalMacro(#^EXTERNAL_MACRO_CALL_PATTERN^#)
18+
// EXTERNAL_MACRO_CALL_PATTERN: Pattern/None/Flair[ArgLabels]/TypeRelation[Convertible]: ['(']{#module: String#}, {#type: String#}[')'][#Void#]; name=module:type:
19+
20+
@freestanding(expression)
21+
macro externalMacroCallPattern() = #externalMacro(module: "MyModule", #^EXTERNAL_MACRO_TYPE_ARG_LABEL^#)
22+
// EXTERNAL_MACRO_TYPE_ARG_LABEL: Pattern/Local/Flair[ArgLabels]: {#type: String#}[#String#]; name=type:

0 commit comments

Comments
 (0)