Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1469

Diagnostic for init? reported at the end despite of early return

    XMLWordPrintable

    Details

      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.

        Attachments

          Activity

            People

            Assignee:
            nubbel Dominique d'Argent
            Reporter:
            duan Daniel Duan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: