Skip to content

[Macros] Provide the freestanding macro role for expansion operations. #66509

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
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
10 changes: 10 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7101,6 +7101,16 @@ ERROR(macro_in_nested,none,
ERROR(macro_without_role,none,
"macro %0 must declare its applicable roles via '@freestanding' or @attached'",
(DeclName))
ERROR(macro_result_type_cannot_be_used,none,
"only a freestanding expression macro can produce a result of type %0",
(Type))
NOTE(macro_remove_result_type,none,
"remove the result type if the macro does not produce a value",
())
NOTE(macro_make_freestanding_expression,none,
"make this macro a freestanding expression macro", ())
ERROR(macro_multiple_freestanding_roles,none,
"macro can only have a single freestanding role", ())
ERROR(macro_expansion_missing_pound,none,
"expansion of macro %0 requires leading '#'", (DeclName))
ERROR(macro_expansion_missing_arguments,none,
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3357,12 +3357,18 @@ TypeRepr *ValueDecl::getResultTypeRepr() const {
returnRepr = FD->getResultTypeRepr();
} else if (auto *SD = dyn_cast<SubscriptDecl>(this)) {
returnRepr = SD->getElementTypeRepr();
} else if (auto *MD = dyn_cast<MacroDecl>(this)) {
returnRepr = MD->resultType.getTypeRepr();
}

return returnRepr;
}

TypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
// FIXME: Macros don't allow opaque result types yet.
if (isa<MacroDecl>(this))
return nullptr;

auto *returnRepr = this->getResultTypeRepr();

auto *dc = getDeclContext();
Expand Down
21 changes: 21 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extension MacroRole {
case 0x10: self = .member
case 0x20: self = .peer
case 0x40: self = .conformance
case 0x80: self = .codeItem

default: fatalError("unknown macro role")
}
}
Expand Down Expand Up @@ -414,6 +416,7 @@ func expandFreestandingMacro(
macroKind: UInt8,
discriminatorText: UnsafePointer<UInt8>,
discriminatorTextLength: Int,
rawMacroRole: UInt8,
sourceFilePtr: UnsafeRawPointer,
sourceLocationPtr: UnsafePointer<UInt8>?,
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
Expand Down Expand Up @@ -446,18 +449,21 @@ func expandFreestandingMacro(
)
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)

