swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.28k stars 10.33k forks source link

[SR-10092] Custom initializers always call member initializers #52494

Open Azoy opened 5 years ago

Azoy commented 5 years ago
Previous ID SR-10092
Radar None
Original Reporter @Azoy
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | @Azoy | |Priority | Medium | md5: db7359379f23291a48589aff8b172323

Issue Description:

During SE-0242 (https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/1), there was discussion about how the compiler currently treats initializers for structs and classes.

func zero() -> Int {
  print("hello")
  return 0
}

struct X {
  var y = zero()

  init(y: Int) {
    self.y = y
  }
}

let x = X(y: 10) // hello

This behavior does not occur with memberwise initializers because they are lowered differently, but user defined initializers all have this same behavior.

Accepting rationale stated that the core team felt this behavior was a bug
(https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/98).

This is a somewhat complicated issue and I plan to write a much longer topic on the forums discussing this and getting some feedback there. I'll post the link here when I get done writing it.

Azoy commented 5 years ago

https://forums.swift.org/t/user-defined-initializers-always-call-member-initializers/21533