Skip to content

Commit 62fabb6

Browse files
committed
[Macros] Properly compute the innermost declaration context of a macro.
How did we go this long without having the right context? Who knows. The fix is trivial and obvious. The failure mode here is that we wouldn't get an appropriate generic context in the result type of a macro, so we would complain about generic parameters not meeting their own generic requirements.
1 parent 0fb7f8f commit 62fabb6

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ DeclContext *Decl::getInnermostDeclContext() const {
574574
return const_cast<ExtensionDecl*>(ext);
575575
if (auto topLevel = dyn_cast<TopLevelCodeDecl>(this))
576576
return const_cast<TopLevelCodeDecl*>(topLevel);
577+
if (auto macro = dyn_cast<MacroDecl>(this))
578+
return const_cast<MacroDecl*>(macro);
577579

578580
return getDeclContext();
579581
}

test/Macros/macros_diagnostics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,13 @@ func test() {
167167
let _: [MacroOrType.Nested] = []
168168
_ = [MacroOrType.Nested]()
169169
}
170+
171+
// Make sure we have the right declaration context for type-checking the result
172+
// types of macros. At one point, we would reject the following macro.
173+
protocol MyProto {
174+
}
175+
struct MyStruct<T: MyProto> {
176+
}
177+
178+
@freestanding(expression) macro myMacro<T : MyProto>(_ value: MyStruct<T>) -> MyStruct<T> = #externalMacro(module: "A", type: "B")
179+
// expected-warning@-1{{external macro implementation type}}

0 commit comments

Comments
 (0)