Skip to content

Commit 9d9f832

Browse files
committed
[Macros] Allow macro parameters to have default arguments.
SE-0382 allows macro parameters to have default arguments. Enable these default arguments, with the normal type checking rules. One significant quirk to this implementation is that the actual default argument values don't make it to the macro implementation. This is a previously-unconsidered design problem we'll need to address. Tracked by rdar://104043987.
1 parent df35da4 commit 9d9f832

File tree

8 files changed

+17
-12
lines changed

8 files changed

+17
-12
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,8 +2028,6 @@ ERROR(foreign_diagnostic,none,
20282028
//------------------------------------------------------------------------------
20292029
ERROR(expected_macro_value_type,PointsToFirstBadToken,
20302030
"expected macro value type following ':'", ())
2031-
ERROR(no_default_arg_macro,none,
2032-
"default arguments are not allowed in macros", ())
20332031
ERROR(expected_lparen_macro,PointsToFirstBadToken,
20342032
"expected '(' for macro parameters or ':' for a value-like macro", ())
20352033
ERROR(expected_type_macro_result,PointsToFirstBadToken,

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6965,8 +6965,6 @@ ERROR(macro_definition_unsupported,none,
69656965
ERROR(external_macro_arg_not_type_name,none,
69666966
"argument to `#externalMacro` must be a string literal naming "
69676967
"the external macro's %select{module|type}0", (unsigned))
6968-
ERROR(attached_declaration_macro_not_supported,none,
6969-
"attached declaration macros are not yet supported", ())
69706968
ERROR(invalid_decl_in_macro_expansion,none,
69716969
"macro expansion cannot introduce %0",
69726970
(DescriptiveDeclKind))

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9595,9 +9595,10 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
95959595
DeclName macroFullName;
95969596

95979597
// Parameter list.
9598+
DefaultArgumentInfo defaultArgs;
95989599
SmallVector<Identifier, 2> namePieces;
95999600
auto parameterResult = parseSingleParameterClause(
9600-
ParameterContextKind::Macro, &namePieces, nullptr);
9601+
ParameterContextKind::Macro, &namePieces, &defaultArgs);
96019602
status |= parameterResult;
96029603
parameterList = parameterResult.getPtrOrNull();
96039604

@@ -9640,6 +9641,8 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
96409641
status |= whereStatus;
96419642
}
96429643

9644+
defaultArgs.setFunctionContext(macro, macro->getParameterList());
9645+
96439646
return dcc.fixupParserResult(status, macro);
96449647
}
96459648

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,14 @@ static ParserStatus parseDefaultArgument(
109109
case Parser::ParameterContextKind::Initializer:
110110
case Parser::ParameterContextKind::EnumElement:
111111
case Parser::ParameterContextKind::Subscript:
112+
case Parser::ParameterContextKind::Macro:
112113
break;
113114
case Parser::ParameterContextKind::Closure:
114115
diagID = diag::no_default_arg_closure;
115116
break;
116117
case Parser::ParameterContextKind::Curried:
117118
diagID = diag::no_default_arg_curried;
118119
break;
119-
case Parser::ParameterContextKind::Macro:
120-
diagID = diag::no_default_arg_macro;
121-
break;
122120
}
123121

124122
assert((diagID.ID != DiagID()) == !defaultArgs &&
@@ -704,7 +702,8 @@ mapParsedParameters(Parser &parser,
704702
paramContext == Parser::ParameterContextKind::Operator ||
705703
paramContext == Parser::ParameterContextKind::Initializer ||
706704
paramContext == Parser::ParameterContextKind::EnumElement ||
707-
paramContext == Parser::ParameterContextKind::Subscript) &&
705+
paramContext == Parser::ParameterContextKind::Subscript ||
706+
paramContext == Parser::ParameterContextKind::Macro) &&
708707
"Default arguments are only permitted on the first param clause");
709708

710709
if (param.DefaultArg) {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20272027
MD->diagnose(diag::macro_without_role, MD->getName());
20282028

20292029
TypeChecker::checkParameterList(MD->getParameterList(), MD);
2030+
checkDefaultArguments(MD->getParameterList());
20302031

20312032
// Check the macro definition.
20322033
switch (auto macroDef = MD->getDefinition()) {

test/Macros/macros_diagnostics.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,13 @@ func testExternalMacroOutOfPlace() {
146146

147147
@freestanding(expression)
148148
public macro macroWithDefaults(_: Int = 17) = #externalMacro(module: "A", type: "B")
149-
// expected-error@-1{{default arguments are not allowed in macros}}
150-
// expected-warning@-2{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
149+
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
150+
// expected-note@-2{{'macroWithDefaults' declared here}}
151+
152+
func callMacroWithDefaults() {
153+
_ = #macroWithDefaults()
154+
// expected-error@-1 {{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
155+
}
151156

152157
// Make sure we don't allow macros to prevent type folding.
153158
@attached(member)

test/Serialization/Inputs/def_macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
1+
@freestanding(expression) public macro publicStringify<T>(_ value: T, label: String? = nil) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
22

33
@freestanding(expression) macro internalStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
44

test/Serialization/macros.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import def_macros
1313

1414
func test(a: Int, b: Int) {
1515
_ = #publicStringify(a + b)
16+
_ = #publicStringify(a + b, label: "hello")
1617

1718
_ = #internalStringify(a + b)
1819
// expected-error@-1{{no macro named 'internalStringify'}}

0 commit comments

Comments
 (0)