Details
-
Type:
Bug
-
Status: Open
-
Priority:
Medium
-
Resolution: Unresolved
-
Component/s: Compiler
-
Labels:None
-
Environment:
macOS 10.12.4, Xcode 8.3.1
-
Radar URL:
Description
Given a function f that takes Any in one of its parameters, passing self in a call to f is allowed in a closure that initializes a let property.
The compiler should raise an issue for using self before all stored properties are initialized and the issue should disappear when the property is transformed to a lazy var. But the current behavior is that self is casted as func `self`() -> Self and the compiler does not complain.
The following code reproduces the issue:
import UIKit class A: NSObject { private let control: UIControl = { print("I'm not lazy") let control = UIControl() control.addTarget(self, action: #selector(valueChanged), for: .valueChanged) print(self) // prints "(Function)" doSomething(with: self) return control }() @objc private func valueChanged() { } } func doSomething(with a: Any) { print("called 'Do Something'") } A()
Attachments
Issue Links
- is duplicated by
-
SR-4865 target self should be illegal in property constructor
-
- Resolved
-
-
SR-6895 Timer property with initializer and `self` target causes crash at runtime
-
- Resolved
-
-
SR-7198 Using self in computed let
-
- Resolved
-
-
SR-7368 Bugs of self-referencing Default Property Values in subclasses
-
- Resolved
-
-
SR-7981 `self` available without backticks in let initialization closure of NSObject
-
- Resolved
-
-
SR-8440 Using self inside a let initializer
-
- Resolved
-
- relates to
-
SR-4138 Static properties are allowed to use without qualifier in initializer of instance property
-
- Open
-