Skip to content

Commit ca5cbd2

Browse files
committed
[Macros] Tighten restriction on non-expression macros not having return types
There is no reason to special-case `Void` to permit it. Rather, make this a syntactic rule. Thanks to Alex Hoppen for the suggestion.
1 parent c5ec389 commit ca5cbd2

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,11 +2072,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20722072
}
20732073
}
20742074

2075-
// If the macro has a (non-Void) result type, it must have the freestanding
2075+
// If the macro has a result type, it must have the freestanding
20762076
// expression role. Other roles cannot have result types.
20772077
if (auto resultTypeRepr = MD->getResultTypeRepr()) {
2078-
if (!MD->getMacroRoles().contains(MacroRole::Expression) &&
2079-
!MD->getResultInterfaceType()->isEqual(Ctx.getVoidType())) {
2078+
if (!MD->getMacroRoles().contains(MacroRole::Expression)) {
20802079
auto resultType = MD->getResultInterfaceType();
20812080
Ctx.Diags.diagnose(
20822081
MD->arrowLoc, diag::macro_result_type_cannot_be_used, resultType)

test/Macros/attached_macros_diags.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
// expected-warning@-1{{external macro implementation type 'MyMacros.Macro1' could not be found for macro 'm1()'}}
77
// expected-note@-2{{'m1()' declared here}}
88

9-
@attached(accessor) macro m2(_: Int) -> Void = #externalMacro(module: "MyMacros", type: "Macro2")
9+
@attached(accessor) macro m2(_: Int) = #externalMacro(module: "MyMacros", type: "Macro2")
1010
// expected-warning@-1{{external macro implementation type 'MyMacros.Macro2' could not be found for macro 'm2'}}
1111
// expected-note@-2{{candidate has partially matching parameter list (Int)}}
1212
// expected-note@-3{{candidate expects value of type 'Int' for parameter #1 (got 'String')}}
1313

14-
@attached(accessor) macro m2(_: Double) -> Void = #externalMacro(module: "MyMacros", type: "Macro2")
14+
@attached(accessor) macro m2(_: Double) = #externalMacro(module: "MyMacros", type: "Macro2")
1515
// expected-warning@-1{{external macro implementation type 'MyMacros.Macro2' could not be found for macro 'm2'}}
1616
// expected-note@-2{{candidate has partially matching parameter list (Double)}}
1717
// expected-note@-3{{candidate expects value of type 'Double' for parameter #1 (got 'String')}}
1818

19-
@attached(accessor) macro m3(message: String) -> Void = #externalMacro(module: "MyMacros", type: "Macro3")
19+
@attached(accessor) macro m3(message: String) = #externalMacro(module: "MyMacros", type: "Macro3")
2020
// expected-warning@-1{{external macro implementation type 'MyMacros.Macro3' could not be found for macro 'm3(message:)'}}
2121

2222
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacros", type: "StringifyMacro")

test/Macros/macros_diagnostics.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ struct SomeType {
211211

212212
@freestanding(declaration) macro nonExpressionReturnsVoid<T>(_: T) -> Void = #externalMacro(module: "A", type: "B")
213213
// expected-warning@-1{{external macro implementation type}}
214+
// expected-error@-2{{only a freestanding expression macro can produce a result of type 'Void'}}
215+
// expected-note@-3{{make this macro a freestanding expression macro}}{{1-1=@freestanding(expression)\n}}
216+
// expected-note@-4{{remove the result type if the macro does not produce a value}}{{68-76=}}
214217

215218

216219
@freestanding(expression)

test/Macros/parsing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ macro am1()
5151
named, // expected-error{{introduced name kind 'named' requires a single argument '(name)'}}
5252
arbitrary(a) // expected-error{{introduced name kind 'arbitrary' must not have an argument}}
5353
)
54-
macro am2() -> Void
54+
macro am2()
5555
// expected-error@-1{{macro 'am2()' requires a definition}}
5656

5757
#m1 + 1

0 commit comments

Comments
 (0)