Are macros supported on Linux yet?

ConformanceMacro was removed as per https://github.com/apple/swift-evolution/blob/main/proposals/0402-extension-macros.md. Doug's example repo is apparently outdated.

Could you try example plugin in swift-syntax repo? https://github.com/apple/swift-syntax/tree/main/Examples

Edit: Ooh macro examples are guarded by #if os(macOS) https://github.com/apple/swift-syntax/blob/13f113e8a180d4cf1b4460d7e3db697cdf3a3fa8/Examples/Package.swift#L65-L84
Could you try it removing that #if guard, and see if it works?

I am still getting errors like this, so it's not fully working yet?

To be clear, it should fully work. If not, it's a bug and we should fix it asap. So please let me know.

From Swift.org - Download Swift, I downloaded swift-DEVELOPMENT-SNAPSHOT-2023-09-01-a-ubuntu20.04-aarch64.tar.gz , cloned swift-syntax repo, applied this patch

diff --git a/Examples/Package.swift b/Examples/Package.swift
index f10a7c4a..821d3939 100644
--- a/Examples/Package.swift
+++ b/Examples/Package.swift
@@ -62,7 +62,7 @@ let package = Package(
 // The following targets are added only if the OS is macOS. This is because Swift macros are currently
 // not available on other platforms. As a result, we're guarding these targets with `#if os(macOS)`
 // to ensure that they are only included in the package when building on a macOS system.
-#if os(macOS)
+#if os(macOS) || os(Linux)
 package.targets.append(
   contentsOf: [
     .target(

in the Examples directory run MacroExamplesPlayground

$ cd Examples
$ ~/swift-DEVELOPMENT-SNAPSHOT-2023-09-01-a-ubuntu20.04-aarch64/usr/bin/swift run MacroExamplesPlayground

and succeeded :slight_smile:

(Unfortunately 5.9 snapshots haven't got the macro patches at this moment)

8 Likes

Thanks, I reproduced locally after patching the package manifest as you say, worked fine as for you.

I then downloaded the CI-built 5.9.0 toolchain for Ubuntu 20.04 (which I got by clicking the check mark on this commit, picking the Ubuntu 20.04 toolchain, then finding the download link at the end of the build log) and did the same, but it crashed the compiler this time:

swift-frontend: /home/build-user/swift/lib/Serialization/Serialization.cpp:1669: void swift::serialization::Serializer::writeLocalNormalProtocolConformance(swift::NormalProtocolConformance *): Assertion `conformance->isComplete() || allowCompilerErrors()' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/foo/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /home/foo/swift-syntax/Examples/Sources/MacroExamples/Playground/main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -I /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug -color-diagnostics -enable-testing -g -module-cache-path /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /home/foo/usr/bin/swift-driver -entry-point-function-name MacroExamplesPlayground_main -load-plugin-executable /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/MacroExamplesImplementation#MacroExamplesImplementation -empty-abi-descriptor -resource-dir /home/foo/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fPIC -module-name MacroExamplesPlayground -package-name examples -plugin-path /home/foo/usr/lib/swift/host/plugins -plugin-path /home/foo/usr/local/lib/swift/host/plugins -emit-module-doc-path /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/MacroExamplesPlayground.swiftdoc -emit-module-source-info-path /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/MacroExamplesPlayground.swiftsourceinfo -emit-dependencies-path /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/MacroExamplesPlayground.build/MacroExamplesPlayground.emit-module.d -o /home/foo/swift-syntax/Examples/.build/x86_64-unknown-linux-gnu/debug/MacroExamplesPlayground.swiftmodule
1.      Swift version 5.9-dev (LLVM 97263007aaf8c2b, Swift b850c17a6db76c2)
2.      Compiling with the current language version
3.      While evaluating request ExecuteSILPipelineRequest(Run pipelines { Non-Diagnostic Mandatory Optimizations, Serialization, Rest of Onone } on SIL for MacroExamplesPlayground)
4.      While running pass #1236 SILModuleTransform "SerializeSILPass".
5.      While serializing protocol conformance to 'Observable' (in module 'MacroExamplesInterface') for type 'Dog' (declared at [/home/foo/swift-syntax/Examples/Sources/MacroExamples/Playground/main.swift:90:7 - line:101:1] RangeText="class Dog {
  var name: String?
  var treat: Treat?

  var isHappy: Bool = true

  init() {}

  func bark() {
    print("bork bork")
  }
")
Stack dump without symbol names

Let me know if you see the same, or if that doesn't matter.

Edit: one thing I just noticed is that this Macro target was not there in the 5.9 branch of swift-syntax, so maybe the issue is because I'm trying to build this Macro example from latest swift-syntax trunk with that Swift 5.9.0 build?

Second edit: I just tried the same example with the latest Sep. 1 5.9 snapshot build and cannot reproduce the crash, so that 5.9.0 crash must not be because of the Swift version. Maybe some other commit on the release/5.9 branch fixed the issue, though I don't see what it could be, or that 5.9.0 CI build I linked above is just broken for some spurious reason. @rintaro, you may want to run the final 5.9.0 builds through this test to make sure.

Thank you for looking into this @Finagolfin.

I confirmed this crash (assertion failure) happens even in the macOS toolchain from that PR. We're looking into it.

1 Like

This looks exactly like the crash I fixed here, which landed in the 5.9.0 branch after the toolchain you mention was built. That fix has been on main and release/5.9 for a while longer, we just pulled it into release/5.9.0 fairly late.

Doug

Heads-up that the Linux snapshots for both the main and 5.9 compilers now include support for macros.

Doug

26 Likes

great job to everyone who worked on this! i have been wanting to try out the macros feature for a while now and i’m glad this made it into the 5.9 release.

4 Likes

Thanks for the explanation. I just wanted to make sure whatever fix made it work for the 5.9 branch was also pulled into the separate 5.9.0 branch, sounds like it has.