Skip to content

Commit 32a918a

Browse files
committed
[Macros] Ensure that we check the macro definition in the primary file.
Otherwise, we won't diagnose issues with a macro until the point of use.
1 parent ab4ecc3 commit 32a918a

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "swift/AST/ForeignErrorConvention.h"
4040
#include "swift/AST/GenericEnvironment.h"
4141
#include "swift/AST/Initializer.h"
42+
#include "swift/AST/MacroDefinition.h"
4243
#include "swift/AST/NameLookup.h"
4344
#include "swift/AST/NameLookupRequests.h"
4445
#include "swift/AST/PrettyStackTrace.h"
@@ -2004,6 +2005,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20042005
MD->diagnose(diag::macro_in_nested, MD->getName());
20052006
if (!MD->getMacroContexts())
20062007
MD->diagnose(diag::macro_without_context, MD->getName());
2008+
2009+
(void)MD->getDefinition();
20072010
}
20082011

20092012
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {

test/Macros/macro_availability_macosx.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@available(macOS 12.0, *)
55
struct X { }
66

7-
@expression macro m1: X = A.B // expected-error{{'X' is only available in macOS 12.0 or newer}}
7+
@expression macro m1: X = #externalMacro(module: "A", type: "B") // expected-error{{'X' is only available in macOS 12.0 or newer}}
88
99
@available(macOS 12.0, *)
10-
@expression macro m2: X = A.B
10+
@expression macro m2: X = #externalMacro(module: "A", type: "B")

test/Macros/macro_expand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
@expression macro customFileID: String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
2020
@expression macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
21-
@expression macro fileID<T: _ExpressibleByStringLiteral>: T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
21+
@expression macro fileID<T: ExpressibleByStringLiteral>: T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
2222
@expression macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
2323

2424
func testFileID(a: Int, b: Int) {

test/Macros/macros_diagnostics.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,47 @@
44
// expected-note@-1 2{{'stringify' declared here}}
55
@expression macro missingMacro1(_: Any) = MissingModule.MissingType // expected-note{{'missingMacro1' declared here}}
66
// expected-warning@-1{{external macro definitions are now written using #externalMacro}}{{43-68=#externalMacro(module: "MissingModule", type: "MissingType")}}
7-
@expression macro missingMacro2(_: Any) = MissingModule.MissingType
7+
@expression macro missingMacro2(_: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
88

99
protocol P { }
1010

11-
@expression macro tryToHide<T: P>(_: P) -> some P = BuiltinMacros.Blah
11+
@expression macro tryToHide<T: P>(_: T) -> some P = #externalMacro(module: "BuiltinMacros", type: "Blah")
1212
// expected-error@-1{{some' types are only permitted in properties, subscripts, and functions}}
13+
// expected-error@-2{{generic parameter 'T' could not be inferred}}
1314

1415
internal struct X { } // expected-note{{type declared here}}
1516

16-
@expression public macro createAnX: X = BuiltinMacros.Blah
17+
@expression public macro createAnX: X = #externalMacro(module: "BuiltinMacros", type: "Blah")
1718
// expected-error@-1{{macro cannot be declared public because its result type uses an internal type}}
1819

19-
@expression macro m1: Int = A.B
20-
@expression macro m1: Float = A.B
20+
@expression macro m1: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
21+
@expression macro m1: Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
2122

22-
@expression macro m2: Int = A.B // expected-note{{'m2' previously declared here}}
23-
@expression macro m2: Int = A.B // expected-error{{invalid redeclaration of 'm2'}}
23+
@expression macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m2' previously declared here}}
24+
@expression macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm2'}}
2425

25-
@expression macro m3(_: Int) -> Int = A.B
26-
@expression macro m3(_: Int) -> Float = A.B
26+
@expression macro m3(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
27+
@expression macro m3(_: Int) -> Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
2728

28-
@expression macro m4(_: Int) -> Int = A.B // expected-note{{'m4' previously declared here}}
29-
@expression macro m4(_: Int) -> Int = A.B // expected-error{{invalid redeclaration of 'm4'}}
29+
@expression macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m4' previously declared here}}
30+
@expression macro m4(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm4'}}
3031

3132
struct ZZZ {
32-
macro m5: Int = A.B
33+
macro m5: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
3334
// expected-error@-1{{macro 'm5' can only be declared at file scope}}
3435
// expected-error@-2{{macro 'm5' must declare its applicable contexts (e.g., '@expression')}}
3536
}
3637

37-
@expression macro multiArgMacro(_: Any, second: Any) = MissingModule.MissingType
38+
@expression macro multiArgMacro(_: Any, second: Any) = #externalMacro(module: "MissingModule", type: "MissingType")
3839
// expected-note@-1{{'multiArgMacro(_:second:)' declared here}}
3940

40-
@expression macro overloaded1(_ p: P) = MissingModule.MissingType
41+
@expression macro overloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType")
4142
func overloaded1(_ p: Any) { }
4243

43-
@expression macro notOverloaded1(_ p: P) = MissingModule.MissingType // expected-note{{'notOverloaded1' previously declared here}}
44-
@expression macro notOverloaded1(_ p: P) = MissingModule.MissingOtherType // expected-error{{invalid redeclaration of 'notOverloaded1'}}
44+
@expression macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingType") // expected-note{{'notOverloaded1' previously declared here}}
45+
@expression macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingOtherType") // expected-error{{invalid redeclaration of 'notOverloaded1'}}
4546

46-
@expression macro intIdentity(value: Int, _: Float) -> Int = MissingModule.MissingType
47+
@expression macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
4748
// expected-note@-1{{macro 'intIdentity(value:_:)' declared here}}
4849

4950
func testDiags(a: Int, b: Int) {

test/Macros/parsing.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ protocol Q { associatedtype Assoc }
99
@expression macro m5<T: P>(_: T) = #externalMacro(module: "A", type: "M4")
1010

1111
@expression macro m6 = A // expected-error{{expected '(' for macro parameters or ':' for a value-like macro}}
12+
// expected-error@-1{{macro must itself be defined by a macro expansion such as '#externalMacro(...)'}}

0 commit comments

Comments
 (0)