flint icon indicating copy to clipboard operation
flint copied to clipboard

Crash when using a mutating func in init

Open Aurel300 opened this issue 7 years ago • 1 comments

Encountered while working on asset traits in our fork.

E.g.

struct S {
  var rawValue: Int

  init(unsafeRawValue: Int) {
    setRawValue(unsafeRawValue)
  }

  mutating func setRawValue(value: Int) -> Int {
    rawValue = value
    return rawValue
  }
}

This should output a meaningful error message (assuming IR really cannot be output in this case?) instead of crashing the compiler completely.

Aurel300 avatar Oct 17 '18 09:10 Aurel300

I did a little investigation. If we want to stay consistent with Swift, then we need to enforce that all properties are initialised:

screenshot 2018-10-23 at 19 02 07

It does seem like mutating functions can be called from the initialiser and that we can set properties like one would expect.

screenshot 2018-10-23 at 19 02 25

Another thing (and I think flint has this) is to generate an initialiser with all uninitialised properties if one is not present

screenshot 2018-10-23 at 19 04 36

Ultimately I think we should treat init as if it was mutable. It's a function that's always run after memory is allocated and before control is handed back to the enclosing scope which may bind it to a let/immutable identifier. Prior to this handover we should be free to mutate it.

nvgrw avatar Oct 23 '18 18:10 nvgrw