Skip to content

Commit ae029f7

Browse files
committed
[Macros] Diagnose top-level expansion of undefined freestanding macro
The root problem is that `ResolveMacroRequest` was shortcuting after calling `lookupMacros`. It shouldn't need to call `lookupMacros` at all, but should go straight to CSGen. It's fixed by deleting 4 lines of code. rdar://108280416
1 parent adc1a44 commit ae029f7

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,10 +1483,6 @@ ResolveMacroRequest::evaluate(Evaluator &evaluator,
14831483

14841484
auto &ctx = dc->getASTContext();
14851485
auto roles = macroRef.getMacroRoles();
1486-
auto foundMacros = TypeChecker::lookupMacros(
1487-
dc, macroRef.getMacroName(), SourceLoc(), roles);
1488-
if (foundMacros.empty())
1489-
return ConcreteDeclRef();
14901486

14911487
// If we already have a MacroExpansionExpr, use that. Otherwise,
14921488
// create one.

test/Macros/macros_diagnostics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,7 @@ struct MyStruct<T: MyProto> {
185185

186186
@freestanding(expression) macro myMacro<T : MyProto>(_ value: MyStruct<T>) -> MyStruct<T> = #externalMacro(module: "A", type: "B")
187187
// expected-warning@-1{{external macro implementation type}}
188+
189+
#undefinedMacro { definitelyNotDefined }
190+
// expected-error@-1{{cannot find 'definitelyNotDefined' in scope}}
191+
// expected-error@-2{{no macro named 'undefinedMacro'}}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: swift_swift_parser
2+
3+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -parse-as-library -module-name MacrosTest
4+
5+
// We need this test because top-level freestanding macro expansions are parsed
6+
// differently in library mode.
7+
8+
#undefinedMacro1
9+
// expected-error@-1{{no macro named 'undefinedMacro1'}}
10+
11+
#undefinedMacro2 { definitelyNotDefined }
12+
// expected-error@-1{{no macro named 'undefinedMacro2'}}
13+
// expected-error@-2{{cannot find 'definitelyNotDefined' in scope}}

0 commit comments

Comments
 (0)