Skip to content

[Macros] Allow macro parameters to have default arguments. #64723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2028,8 +2028,6 @@ ERROR(foreign_diagnostic,none,
//------------------------------------------------------------------------------
ERROR(expected_macro_value_type,PointsToFirstBadToken,
"expected macro value type following ':'", ())
ERROR(no_default_arg_macro,none,
"default arguments are not allowed in macros", ())
ERROR(expected_lparen_macro,PointsToFirstBadToken,
"expected '(' for macro parameters or ':' for a value-like macro", ())
ERROR(expected_type_macro_result,PointsToFirstBadToken,
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -6970,8 +6970,6 @@ ERROR(macro_definition_unsupported,none,
ERROR(external_macro_arg_not_type_name,none,
"argument to `#externalMacro` must be a string literal naming "
"the external macro's %select{module|type}0", (unsigned))
ERROR(attached_declaration_macro_not_supported,none,
"attached declaration macros are not yet supported", ())
ERROR(invalid_decl_in_macro_expansion,none,
"macro expansion cannot introduce %0",
(DescriptiveDeclKind))
Expand Down
5 changes: 4 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9595,9 +9595,10 @@ ParserResult<MacroDecl> Parser::parseDeclMacro(DeclAttributes &attributes) {
DeclName macroFullName;

// Parameter list.
DefaultArgumentInfo defaultArgs;
SmallVector<Identifier, 2> namePieces;
auto parameterResult = parseSingleParameterClause(
ParameterContextKind::Macro, &namePieces, nullptr);
ParameterContextKind::Macro, &namePieces, &defaultArgs);
status |= parameterResult;
parameterList = parameterResult.getPtrOrNull();

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

defaultArgs.setFunctionContext(macro, macro->getParameterList());

return dcc.fixupParserResult(status, macro);
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,14 @@ static ParserStatus parseDefaultArgument(
case Parser::ParameterContextKind::Initializer:
case Parser::ParameterContextKind::EnumElement:
case Parser::ParameterContextKind::Subscript:
case Parser::ParameterContextKind::Macro:
break;
case Parser::ParameterContextKind::Closure:
diagID = diag::no_default_arg_closure;
break;
case Parser::ParameterContextKind::Curried:
diagID = diag::no_default_arg_curried;
break;
case Parser::ParameterContextKind::Macro:
diagID = diag::no_default_arg_macro;
break;
}

assert((diagID.ID != DiagID()) == !defaultArgs &&
Expand Down Expand Up @@ -704,7 +702,8 @@ mapParsedParameters(Parser &parser,
paramContext == Parser::ParameterContextKind::Operator ||
paramContext == Parser::ParameterContextKind::Initializer ||
paramContext == Parser::ParameterContextKind::EnumElement ||
paramContext == Parser::ParameterContextKind::Subscript) &&
paramContext == Parser::ParameterContextKind::Subscript ||
paramContext == Parser::ParameterContextKind::Macro) &&
"Default arguments are only permitted on the first param clause");

if (param.DefaultArg) {
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
MD->diagnose(diag::macro_without_role, MD->getName());

TypeChecker::checkParameterList(MD->getParameterList(), MD);
checkDefaultArguments(MD->getParameterList());

// Check the macro definition.
switch (auto macroDef = MD->getDefinition()) {
Expand Down
9 changes: 7 additions & 2 deletions test/Macros/macros_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ func testExternalMacroOutOfPlace() {

@freestanding(expression)
public macro macroWithDefaults(_: Int = 17) = #externalMacro(module: "A", type: "B")
// expected-error@-1{{default arguments are not allowed in macros}}
// expected-warning@-2{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
// expected-note@-2{{'macroWithDefaults' declared here}}

func callMacroWithDefaults() {
_ = #macroWithDefaults()
// expected-error@-1 {{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'}}
}

// Make sure we don't allow macros to prevent type folding.
@attached(member)
Expand Down
2 changes: 1 addition & 1 deletion test/Serialization/Inputs/def_macros.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@freestanding(expression) public macro publicStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
@freestanding(expression) public macro publicStringify<T>(_ value: T, label: String? = nil) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")

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

Expand Down
1 change: 1 addition & 0 deletions test/Serialization/macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import def_macros

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

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