Skip to content

Commit c31f6fa

Browse files
authored
Merge pull request #64631 from DougGregor/macro-parameter-defaults
2 parents 00c14fa + 9d9f832 commit c31f6fa

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
@@ -6970,8 +6970,6 @@ ERROR(macro_definition_unsupported,none,
69706970
ERROR(external_macro_arg_not_type_name,none,
69716971
"argument to `#externalMacro` must be a string literal naming "
69726972
"the external macro's %select{module|type}0", (unsigned))
6973-
ERROR(attached_declaration_macro_not_supported,none,
6974-
"attached declaration macros are not yet supported", ())
69756973
ERROR(invalid_decl_in_macro_expansion,none,
69766974
"macro expansion cannot introduce %0",
69776975
(DescriptiveDeclKind))

lib/Parse/ParseDecl.cpp

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

96049604
// Parameter list.
9605+
DefaultArgumentInfo defaultArgs;
96059606
SmallVector<Identifier, 2> namePieces;
96069607
auto parameterResult = parseSingleParameterClause(
9607-
ParameterContextKind::Macro, &namePieces, nullptr);
9608+
ParameterContextKind::Macro, &namePieces, &defaultArgs);
96089609
status |= parameterResult;
96099610
parameterList = parameterResult.getPtrOrNull();
96109611

@@ -9647,6 +9648,8 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
96479648
status |= whereStatus;
96489649
}
96499650

9651+
defaultArgs.setFunctionContext(macro, macro->getParameterList());
9652+
96509653
return dcc.fixupParserResult(status, macro);
96519654
}
96529655

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
@@ -2043,6 +2043,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20432043
MD->diagnose(diag::macro_without_role, MD->getName());
20442044

20452045
TypeChecker::checkParameterList(MD->getParameterList(), MD);
2046+
checkDefaultArguments(MD->getParameterList());
20462047

20472048
// Check the macro definition.
20482049
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)