Skip to content

Commit 9985807

Browse files
authored
Merge pull request swiftlang#63565 from DougGregor/macro-syntax-cleanup
2 parents fad0f90 + 04eca73 commit 9985807

File tree

9 files changed

+54
-63
lines changed

9 files changed

+54
-63
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8358,12 +8358,11 @@ class MacroDecl : public GenericContext, public ValueDecl {
83588358
/// The location of the 'macro' keyword.
83598359
SourceLoc macroLoc;
83608360

8361-
/// The parameter list for a function-like macro.
8361+
/// The parameter list.
83628362
ParameterList *parameterList;
83638363

8364-
/// Where the '->' or ':' is located, for a function- or value-like macro,
8365-
/// respectively.
8366-
SourceLoc arrowOrColonLoc;
8364+
/// Where the '->' is located, if present.
8365+
SourceLoc arrowLoc;
83678366

83688367
/// The result type.
83698368
TypeLoc resultType;
@@ -8375,7 +8374,7 @@ class MacroDecl : public GenericContext, public ValueDecl {
83758374
MacroDecl(SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
83768375
GenericParamList *genericParams,
83778376
ParameterList *parameterList,
8378-
SourceLoc arrowOrColonLoc,
8377+
SourceLoc arrowLoc,
83798378
TypeRepr *resultType,
83808379
Expr *definition,
83818380
DeclContext *parent);

lib/AST/Decl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9820,14 +9820,14 @@ MacroDecl::MacroDecl(
98209820
SourceLoc macroLoc, DeclName name, SourceLoc nameLoc,
98219821
GenericParamList *genericParams,
98229822
ParameterList *parameterList,
9823-
SourceLoc arrowOrColonLoc,
9823+
SourceLoc arrowLoc,
98249824
TypeRepr *resultType,
98259825
Expr *definition,
98269826
DeclContext *parent
98279827
) : GenericContext(DeclContextKind::MacroDecl, parent, genericParams),
98289828
ValueDecl(DeclKind::Macro, parent, name, nameLoc),
98299829
macroLoc(macroLoc), parameterList(parameterList),
9830-
arrowOrColonLoc(arrowOrColonLoc),
9830+
arrowLoc(arrowLoc),
98319831
resultType(resultType),
98329832
definition(definition) {
98339833

@@ -9849,6 +9849,8 @@ SourceRange MacroDecl::getSourceRange() const {
98499849
SourceLoc endLoc = getNameLoc();
98509850
if (parameterList)
98519851
endLoc = parameterList->getEndLoc();
9852+
if (resultType.getSourceRange().isValid())
9853+
endLoc = resultType.getSourceRange().End;
98529854
if (definition)
98539855
endLoc = definition->getEndLoc();
98549856
if (auto trailing = getTrailingWhereClause())

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ void CompletionLookup::addMacroExpansion(const MacroDecl *MD,
17931793
addValueBaseName(Builder, MD->getBaseIdentifier());
17941794

17951795
Type macroType = MD->getInterfaceType();
1796-
if (MD->parameterList && macroType->is<FunctionType>()) {
1796+
if (MD->parameterList && MD->parameterList->size() > 0) {
17971797
Builder.addLeftParen();
17981798
addCallArgumentPatterns(Builder, macroType->castTo<FunctionType>(),
17991799
MD->parameterList,

lib/Parse/ParseDecl.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9487,40 +9487,30 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
94879487

94889488
// Parse the macro signature.
94899489
ParameterList *parameterList = nullptr;
9490-
SourceLoc arrowOrColonLoc;
9490+
SourceLoc arrowLoc;
94919491
TypeRepr *resultType = nullptr;
94929492
DeclName macroFullName;
9493-
if (consumeIf(tok::colon, arrowOrColonLoc)) {
9494-
// Value-like macros.
9495-
auto type = parseType(diag::expected_macro_value_type);
9496-
status |= type;
9493+
9494+
// Parameter list.
9495+
SmallVector<Identifier, 2> namePieces;
9496+
auto parameterResult = parseSingleParameterClause(
9497+
ParameterContextKind::Macro, &namePieces, nullptr);
9498+
status |= parameterResult;
9499+
parameterList = parameterResult.getPtrOrNull();
9500+
9501+
// ->
9502+
if (consumeIf(tok::arrow, arrowLoc)) {
9503+
// Result type.
9504+
auto parsedResultType =
9505+
parseDeclResultType(diag::expected_type_macro_result);
9506+
resultType = parsedResultType.getPtrOrNull();
9507+
status |= parsedResultType;
94979508
if (status.isErrorOrHasCompletion())
94989509
return status;
9499-
9500-
resultType = type.getPtrOrNull();
9501-
macroFullName = macroName;
9502-
} else {
9503-
// Parameter list.
9504-
SmallVector<Identifier, 2> namePieces;
9505-
auto parameterResult = parseSingleParameterClause(
9506-
ParameterContextKind::Macro, &namePieces, nullptr);
9507-
status |= parameterResult;
9508-
parameterList = parameterResult.getPtrOrNull();
9509-
9510-
// ->
9511-
if (consumeIf(tok::arrow, arrowOrColonLoc)) {
9512-
// Result type.
9513-
auto parsedResultType =
9514-
parseDeclResultType(diag::expected_type_macro_result);
9515-
resultType = parsedResultType.getPtrOrNull();
9516-
status |= parsedResultType;
9517-
if (status.isErrorOrHasCompletion())
9518-
return status;
9519-
}
9520-
9521-
macroFullName = DeclName(Context, macroName, namePieces);
95229510
}
95239511

9512+
macroFullName = DeclName(Context, macroName, namePieces);
9513+
95249514
// Parse '=' <expression>
95259515
Expr *definition = nullptr;
95269516
if (consumeIf(tok::equal)) {
@@ -9534,7 +9524,7 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
95349524
// Create the macro declaration.
95359525
auto *macro = new (Context) MacroDecl(
95369526
macroLoc, macroFullName, macroNameLoc, genericParams, parameterList,
9537-
arrowOrColonLoc, resultType, definition, CurDeclContext);
9527+
arrowLoc, resultType, definition, CurDeclContext);
95389528
macro->getAttrs() = attributes;
95399529

95409530
// Parse a 'where' clause if present.

test/IDE/complete_pound_expr_macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import ObjectiveC
77

8-
macro myLine: Int = A.B
9-
macro myFilename<T: ExpressiblyByStringLiteral>: T = A.B
8+
macro myLine() -> Int = A.B
9+
macro myFilename<T: ExpressiblyByStringLiteral>() -> T = A.B
1010
macro myStringify<T>(_: T) -> (T, String) = A.B
1111

1212
struct Color { }

test/Macros/macro_availability_macosx.swift

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

7-
@freestanding(expression) macro m1: X = #externalMacro(module: "A", type: "B") // expected-error{{'X' is only available in macOS 12.0 or newer}}
8-
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm1'}}
7+
@freestanding(expression) macro m1() -> X = #externalMacro(module: "A", type: "B") // expected-error{{'X' is only available in macOS 12.0 or newer}}
8+
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm1()'}}
99

1010
@available(macOS 12.0, *)
11-
@freestanding(expression) macro m2: X = #externalMacro(module: "A", type: "B")
12-
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm2'}}
11+
@freestanding(expression) macro m2() -> X = #externalMacro(module: "A", type: "B")
12+
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'm2()'}}

test/Macros/macros_diagnostics.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ protocol P { }
1818

1919
internal struct X { } // expected-note{{type declared here}}
2020

21-
@freestanding(expression) public macro createAnX: X = #externalMacro(module: "BuiltinMacros", type: "Blah")
21+
@freestanding(expression) public macro createAnX() -> X = #externalMacro(module: "BuiltinMacros", type: "Blah")
2222
// expected-error@-1{{macro cannot be declared public because its result type uses an internal type}}
2323
// expected-warning@-2{{external macro implementation type}}
2424

25-
@freestanding(expression) macro m1: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
25+
@freestanding(expression) macro m1() -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
2626
// expected-warning@-1{{external macro implementation type}}
27-
@freestanding(expression) macro m1: Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
27+
@freestanding(expression) macro m1() -> Float = #externalMacro(module: "BuiltinMacros", type: "Blah")
2828
// expected-warning@-1{{external macro implementation type}}
2929

30-
@freestanding(expression) macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m2' previously declared here}}
30+
@freestanding(expression) macro m2() -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-note{{'m2()' previously declared here}}
3131
// expected-warning@-1{{external macro implementation type}}
32-
@freestanding(expression) macro m2: Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm2'}}
32+
@freestanding(expression) macro m2() -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah") // expected-error{{invalid redeclaration of 'm2()'}}
3333
// expected-warning@-1{{external macro implementation type}}
3434

3535
@freestanding(expression) macro m3(_: Int) -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
@@ -43,9 +43,9 @@ internal struct X { } // expected-note{{type declared here}}
4343
// expected-warning@-1{{external macro implementation type}}
4444

4545
struct ZZZ {
46-
macro m5: Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
47-
// expected-error@-1{{macro 'm5' can only be declared at file scope}}
48-
// expected-error@-2{{macro 'm5' must declare its applicable roles}}
46+
macro m5() -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
47+
// expected-error@-1{{macro 'm5()' can only be declared at file scope}}
48+
// expected-error@-2{{macro 'm5()' must declare its applicable roles}}
4949
// expected-warning@-3{{external macro implementation type}}
5050
}
5151

test/Macros/parsing.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
protocol P { }
33
protocol Q { associatedtype Assoc }
44

5-
@freestanding(expression) macro m1: Int = #externalMacro(module: "A", type: "M1")
6-
// expected-warning@-1{{external macro implementation type 'A.M1' could not be found for macro 'm1'; the type must be public and provided via '-load-plugin-library'}}
5+
@freestanding(expression) macro m1() -> Int = #externalMacro(module: "A", type: "M1")
6+
// expected-warning@-1{{external macro implementation type 'A.M1' could not be found for macro 'm1()'; the type must be public and provided via '-load-plugin-library'}}
77
@freestanding(expression) macro m2(_: Int) = #externalMacro(module: "A", type: "M2")
88
// expected-warning@-1{{external macro implementation type 'A.M2' could not be found for macro 'm2'; the type must be public and provided via '-load-plugin-library'}}
99
@freestanding(expression) macro m3(a b: Int) -> Int = #externalMacro(module: "A", type: "M3")
1010
// expected-warning@-1{{external macro implementation type 'A.M3' could not be found for macro 'm3(a:)'; the type must be public and provided via '-load-plugin-library'}}
11-
@freestanding(expression) macro m4<T: Q>: T = #externalMacro(module: "A", type: "M4") where T.Assoc: P
12-
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm4'; the type must be public and provided via '-load-plugin-library'}}
11+
@freestanding(expression) macro m4<T: Q>() -> T = #externalMacro(module: "A", type: "M4") where T.Assoc: P
12+
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm4()'; the type must be public and provided via '-load-plugin-library'}}
1313
@freestanding(expression) macro m5<T: P>(_: T) = #externalMacro(module: "A", type: "M4")
1414
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm5'; the type must be public and provided via '-load-plugin-library'}}
1515

@@ -36,8 +36,8 @@ macro m10(_: String) = #externalMacro(module: "A", type: "M4")
3636
accessor,
3737
names: overloaded, arbitrary, named(hello), prefixed(_), suffixed(_favorite)
3838
)
39-
macro am1: Void
40-
// expected-error@-1{{macro 'am1' requires a definition}}
39+
macro am1()
40+
// expected-error@-1{{macro 'am1()' requires a definition}}
4141

4242
@attached(
4343
accessor,
@@ -46,5 +46,5 @@ macro am1: Void
4646
named, // expected-error{{introduced name kind 'named' requires a single argument '(name)'}}
4747
arbitrary(a) // expected-error{{introduced name kind 'arbitrary' must not have an argument}}
4848
)
49-
macro am2: Void
50-
// expected-error@-1{{macro 'am2' requires a definition}}
49+
macro am2() -> Void
50+
// expected-error@-1{{macro 'am2()' requires a definition}}

test/ModuleInterface/macros.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
@freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")
1313

1414
// CHECK: #if compiler(>=5.3) && $FreestandingExpressionMacros && $Macros
15-
// CHECK: @freestanding(expression) public macro publicLine<T>: T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
15+
// CHECK: @freestanding(expression) public macro publicLine<T>() -> T = #externalMacro(module: "SomeModule", type: "Line") where T : Swift.ExpressibleByIntegerLiteral
1616
// CHECK-NEXT: #endif
17-
@freestanding(expression) public macro publicLine<T: ExpressibleByIntegerLiteral>: T = #externalMacro(module: "SomeModule", type: "Line")
17+
@freestanding(expression) public macro publicLine<T: ExpressibleByIntegerLiteral>() -> T = #externalMacro(module: "SomeModule", type: "Line")
1818

1919
// CHECK: #if compiler(>=5.3) && $Macros
20-
// CHECK: @attached(accessor) public macro myWrapper: Swift.Void = #externalMacro(module: "SomeModule", type: "Wrapper")
20+
// CHECK: @attached(accessor) public macro myWrapper() -> () = #externalMacro(module: "SomeModule", type: "Wrapper")
2121
// CHECK-NEXT: #endif
22-
@attached(accessor) public macro myWrapper: Void = #externalMacro(module: "SomeModule", type: "Wrapper")
22+
@attached(accessor) public macro myWrapper() = #externalMacro(module: "SomeModule", type: "Wrapper")
2323

2424
// CHECK-NOT: internalStringify
2525
@freestanding(expression) macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "SomeModule", type: "StringifyMacro")

0 commit comments

Comments
 (0)