Skip to content
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-7198] Using self in computed let #49746

Closed
swift-ci opened this issue Mar 14, 2018 · 0 comments
Closed

[SR-7198] Using self in computed let #49746

swift-ci opened this issue Mar 14, 2018 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation duplicate Resolution: Duplicates another issue expressions Feature: expressions identifiers Feature: Identifiers missing warning Bug: Missing warning self Feature → expressions: The 'self' expression swift 5.6 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 14, 2018

Previous ID SR-7198
Radar None
Original Reporter nrbrook (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode Version 9.2 (9C40b)

Swift 4

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 04555a2ce1020475bcf49a13514222fd

duplicates:

Issue Description:

See the attached project, relevant code here:

class Test: UIButton {
    var test = 0
    
    override func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents) {
        print("\(String(describing: target))")
        super.addTarget(target, action: action, for: controlEvents)
    }
}

class ViewController: UIViewController {
    let test: Test = {
        let b = Test(type: .system);
        b.addTarget(self, action: #selector(ViewController.changed), for: UIControlEvents.touchUpInside)
        return b
    }()
    
    @objc func changed() {
        print("Hello pressed!")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let s = self.test
        s.setTitle("Hello", for: .normal)
        s.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        self.view.addSubview(s)
    }
}

If you run this and press the button it will print:

Optional((Function))
Hello pressed!

If you put a breakpoint in Test.addTarget and when hit run po target! in lldb, continue, then press the button, you get a crash: unrecognized selector sent to instance

The behaviour of this code is, I believe, undefined. Should self be the closure, in which case the crash should occur every time, or should this generate a compiler error as self is not valid in this context?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added duplicate Resolution: Duplicates another issue compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation swift 5.6 expressions Feature: expressions missing warning Bug: Missing warning identifiers Feature: Identifiers self Feature → expressions: The 'self' expression type checker Area → compiler: Semantic analysis labels May 2, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation duplicate Resolution: Duplicates another issue expressions Feature: expressions identifiers Feature: Identifiers missing warning Bug: Missing warning self Feature → expressions: The 'self' expression swift 5.6 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants