Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init accessors affect initialization of Optionals #73922

Open
CrystDragon opened this issue May 27, 2024 · 0 comments
Open

Init accessors affect initialization of Optionals #73922

CrystDragon opened this issue May 27, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@CrystDragon
Copy link

CrystDragon commented May 27, 2024

Description

This code compiles fine:

struct A {
  let a: Int?
  init() {
    self.a = 10
  }
}

But when using an init accessor to wrap the original property, the compiler will emit an error:

struct AA {
  let a: Int?
  var aa: Int? {
    get {
      a
    }
    @storageRestrictions(initializes: a)
    init {
      a = newValue
    }
  }
  init() {
    self.a = 10  // Error: Immutable value 'self.a' may only be initialized once
  }
}

Note that neither self.a = 10 nor self.aa = 10 is permitted by the compiler.

The above behaviors are inconsistent.

Reproduction

The latter code snippet in "Description".

Expected behavior

No compiler errors.

Environment

  • Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
  • Xcode 15.3 (15E204a)

Additional information

No response

@CrystDragon CrystDragon added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 27, 2024
@CrystDragon CrystDragon changed the title Init accessors will affect optional initialization Init accessors affects optional initialization May 27, 2024
@CrystDragon CrystDragon changed the title Init accessors affects optional initialization Init accessors affects initialization of Optionals May 27, 2024
@CrystDragon CrystDragon changed the title Init accessors affects initialization of Optionals Init accessors affect initialization of Optionals May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant