-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Closed
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsFeature: expressionsidentifiersFeature: IdentifiersFeature: Identifiersmissing warningBug: Missing warningBug: Missing warningselfFeature → expressions: The 'self' expressionFeature → expressions: The 'self' expressionswift 5.6type checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysis
Description
| Previous ID | SR-4559 |
| Radar | rdar://problem/24223414 |
| Original Reporter | mouhcine (JIRA User) |
| Type | Bug |
| Status | Resolved |
| Resolution | Done |
Environment
macOS 10.12.4, Xcode 8.3.1
Additional Detail from JIRA
| Votes | 8 |
| Component/s | Compiler |
| Labels | Bug |
| Assignee | mouhcine (JIRA) |
| Priority | Medium |
md5: 557db84df0937d03fb4a61df8d1c72cf
is duplicated by:
- [SR-4865] target self should be illegal in property constructor #47442
- [SR-6895] Timer property with initializer and
selftarget causes crash at runtime #49444 - [SR-7198] Using self in computed let #49746
- [SR-7368] Bugs of self-referencing Default Property Values in subclasses #49916
- [SR-7981]
selfavailable without backticks in let initialization closure of NSObject #50515 - [SR-8440] Using self inside a let initializer #50962
relates to:
Issue Description:
This compiles because instance method `self` is accessible as a static member: `self`(_ self: TypeWithSelfMethod) -> () -> Void
func funcThatReturnsSomething(_ any: Any) -> Any {
any
}
struct TypeWithSelfMethod {
let property = self
let propertyFromClosure: () = {
print(self)
}()
let propertyFromFunc = funcThatReturnsSomething(self)
func `self`() {
}
}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
import UIKit
class A: NSObject {
private let control: UIControl = {
let control = UIControl()
control.addTarget(self, action: #selector(valueChanged), for: .valueChanged)
return control
}()
@objc private func valueChanged() {
}
}Metadata
Metadata
Assignees
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsFeature: expressionsidentifiersFeature: IdentifiersFeature: Identifiersmissing warningBug: Missing warningBug: Missing warningselfFeature → expressions: The 'self' expressionFeature → expressions: The 'self' expressionswift 5.6type checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysis