Skip to content

[Macros] Disallow expression macro as default argument #68782

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
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7493,6 +7493,9 @@ ERROR(extension_macro_invalid_conformance,none,
ERROR(macro_attached_to_invalid_decl,none,
"'%0' macro cannot be attached to %1 (%base2)",
(StringRef, DescriptiveDeclKind, const Decl *))
ERROR(macro_as_default_argument, none,
"non-built-in macro cannot be used as default argument",
())
ERROR(conformance_macro,none,
"conformance macros are replaced by extension macros",
())
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,12 @@ Expr *DefaultArgumentExprRequest::evaluate(Evaluator &evaluator,
auto *initExpr = param->getStructuralDefaultExpr();
assert(initExpr);

// Prohibit default argument that is a non-built-in macro to avoid confusion.
if (isa<MacroExpansionExpr>(initExpr)) {
ctx.Diags.diagnose(initExpr->getLoc(), diag::macro_as_default_argument);
return new (ctx) ErrorExpr(initExpr->getSourceRange(), ErrorType::get(ctx));
}

// If the param has an error type, there's no point type checking the default
// expression, unless we are type checking for code completion, in which case
// the default expression might contain the code completion token.
Expand Down
32 changes: 32 additions & 0 deletions test/Macros/macro_default_argument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// REQUIRES: swift_swift_parser

// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath

// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s

@freestanding(expression)
macro MagicLine() -> Int = #externalMacro(module: "MacroDefinition", type: "MagicLineMacro")

struct LineContainer {
let line: Int
}

func partOfDefaultArgumentOkay(container: LineContainer = .init(line: #MagicLine)) {
print(container.line)
}

func parenthesizedExpansionAtDeclOkay(line: Int = (#MagicLine)) {
print(line)
}

func builtInOkay(line: Int = #line) {
print(line)
}

// expected-error@+1{{non-built-in macro cannot be used as default argument}}
func asDefaultArgument(line: Int = #MagicLine) {
print(line)
}

asDefaultArgument()
2 changes: 1 addition & 1 deletion test/ModuleInterface/unbuildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// RUN: not %target-swift-frontend -typecheck-module-from-interface %t/UnbuildableCurrent.swiftinterface 2>&1 | %FileCheck -check-prefixes=ALL,CURRENT-VERIFY %s
// RUN: not %target-swift-frontend -typecheck-module-from-interface %t/UnbuildableFuture.swiftinterface 2>&1 | %FileCheck -check-prefixes=ALL,FUTURE-VERIFY %s

// ALL: Unbuildable{{[^.]+}}.swiftinterface:{{[0-9]+}}:{{[0-9]+}}: error: no macro named 'somethingYouveNeverHeardOf'
// ALL: Unbuildable{{[^.]+}}.swiftinterface:{{[0-9]+}}:{{[0-9]+}}: error: non-built-in macro cannot be used as default argument

#if CURRENT
import UnbuildableCurrent
Expand Down