Skip to content

Commit 6def600

Browse files
committed
[Macros] Ensure that we inherit imports from enclosing file for macro expansion files
1 parent 3e8f0b2 commit 6def600

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ Expr *swift::expandMacroExpr(
477477
auto macroSourceFile = new (ctx) SourceFile(
478478
*dc->getParentModule(), SourceFileKind::MacroExpansion, macroBufferID,
479479
/*parsingOpts=*/{}, /*isPrimary=*/false);
480+
macroSourceFile->setImports(sourceFile->getImports());
480481

481482
// Retrieve the parsed expression from the list of top-level items.
482483
auto topLevelItems = macroSourceFile->getTopLevelItems();
@@ -655,6 +656,7 @@ bool swift::expandFreestandingDeclarationMacro(
655656
auto macroSourceFile = new (ctx) SourceFile(
656657
*dc->getParentModule(), SourceFileKind::MacroExpansion, macroBufferID,
657658
/*parsingOpts=*/{}, /*isPrimary=*/false);
659+
macroSourceFile->setImports(sourceFile->getImports());
658660

659661
PrettyStackTraceDecl debugStack(
660662
"type checking expanded declaration macro", med);
@@ -832,6 +834,7 @@ void swift::expandAccessors(
832834
auto macroSourceFile = new (ctx) SourceFile(
833835
*dc->getParentModule(), SourceFileKind::MacroExpansion, macroBufferID,
834836
/*parsingOpts=*/{}, /*isPrimary=*/false);
837+
macroSourceFile->setImports(declSourceFile->getImports());
835838

836839
PrettyStackTraceDecl debugStack(
837840
"type checking expanded declaration macro", storage);
@@ -1014,6 +1017,7 @@ void swift::expandAttributes(CustomAttr *attr, MacroDecl *macro, Decl *member,
10141017
auto macroSourceFile = new (ctx) SourceFile(
10151018
*dc->getParentModule(), SourceFileKind::MacroExpansion, macroBufferID,
10161019
/*parsingOpts=*/{}, /*isPrimary=*/false);
1020+
macroSourceFile->setImports(declSourceFile->getImports());
10171021

10181022
PrettyStackTraceDecl debugStack(
10191023
"type checking expanded declaration macro", member);

test/Macros/macro_expand.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
101101
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:[[@LINE-6]]:7 - {{.*}}macro_expand.swift:[[@LINE-6]]:27
102102
// CHECK-DIAGS-NEXT: oa - oa
103103
// CHECK-DIAGS-NEXT: END CONTENTS OF FILE
104+
105+
_ = #addBlocker({ // expected-note{{in expansion of macro 'addBlocker' here}}
106+
107+
print("hello")
108+
print(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
109+
// expected-note@-1{{use '-'}}
110+
print(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
111+
// expected-note@-1{{use '-'}}
112+
}())
113+
104114
// Check recursion.
105115
#recurse(false) // okay
106116
#recurse(true) // expected-note{{in expansion of macro 'recurse' here}}

0 commit comments

Comments
 (0)