Skip to content

[SR-4559] Method called 'self' can be confused with regular 'self' #47136

@swift-ci

Description

@swift-ci
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:

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

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