swiftlang / swift

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

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

Open juozasvalancius opened 9 months ago

juozasvalancius commented 9 months 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 6 months ago

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

drseg commented 5 months ago

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

regexident commented 5 months ago

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

MihaelIsaev commented 2 months ago

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

MihaelIsaev commented 2 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! 🚀