Skip to content

Commit 987e569

Browse files
authored
Merge pull request #65519 from rintaro/macros-nestedexpand-rdar108622244
[Macros] Don't expand macros inside macro expession arguments
2 parents 94bfb24 + 5c2faf3 commit 987e569

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5389,7 +5389,15 @@ namespace {
53895389
E->setMacroRef(macroRef);
53905390
E->setType(expandedType);
53915391

5392-
if (!cs.Options.contains(ConstraintSystemFlags::DisableMacroExpansions)) {
5392+
// FIXME: Expansion should be lazy.
5393+
// i.e. 'ExpandMacroExpansionExprRequest' should be sinked into
5394+
// 'getRewritten()', and performed on-demand.
5395+
if (!cs.Options.contains(ConstraintSystemFlags::DisableMacroExpansions) &&
5396+
// Do not expand macros inside macro arguments. For example for
5397+
// '#stringify(#assert(foo))' when typechecking `#assert(foo)`,
5398+
// we don't want to expand it.
5399+
llvm::none_of(makeArrayRef(ExprStack).drop_back(1),
5400+
[](Expr *E) { return isa<MacroExpansionExpr>(E); })) {
53935401
(void)evaluateOrDefault(cs.getASTContext().evaluator,
53945402
ExpandMacroExpansionExprRequest{E}, None);
53955403
}

test/Macros/macro_expand.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ func testDiscardableStringify(x: Int) {
181181
}
182182
#endif
183183

184+
#if TEST_DIAGNOSTICS
185+
// This causes an error when non-'Bool' value is passed.
186+
@freestanding(expression) macro assertAny<T>(_ value: T) = #externalMacro(module: "MacroDefinition", type: "AssertMacro")
187+
188+
func testNested() {
189+
struct Nested { }
190+
_ = #stringify(#assertAny(Nested()))
191+
// expected-note@-1 2 {{in expansion of macro 'stringify' here}}
192+
// CHECK-DIAGS-NOT: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
193+
// CHECK-DIAGS: @__swiftmacro_9MacroUser10testNestedyyF9stringifyfMf_9assertAnyfMf_.swift:1:8: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
194+
// CHECK-DIAGS-NOT: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
195+
}
196+
#endif
197+
184198
func testStringifyWithThrows() throws {
185199
// Okay, we can put the try inside or outside
186200
_ = try #stringify(maybeThrowing())

0 commit comments

Comments
 (0)