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

[SR-15306] Compiler Issue with SwiftUI State Property and Generic Property #57628

Open
Mordil opened this issue Oct 12, 2021 · 3 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@Mordil
Copy link

Mordil commented Oct 12, 2021

Previous ID SR-15306
Radar None
Original Reporter @Mordil
Type Bug

Attachment: Download

Environment

Xcode 12.5 (12E262)

macOS 11.6

MacBook Pro (16-inch, 2019)

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 1f5055b3203ab7ae8c500f178b983b68

Issue Description:

Using the Time library, I've run into a compiler issue with SwiftUI and @State with initialization of View objects:

struct Failure: View {
  @State
  private var offset: CGFloat
  private let clockChime: ClockChime<Minute>

  var body: some View { /* */ }

  init() {
    self.offset = 0 // compiler error: self used before it is fully initialized
    self.clockChime = Clocks.system.chime(every: .minutes(1))
  }
}

This only occurs when using the ClockChime type - if it's replaced with plain Swift types such as Bool or String it compiles just fine.

It also compiles just fine if the @State property definition is default initialized and ignored in the initializer

@State
private var offset: CGFloat = 0
@Mordil
Copy link
Author

Mordil commented Oct 12, 2021

I have attached a minimal reproducer of this bug

@LucianoPAlmeida
Copy link
Collaborator

I think this is the same problem as SR-13896, and it's due to a limitation in property wrapper initialization. You can work around this by explicitly initializing the property wrapper `self._offset = State<CGFloat>(wrappedValue: 0)`.

Also reproducible in Xcode 13 (13A233)
cc @hborla

@Mordil
Copy link
Author

Mordil commented Oct 12, 2021

Yes, it sounds like this is a duplicate of SR-13896

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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.
Projects
None yet
Development

No branches or pull requests

2 participants