New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-4559] Method called 'self' can be confused with regular 'self' #47136
Comments
It looks like there's no actual loss of safety here, because
|
Comment by Andrej (JIRA) Consider the following example: class Foo {
private let dimmingView: UIView = {
let view = UIView();
view.addGestureRecognizer(
UITapGestureRecognizer(
target: self,
action: #selector(handleBackdropTap(gestureRecognizer:))
)
)
return view
}()
// ...
} When you run this code you'll find that the view does have gesture recognizer added but its target is `nil`. This is highly misleading. I don't think Swift < 4 allowed this kind of behavior. |
There's more discussion at the closed-as-duplicate https://bugs.swift.org/browse/SR-4865 |
Implementation notes from Slava and Pavel: Approach 1: In contexts where Approach 2: Implemented as a fix in constraint system, similar to |
Comment by Jack March (JIRA) Thanks a bunch theindigamer (JIRA User) for getting those notes! Went with approach 1 and made fix generic as isn't just for NSObject, it can also occur for any type that has/inherits a function called 'self'. fix here #37992 |
Fixed by #37992 Please use next available snapshot of main branch to verify and close. |
I believe I found a case where this warning is invalid, I filed https://bugs.swift.org/browse/SR-15691 |
Note that there is a swiftlint rule for many cases of this that you can enable now before the warning lands in swift 5.6 https://realm.github.io/SwiftLint/self_in_property_initialization.html |
Environment
macOS 10.12.4, Xcode 8.3.1
Additional Detail from JIRA
md5: 557db84df0937d03fb4a61df8d1c72cf
is duplicated by:
self
target causes crash at runtimeself
available without backticks in let initialization closure of NSObjectrelates to:
Issue Description:
This compiles because instance method `self` is accessible as a static member: `self`(_ self: TypeWithSelfMethod) -> () -> Void
This is particularly problematic as NSObject has a method with this name, so code to add target is compiling and doesn't refer to the self the user likely expects
The text was updated successfully, but these errors were encountered: