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-1016] #selector broken for no-argument initializers and functions #43628

Open
swift-ci opened this issue Mar 22, 2016 · 7 comments
Open

[SR-1016] #selector broken for no-argument initializers and functions #43628

swift-ci opened this issue Mar 22, 2016 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1016
Radar rdar://problem/25315833
Original Reporter marc (JIRA User)
Type Bug
Environment

Xcode 7.3 / Swift 2.2

Additional Detail from JIRA
Votes 3
Component/s Compiler
Labels Bug
Assignee @DougGregor
Priority Medium

md5: 5b9028031618d217e4b82fcbe4d30fd1

is duplicated by:

  • SR-1033 #selector disambiguation between foo() and foo(x:)
  • SR-11179 #selector format is inconsistent

Issue Description:

#selector(init()) does not work:

import UIKit
#selector(UIViewController.init())

// error: argument of '#selector' does not refer to an initializer or method

And using #selector(init) as a workaround does not work if there is more than one initializer:

import UIKit
#selector(UIViewController.init)

// error: ambiguous use of 'init(nibName:bundle:)'
// note: found this candidate
//    public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
// note: found this candidate
//    public init?(coder aDecoder: NSCoder)
// note: found this candidate
//    public convenience init()

The bug affects regular non-argument functions as well.

@belkadan
Copy link
Contributor

You can work around this by using #selector(UIViewController.init as () -> UIViewController).

@belkadan
Copy link
Contributor

cc @DougGregor

@swift-ci
Copy link
Collaborator Author

Comment by Marc Knaup (JIRA)

Still not working in swift-DEVELOPMENT-SNAPSHOT-2017-09-26-a-osx but by now with a different error message:

import UIKit
#selector(UIViewController.init())

// error: argument of '#selector' does not refer to an '@objc' method, property, or initializer

Since init() is inherited from NSObject it should be a valid @objc initializer.

@DougGregor
Copy link
Member

Given that this would be a change in the language semantics, it should go through swift-evolution.

@swift-ci
Copy link
Collaborator Author

Comment by Marc Knaup (JIRA)

What would be a change in semantics here?

#selector(NSObject.init()) and #selector(NSObject()) also don't work with the same error, but NSObject's init() is an ObjC initializer, isn't it?

@DougGregor
Copy link
Member

Well, it's making some ill-formed code well-formed, and means that the expression

UIViewController.init()

has a different meaning within #selector than it does outside of #selector.

@swift-ci
Copy link
Collaborator Author

Comment by Marc Knaup (JIRA)

I understand. But why is that a problem?

#selector(UIViewController.init()) and alike isn't valid code at the moment anyway and results in a compile error which is not helpful.

The way I understand #selector is that it gives me a Selector to whatever method/initializer/property I put in there. Why should that no-arg example above be treated differently than #selector(UIViewController.init(_:))? Also Swift and Xcode both refer to it as init() for example in #function and in auto-completion. Why would a user expect .init() to be executed but not {{.init(: )}}_?

This is inconsistent and confusing.

To sum it up - the actual issue is still the same:
It's not possible to refer to a zero-argument initializers / methods if there is another initializer / method with the same name. Unless you add a cast to fix the ambiguity.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

3 participants