-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
Description
The following code fails to compile:
@Observable
class BrokenViewModel {
@MyEmptyMacro // TODO: make this the best macro ever
var x = 1
}Because the macro expansion adds @ObservationTracked directly after the comment, with no line break in between:
Reproduction
// ✅
@Observable
class WorkingViewModel {
// TODO: make this the best macro ever
@MyEmptyMacro
var x = 1
}
// 🛑
@Observable
class BrokenViewModel {
@MyEmptyMacro // TODO: make this the best macro ever
var x = 1
}You need an extra macro in the project to make it work, so here's a sample Swift package that reproduces the issue if you build it in Xcode:
Expected behavior
When expanding the macro, @ObservationTracked is added on a new line in order to preserve the comment without disabling the declaration of the underscored backing var. (Or the comment is omitted from the macro expansion, but that seems needlessly destructive.)
Environment
swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx15.0
Xcode Version 26.0 beta 7. Also reproduced with Xcode 16.4.
Additional information
See also: FB15168953: @Entry macro expansion breaks if there is a // comment after the delcaration [sic]. It was basically the same bug, and it was fixed in October 2024. If you have access to internal Apple things, maybe you can see how that was fixed.
Side note: I noticed that, in the non-broken @Observable class, where the comment is on the previous line, the comment ends up being duplicated in the macro expansion. Not a problem, per se, but I thought it was worth noting:
