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-1469] Diagnostic for init? reported at the end despite of early return #44078

Closed
dduan opened this issue May 10, 2016 · 4 comments
Closed
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement

Comments

@dduan
Copy link
Collaborator

dduan commented May 10, 2016

Previous ID SR-1469
Radar None
Original Reporter @dduan
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee nubbel (JIRA)
Priority Medium

md5: 86204ca1807df504b7b7f3c9d8e2cf61

Issue Description:

When a property is not initialized, the Swift compiler points to the end of a initializer and complains. This can be confusing when the initializer has early returns. For example:

struct C {
    let a: Int
    let b: Int // note: 'self.b' not initialized
    init?(x: Int, y: Int) {
        self.a = x
        if y == 42 { return }
        // many lines later
        self.b = y
    } // error: return from initializer without initializing all stored properties
}

Note the error is caused by the early return, but it's not immediately clear given that `b` appears to have been initialized near the end.

Solution: issue the diagnostic message at the early return statement.

@swift-ci
Copy link
Collaborator

Comment by Dominique d'Argent (JIRA)

I made some progress on this, such that it handles the example above correctly:

struct C {
  let a: Int
  let b: Int // note: 'self.b' not initialized
  init?(x: Int, y: Int) {
    self.a = x
    if y == 42 {
      return // error: return from initializer without initializing all stored properties
    }
    // many lines later
    self.b = y
  }
}

However, it currently produces wrong diagnostics in cases where the early return is actually on the "good path":

struct C {
  let a: Int
  let b: Int // note: 'self.b' not initialized
  init?(x: Int, y: Int) {
    self.a = x
    if y == 42 {
      self.b = y
      return // error: return from initializer without initializing all stored properties <-- WRONG!
    }
    // many lines later
  }
}

I probably have to figure out which is the "bad path" by using the `getLivenessAtInst` function and check if there are any unintiialized members.
I'll try to get a PR ready by tomorrow evening.

Disclaimer: I am a complete beginner!

@swift-ci
Copy link
Collaborator

Comment by Dominique d'Argent (JIRA)

Ok, figured it out. PR submitted: #2540

@dduan
Copy link
Collaborator Author

dduan commented May 20, 2016

Good job nubbel (JIRA User) 🙂. Please resolve the issue now that your patch is merged!

@swift-ci
Copy link
Collaborator

Comment by Dominique d'Argent (JIRA)

Thanks!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

2 participants