Skip to content

@Observable Macro does not support properties with the package access modifier #71060

@treastrain

Description

@treastrain

Description

If a property with package access modifier exists in a class with @Observable Macro, the build will fail.

Reproduction

✅ OK

import Observation

@Observable
class MyObservable1 {
    var value = 0
}

❌ Build failed

import Observation

@Observable
class MyObservable2 {
    package var value = 0
}

% swift build

Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
/var/folders/78/4hltfk2n2fz2f88371bhlp_h0000gn/T/swift-generated-sources/@__swiftmacro_20ObservablePlayground13MyObservable2C5value18ObservationTrackedfMp_.swift:2:5: error: duplicate modifier
    package  var _value  = 0
    ^~~~~~~
/Users/treastrain/Developer/github.com/treastrain/ObservablePlayground/Sources/main.swift:11:17: note: in expansion of macro 'ObservationTracked' here
    package var value = 0
                ^~~~~
/var/folders/78/4hltfk2n2fz2f88371bhlp_h0000gn/T/swift-generated-sources/@__swiftmacro_20ObservablePlayground13MyObservable2C5value18ObservationTrackedfMp_.swift:1:21: note: modifier already specified here
@ObservationIgnored private
                    ^~~~~~~
/Users/treastrain/Developer/github.com/treastrain/ObservablePlayground/Sources/main.swift:11:17: note: in expansion of macro 'ObservationTracked' here
    package var value = 0
                ^~~~~
/var/folders/78/4hltfk2n2fz2f88371bhlp_h0000gn/T/swift-generated-sources/@__swiftmacro_20ObservablePlayground13MyObservable2C5value18ObservationTrackedfMp_.swift:2:5: error: duplicate modifier
    package  var _value  = 0
    ^~~~~~~
/Users/treastrain/Developer/github.com/treastrain/ObservablePlayground/Sources/main.swift:11:17: note: in expansion of macro 'ObservationTracked' here
    package var value = 0
                ^~~~~
/var/folders/78/4hltfk2n2fz2f88371bhlp_h0000gn/T/swift-generated-sources/@__swiftmacro_20ObservablePlayground13MyObservable2C5value18ObservationTrackedfMp_.swift:1:21: note: modifier already specified here
@ObservationIgnored private
                    ^~~~~~~
/Users/treastrain/Developer/github.com/treastrain/ObservablePlayground/Sources/main.swift:11:17: note: in expansion of macro 'ObservationTracked' here
    package var value = 0
                ^~~~~
error: fatalError

Expected behavior

Expect the access modifier for properties with @ObservationIgnored generated by that macro to be private rather than the syntactically incorrect package private.

Environment

% swiftc -version

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    ObservationArea → standard library: The `Observation` module under the standard library umbrellaaccess controlFeature → modifiers: Access control and access levelsbugA 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