swiftlang / swift

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

[SR-4138] Static properties are allowed to use without qualifier in initializer of instance property #46721

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-4138
Radar None
Original Reporter Max Medvedev (JIRA User)
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: c9706e2038e23de6e1ecffd1c78b09da

relates to:

Issue Description:

Swift 3 forbids to use static members without qualifier in instance context. But it's still allowed to use static properties in initializers of instance properties (though it's forbidden to use static methods there)

An example:

class A {
  static var staticProp = 0
  static func staticFunc()->Int { return 0 }

  var instanceProp = staticProp /*allowed*/ + staticFunc() /*forbidden*/
  var instanceComputedProp: Int { staticProp /*forbidden*/ + staticFunc() /*forbidden*/ }
  func instanceFunc() {
    var _ = staticProp/*forbidden*/ + staticFunc()/*forbidden*/
  } 
}
belkadan commented 7 years ago

@jckarter, we have dups for this, right?

jckarter commented 7 years ago

I don't recall this exact problem, but it's related to the problems with referring to `self` in `lazy` property initializers. The initializer expressions are evaluated as if they're in static rather than instance context, with some not-terribly-principled hacks to try to use instance context in certain situations.