Skip to content

[SR-7368] Bugs of self-referencing Default Property Values in subclasses #49916

@an0

Description

@an0
mannequin
Previous ID SR-7368
Radar None
Original Reporter @an0
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 9.3

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

md5: 0da77bdeedb7ef554ed87067dd965c4e

duplicates:

Issue Description:

If I understand it correctly, self-reference should not be allowed in Default Property Values. Actually such properties should not support initialization-directly-to-final-value at all and must be var instead of let because:

  • Stored properties must be initialized before super.init,

  • But self cannot be used before super.init.

However currently this code compiles:

class ViewController: UIViewController {
    let tapGR = UITapGestureRecognizer(target: self, action: #selector(handleTap))

    override func viewDidLoad() {
        super.viewDidLoad()
        print(tapGR) // <UITapGestureRecognizer: 0x6000001eff00; state = Possible; view = <UIView 0x7fba644051b0>; target= <(action=handleTapWithSender:, target=<(null) 0x0>)>>
        view.addGestureRecognizer(tapGR)
    }

    @objc private func handleTap(sender: UITapGestureRecognizer) {
        print(sender)
    }
}

but it doesn't really work: tapGR is not really connected to self, as we can see tapGR.target is null and handleTap will never be called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsidentifiersFeature: Identifiersmissing warningBug: Missing warningselfFeature → expressions: The 'self' expressionswift 5.6type checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions