Skip to content

Commit 5666e1d

Browse files
authored
Merge pull request swiftlang#63533 from DougGregor/minor-macro-fixes
2 parents d9d8d56 + 956e81c commit 5666e1d

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

include/swift/AST/DeclContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
519519
LLVM_READONLY
520520
SourceFile *getParentSourceFile() const;
521521

522+
/// Returns the "outermost" source file that contains this context,
523+
/// looking through any source files for generated code, such as
524+
/// macro expansions.
525+
LLVM_READONLY
526+
SourceFile *getOutermostParentSourceFile() const;
527+
522528
/// Determine whether this declaration context is generic, meaning that it or
523529
/// any of its parents have generic parameters.
524530
bool isGenericContext() const;

lib/AST/DeclContext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ SourceFile *DeclContext::getParentSourceFile() const {
323323
return fallbackSF;
324324
}
325325

326+
SourceFile *DeclContext::getOutermostParentSourceFile() const {
327+
auto sf = getParentSourceFile();
328+
if (!sf)
329+
return nullptr;
330+
331+
// Find the originating source file.
332+
while (auto enclosingSF = sf->getEnclosingSourceFile())
333+
sf = enclosingSF;
334+
335+
return sf;
336+
}
337+
326338
DeclContext *DeclContext::getModuleScopeContext() const {
327339
auto DC = const_cast<DeclContext*>(this);
328340

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,10 +1919,11 @@ IRGenModule *IRGenerator::getGenModule(DeclContext *ctxt) {
19191919
if (GenModules.size() == 1 || !ctxt) {
19201920
return getPrimaryIGM();
19211921
}
1922-
SourceFile *SF = ctxt->getParentSourceFile();
1922+
SourceFile *SF = ctxt->getOutermostParentSourceFile();
19231923
if (!SF) {
19241924
return getPrimaryIGM();
19251925
}
1926+
19261927
IRGenModule *IGM = GenModules[SF];
19271928
assert(IGM);
19281929
return IGM;

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9502,9 +9502,8 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
95029502
} else {
95039503
// Parameter list.
95049504
SmallVector<Identifier, 2> namePieces;
9505-
DefaultArgumentInfo defaultArgs;
95069505
auto parameterResult = parseSingleParameterClause(
9507-
ParameterContextKind::Macro, &namePieces, &defaultArgs);
9506+
ParameterContextKind::Macro, &namePieces, nullptr);
95089507
status |= parameterResult;
95099508
parameterList = parameterResult.getPtrOrNull();
95109509

test/Macros/macros_diagnostics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,8 @@ func testExternalMacroOutOfPlace() {
134134
let _: Int = #externalMacro(module: "A", type: "B")
135135
// expected-error@-1{{macro 'externalMacro' can only be used to define another macro}}
136136
}
137+
138+
@freestanding(expression)
139+
public macro macroWithDefaults(_: Int = 17) = #externalMacro(module: "A", type: "B")
140+
// expected-error@-1{{default arguments are not allowed in macros}}
141+
// expected-warning@-2{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}

0 commit comments

Comments
 (0)