Skip to content

@Observable macro breaks when property has attached macro with a // comment on the same line #84145

@ZevEisenberg

Description

@ZevEisenberg

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:

Image

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:

BugReport.zip

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:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions