Skip to content

[SR-4865] target self should be illegal in property constructor #47442

@mattneub

Description

@mattneub
Previous ID SR-4865
Radar None
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 8.3.2

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

md5: d3c2dbfe3cffd99ed51448d61dd795be

duplicates:

Issue Description:

This is legal in an instance property declaration:

    let listen : UIBarButtonItem = {
        let b = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(doListen))
        return b
    }()

It shouldn't be. There is no self at this point. The code compiles but it isn't going to work, so it shouldn't compile at all.

I see a lot of people on Stack Overflow get caught by this sort of thing, but this is the first time I've made the same mistake myself. The usual context is in configuring a Timer.

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