let macroRole = MacroRole(rawMacroRole: rawMacroRole)
let expandedSource: String?
switch MacroPluginKind(rawValue: macroKind)! {
case .InProcess:
expandedSource = expandFreestandingMacroInProcess(
macroPtr: macroPtr,
macroRole: macroRole,
diagEnginePtr: diagEnginePtr,
expansionSyntax: expansion,
sourceFilePtr: sourceFilePtr,
discriminator: discriminator)
case .Executable:
expandedSource = expandFreestandingMacroIPC(
macroPtr: macroPtr,
macroRole: macroRole,
diagEnginePtr: diagEnginePtr,
expansionSyntax: expansion,
sourceFilePtr: sourceFilePtr,
Expand Down Expand Up @@ -485,6 +491,7 @@ func expandFreestandingMacro(

func expandFreestandingMacroIPC(
macroPtr: UnsafeRawPointer,
macroRole: MacroRole,
diagEnginePtr: UnsafeMutablePointer<UInt8>,
expansionSyntax: FreestandingMacroExpansionSyntax,
sourceFilePtr: UnsafePointer<ExportedSourceFile>,
Expand All @@ -502,9 +509,21 @@ func expandFreestandingMacroIPC(

let macro = macroPtr.assumingMemoryBound(to: ExportedExecutableMacro.self).pointee

// Map the macro role.
let pluginMacroRole: PluginMessage.MacroRole
switch macroRole {
case .accessor, .member, .memberAttribute, .peer, .conformance:
preconditionFailure("unhandled macro role for freestanding macro")

case .expression: pluginMacroRole = .expression
case .declaration: pluginMacroRole = .freeStandingDeclaration
case .codeItem: pluginMacroRole = .codeItem
}

// Send the message.
let message = HostToPluginMessage.expandFreestandingMacro(
macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
macroRole: pluginMacroRole,
discriminator: discriminator,
syntax: PluginMessage.Syntax(syntax: Syntax(expansionSyntax), in: sourceFilePtr)!)
do {
Expand Down Expand Up @@ -541,6 +560,7 @@ func expandFreestandingMacroIPC(

func expandFreestandingMacroInProcess(
macroPtr: UnsafeRawPointer,
macroRole: MacroRole,
diagEnginePtr: UnsafeMutablePointer<UInt8>,
expansionSyntax: FreestandingMacroExpansionSyntax,
sourceFilePtr: UnsafePointer<ExportedSourceFile>,
Expand Down Expand Up @@ -580,6 +600,7 @@ func expandFreestandingMacroInProcess(

return SwiftSyntaxMacroExpansion.expandFreestandingMacro(
definition: macro,
macroRole: macroRole,
node: node,
in: context
)
Expand Down
2 changes: 2 additions & 0 deletions lib/ASTGen/Sources/ASTGen/PluginMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal enum HostToPluginMessage: Codable {
/// Expand a '@freestanding' macro.
case expandFreestandingMacro(
macro: PluginMessage.MacroReference,
macroRole: PluginMessage.MacroRole? = nil,
discriminator: String,
syntax: PluginMessage.Syntax
)
Expand Down Expand Up @@ -91,6 +92,7 @@ internal enum PluginToHostMessage: Codable {
case member
case peer
case conformance
case codeItem
}

struct SourceLocation: Codable {
Expand Down
2 changes: 1 addition & 1 deletion lib/IDETool/SyntacticMacroExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void SyntacticMacroExpansionInstance::expand(
SourceFile *SF, const MacroExpansionSpecifier &expansion,
SourceEditConsumer &consumer) {

// Find the expansion at 'expantion.offset'.
// Find the expansion at 'expansion.offset'.
MacroExpansionFinder expansionFinder(
SourceMgr,
SourceMgr.getLocForOffset(*SF->getBufferID(), expansion.offset));
Expand Down
35 changes: 35 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,17 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
llvm_unreachable("should always be type-checked already");
}

/// Determine the number of bits set.
static unsigned numBitsSet(uint64_t value) {
unsigned count = 0;
for (uint64_t i : range(0, 63)) {
if (value & (uint64_t(1) << i))
++count;
}

return count;
}

void visitMacroDecl(MacroDecl *MD) {
TypeChecker::checkDeclAttributes(MD);
checkAccessControl(MD);
Expand Down Expand Up @@ -2060,6 +2071,30 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
break;
}
}

// If the macro has a (non-Void) result type, it must have the freestanding
// expression role. Other roles cannot have result types.
if (auto resultTypeRepr = MD->getResultTypeRepr()) {
if (!MD->getMacroRoles().contains(MacroRole::Expression) &&
!MD->getResultInterfaceType()->isEqual(Ctx.getVoidType())) {
auto resultType = MD->getResultInterfaceType();
Ctx.Diags.diagnose(
MD->arrowLoc, diag::macro_result_type_cannot_be_used, resultType)
.highlight(resultTypeRepr->getSourceRange());
Ctx.Diags.diagnose(MD->arrowLoc, diag::macro_make_freestanding_expression)
.fixItInsert(MD->getAttributeInsertionLoc(false),
"@freestanding(expression)\n");
Ctx.Diags.diagnose(MD->arrowLoc, diag::macro_remove_result_type)
.fixItRemove(SourceRange(MD->arrowLoc, resultTypeRepr->getEndLoc()));
}
}

// A macro can only have a single freestanding macro role.
MacroRoles freestandingRolesInhabited =
MD->getMacroRoles() & getFreestandingMacroRoles();
if (numBitsSet(freestandingRolesInhabited.toRaw()) > 1) {
MD->diagnose(diag::macro_multiple_freestanding_roles);
}
}

void visitMacroExpansionDecl(MacroExpansionDecl *MED) {
Expand Down
13 changes: 11 additions & 2 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ extern "C" ptrdiff_t swift_ASTGen_checkMacroDefinition(

extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
void *diagEngine, void *macro, uint8_t externalKind,
const char *discriminator, ptrdiff_t discriminatorLength, void *sourceFile,
const char *discriminator, ptrdiff_t discriminatorLength,
uint8_t rawMacroRole, void *sourceFile,
const void *sourceLocation, const char **evaluatedSource,
ptrdiff_t *evaluatedSourceLength);

Expand Down Expand Up @@ -901,6 +902,13 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
#endif
});

// Only one freestanding macro role is permitted, so look at the roles to
// figure out which one to use.
MacroRole macroRole =
macroRoles.contains(MacroRole::Expression) ? MacroRole::Expression
: macroRoles.contains(MacroRole::Declaration) ? MacroRole::Declaration
: MacroRole::CodeItem;

auto macroDef = macro->getDefinition();
switch (macroDef.kind) {
case MacroDefinition::Kind::Undefined:
Expand Down Expand Up @@ -961,7 +969,8 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
swift_ASTGen_expandFreestandingMacro(
&ctx.Diags, externalDef->opaqueHandle,
static_cast<uint32_t>(externalDef->kind), discriminator->data(),
discriminator->size(), astGenSourceFile,
discriminator->size(),
static_cast<uint32_t>(macroRole), astGenSourceFile,
expansion->getSourceRange().Start.getOpaquePointerValue(),
&evaluatedSourceAddress, &evaluatedSourceLength);
if (!evaluatedSourceAddress)
Expand Down
20 changes: 20 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ public struct StringifyMacro: ExpressionMacro {
}
}

public struct ExprAndDeclMacro: ExpressionMacro, DeclarationMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
guard let argument = macro.argumentList.first?.expression else {
fatalError("boom")
}

return "(\(argument), \(StringLiteralExprSyntax(content: argument.description)))"
}

public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> [DeclSyntax] {
return []
}
}

public struct StringifyAndTryMacro: ExpressionMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
Expand Down
24 changes: 24 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,27 @@ func testHasEqualsSelf(
_ = (zP == true)
_ = (wP == true)
}

// Macro whose implementation is both an expression and declaration macro.
@freestanding(declaration)
macro AsDeclMacro<T>(_ value: T) = #externalMacro(module: "MacroDefinition", type: "ExprAndDeclMacro")

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

func testExpressionAndDeclarationMacro() {
#AsExprMacro(1 + 1) // expected-warning{{expression of type '(Int, String)' is unused}}
struct Inner {
#AsDeclMacro(1 + 1)
}
#AsDeclMacro(1 + 1)
}

// Expression macro implementation with declaration macro role
@freestanding(declaration) macro stringifyAsDeclMacro<T>(_ value: T) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
func testExpressionAsDeclarationMacro() {
#if TEST_DIAGNOSTICS
#stringifyAsDeclMacro(1+1)
// expected-error@-1{{macro implementation type 'StringifyMacro' doesn't conform to required protocol 'DeclarationMacro' (from macro 'stringifyAsDeclMacro')}}
#endif
}
4 changes: 2 additions & 2 deletions test/Macros/macro_plugin_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

// CHECK: ->(plugin:[[#PID:]]) {"getCapability":{}}
// CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}}
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"\"123\"\n + \"foo \""}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testStringWithError","typeName":"TestStringWithErrorMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":336},"source":"#testStringWithError(321)"}}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testStringWithError","typeName":"TestStringWithErrorMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":336},"source":"#testStringWithError(321)"}}}
// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[{"fixIts":[],"highlights":[],"message":"message from plugin","notes":[],"position":{"fileName":"BUILD_DIR{{.*}}test.swift","offset":336},"severity":"error"}],"expandedSource":"\"bar\""}}

//--- test.swift
Expand Down
6 changes: 3 additions & 3 deletions test/Macros/macro_plugin_error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":[[#]]},"source":"#fooMacro(1)"}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":[[#]]},"source":"#fooMacro(1)"}}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"invalidResponse":{}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":8,"offset":[[#]]},"source":"#fooMacro(2)"}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":8,"offset":[[#]]},"source":"#fooMacro(2)"}}}
// ^ This messages causes the mock plugin exit because there's no matching expected message.

// CHECK: ->(plugin:[[#PID2:]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":10,"offset":[[#]]},"source":"#fooMacro(3)"}}}
// CHECK: ->(plugin:[[#PID2:]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":10,"offset":[[#]]},"source":"#fooMacro(3)"}}}
// CHECK-NEXT: <-(plugin:[[#PID2:]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"3.description"}}

//--- test.swift
Expand Down
6 changes: 3 additions & 3 deletions test/Macros/macro_plugin_server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(a + b)"}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(a + b)"}}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(a + b, \"a + b\")"}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"EvilMacros","name":"evil","typeName":"CrashingMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#evil(42)"}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"EvilMacros","name":"evil","typeName":"CrashingMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{{{.+}}},"source":"#evil(42)"}}}
// ^ This crashes the plugin server.

// CHECK-NEXT: ->(plugin:[[#PID2:]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libEvilMacros.dylib","moduleName":"EvilMacros"}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"loadPluginLibraryResult":{"diagnostics":[],"loaded":true}}
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(b + a)"}}}
// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"${{.*}}","macro":{"moduleName":"MacroDefinition","name":"stringify","typeName":"StringifyMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{{{.+}}},"source":"#stringify(b + a)"}}}
// CHECK-NEXT: <-(plugin:[[#PID2]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"(b + a, \"b + a\")"}}

@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
Expand Down
20 changes: 19 additions & 1 deletion test/Macros/macros_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal struct X { } // expected-note{{type declared here}}
// expected-warning@-1{{external macro implementation type}}

struct ZZZ {
macro m5() -> Int = #externalMacro(module: "BuiltinMacros", type: "Blah")
macro m5() = #externalMacro(module: "BuiltinMacros", type: "Blah")
// expected-error@-1{{macro 'm5()' can only be declared at file scope}}
// expected-error@-2{{macro 'm5()' must declare its applicable roles}}
// expected-warning@-3{{external macro implementation type}}
Expand Down Expand Up @@ -200,3 +200,21 @@ struct SomeType {
// expected-error@-2{{use of protocol 'Hashable' as a type must be written 'any Hashable'}}
// expected-error@-3{{external macro implementation type}}
}



@freestanding(declaration) macro nonExpressionReturnsInt<T>(_: T) -> Int = #externalMacro(module: "A", type: "B")
// expected-warning@-1{{external macro implementation type}}
// expected-error@-2{{only a freestanding expression macro can produce a result of type 'Int'}}
// expected-note@-3{{make this macro a freestanding expression macro}}{{1-1=@freestanding(expression)\n}}
// expected-note@-4{{remove the result type if the macro does not produce a value}}{{67-74=}}

@freestanding(declaration) macro nonExpressionReturnsVoid<T>(_: T) -> Void = #externalMacro(module: "A", type: "B")
// expected-warning@-1{{external macro implementation type}}


@freestanding(expression)
@freestanding(declaration)
macro multipleFreestandingRoles<T>(_: T) -> Void = #externalMacro(module: "A", type: "B")
// expected-warning@-1{{external macro implementation type}}
// expected-error@-2{{macro can only have a single freestanding role}}
1 change: 1 addition & 0 deletions test/Macros/parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protocol Q { associatedtype Assoc }
@freestanding(expression) @freestanding(declaration, names: named(Foo)) @attached(accessor)
macro m10(_: String) = #externalMacro(module: "A", type: "M4")
// expected-warning@-1{{external macro implementation type 'A.M4' could not be found for macro 'm10'}}
// expected-error@-2{{macro can only have a single freestanding role}}

@attached(
accessor,
Expand Down