Skip to content

Commit 237c70e

Browse files
committed
[CodeComplete] Offer completion suggestions for default argument values inside the macro declaration
We were not type checking the default argument initializer because `MacroDecl` is not an `AbstractFucntionDecl`. Add `MacroDecl` to the list of nodes we know how to get parameters from. rdar://108163564
1 parent 053d215 commit 237c70e

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

include/swift/AST/Decl.h

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

8874+
/// Retrieve parameter declaration from the given source at given index, or
8875+
/// nullptr if the source does not have a parameter list.
8876+
const ParamDecl *getParameterAt(const DeclContext *source, unsigned index);
8877+
88748878
void simple_display(llvm::raw_ostream &out,
88758879
OptionSet<NominalTypeDecl::LookupDirectFlags> options);
88768880

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8281,6 +8281,14 @@ const ParamDecl *swift::getParameterAt(const ValueDecl *source,
82818281
return nullptr;
82828282
}
82838283

8284+
const ParamDecl *swift::getParameterAt(const DeclContext *source,
8285+
unsigned index) {
8286+
if (auto *params = getParameterList(const_cast<DeclContext *>(source))) {
8287+
return index < params->size() ? params->get(index) : nullptr;
8288+
}
8289+
return nullptr;
8290+
}
8291+
82848292
Type AbstractFunctionDecl::getMethodInterfaceType() const {
82858293
assert(getDeclContext()->isTypeContext());
82868294
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/TypeCheckStmt.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,13 +2366,8 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
23662366
}
23672367
}
23682368
} else if (auto *defaultArg = dyn_cast<DefaultArgumentInitializer>(DC)) {
2369-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(defaultArg->getParent())) {
2370-
auto *Param = AFD->getParameters()->get(defaultArg->getIndex());
2371-
(void)Param->getTypeCheckedDefaultExpr();
2372-
return false;
2373-
}
2374-
if (auto *SD = dyn_cast<SubscriptDecl>(defaultArg->getParent())) {
2375-
auto *Param = SD->getIndices()->get(defaultArg->getIndex());
2369+
if (const ParamDecl *Param =
2370+
getParameterAt(defaultArg->getParent(), defaultArg->getIndex())) {
23762371
(void)Param->getTypeCheckedDefaultExpr();
23772372
return false;
23782373
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
let globalVar = 1
5+
macro expect(file: Int = #^DEFAULT_ARG^#) = #externalMacro(module: "MyModule", type: "MyMacro")
6+
7+
// DEFAULT_ARG: Decl[GlobalVar]/CurrModule/TypeRelation[Convertible]: globalVar[#Int#]; name=globalVar

0 commit comments

Comments
 (0)