Skip to content

[SR-7981] self available without backticks in let initialization closure of NSObject #50515

@swift-ci

Description

@swift-ci
Previous ID SR-7981
Radar None
Original Reporter benpious (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 9.3.0.

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

md5: 8bc95e194233eeaad7dd08b80e745ab5

duplicates:

Issue Description:

This code compiles. In Xcode, it even colors self as though it's a keyword.

import UIKit

class C: NSObject {
    let button: UIButton = {
       let button = UIButton()
        button.addTarget(self,
                         action: #selector(f),
                         for: .touchUpInside)
        return button
    }()
    
    @objc
    func f() {   
    }
}

But self is actually NSObject.self, which is a function.

Note that in a non-NSObject subclass, this code does not compile, with the error "error: use of unresolved identifier 'self'". So I'm guessing that the compiler currently has a hack of some sort to prevent self from referring to NSObject.self as in the class object in this context, since that would be confusing. And that that's why the compiler allows NSObject.self to not be backticked.

Note that

class C {
    func self() {
    }
}

also doesn't compile, nor does refering to a func `self`() function without backticks, so allowing this is clearly wrong.

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