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-6945] Error in struct init, saying 'self' used before assignment for the assignment itself #49493

Open
milseman mannequin opened this issue Feb 7, 2018 · 3 comments
Open

Comments

@milseman
Copy link
Mannequin

@milseman milseman mannequin commented Feb 7, 2018

Previous ID SR-6945
Radar None
Original Reporter @milseman
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: b3efc2af15234adf6e9b4959548a8d2d

is duplicated by:

  • SR-15670 'let' property reinitialization checking is flow-insensitive

Issue Description:

When initializing a struct, having a conditional call to another init followed by an early return produces a compilation error about whether the struct was properly initialized.

Reproducer:
https://gist.github.com/milseman/d8360121856f4780bd5172cc94f96251

struct S1 {
  var stored: UInt?

  init(_ x: UInt) {
    if x > 42 {
      self.init(_big: x)
      return
    }
    self.stored = x // <--- error: 'self' used before 'self.init' call or assignment to 'self'
  }

  init(_big x: UInt) {
    self.stored = x
  }
}

struct S2 {
  var stored: UInt?

  init(_ x: UInt) {
    if x > 42 {
      self.init(_big: x)
      return
    }
    self.init(_small: x)
  }

  init(_big x: UInt) {
    self.stored = x
  }

  init(_small x: UInt) {
    self.stored = x
  }
}

S1 has the error, S2 does not.

@gottesmm
Copy link
Member

@gottesmm gottesmm commented Feb 7, 2018

@swift-ci sync

@belkadan
Copy link
Contributor

@belkadan belkadan commented Feb 8, 2018

Workaround: use self = S1(_big: x) instead.

We try to decide up front if a struct initializer is going to be per-field or delegating, but I guess I screwed that check up.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Feb 8, 2018

Not a regression, at least.

@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
Projects
None yet
Development

No branches or pull requests

2 participants