swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.69k stars 10.39k forks source link

Freestanding declaration macro fails inside classes: Expected '{' in body of function declaration #68704

Open juozasvalancius opened 1 year ago

juozasvalancius commented 1 year ago

Description

Using a freestanding declaration macro inside a class produces a build error:

Expected '{' in body of function declaration

Here is the expanded macro:

func myFunction() {
}

The same code compiles if the macro is used inside a struct.

Steps to reproduce

Create a swift package using the macro template.

Client code:

import MyMacro

struct MyStruct {
    #blankFunction // works ok here
}

class MyClass {
    #blankFunction // fails here
}

Macro declaration:

@freestanding(declaration, names: named(myFunction))
public macro blankFunction() = #externalMacro(
    module: "MyMacroMacros",
    type: "BlankFunctionMacro"
)

Macro implementation:

import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct BlankFunctionMacro: DeclarationMacro {
    public static func expansion(
        of node: some FreestandingMacroExpansionSyntax,
        in context: some MacroExpansionContext
    ) throws -> [DeclSyntax] {
        return ["func myFunction() {}"]
    }
}

@main
struct MyMacroPlugin: CompilerPlugin {
    let providingMacros: [Macro.Type] = [
        BlankFunctionMacro.self,
    ]
}

Expected behavior

swift run MyMacroClient should compile and run without any errors.

Environment

huyaoyu commented 11 months ago

Hi facing the same issue now. Just a follow-up. What's the progress regarding this issue? Thank you!

drseg commented 10 months ago

Also here. Thanks for reporting, hoping for a fix...

regexident commented 10 months ago

It's also current blocking me on a macros-heavy project.

MihaelIsaev commented 7 months ago

Same here, blocking awesome ideas realisation. Any updates on this?

MihaelIsaev commented 7 months ago

Tried on Swift 5.9.2 and 5.10 - doesn't work. 🙅‍♂️

Tried on Swift 6.0 DEVELOPMENT-SNAPSHOT-2024-04-30-a-osx and it works! 🚀