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-6533] Arbitrary rules for resolve of overloaded Any parameters #49083

Open
swift-ci opened this issue Dec 5, 2017 · 5 comments
Open

[SR-6533] Arbitrary rules for resolve of overloaded Any parameters #49083

swift-ci opened this issue Dec 5, 2017 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 5, 2017

Previous ID SR-6533
Radar None
Original Reporter nschum (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: ee2cc6b598c0d801a4618a9c35f20064

Issue Description:

The rules for overloading functions with `Any` parameters are strangely inconsistent:

func foo(_: Any) {print(1)}
func foo(_: Any?) {print(2)}

let x: Int? = 42
foo(x) // 1
foo(nil) // 2
func foo(_: Any) {print(1)}
func foo(_: [Any]) {print(2)}

foo([42]) // 1
foo([]) // 2

I would probably expect 2 for all these calls.

@belkadan
Copy link
Contributor

belkadan commented Dec 5, 2017

@xedin, comments?

@xedin
Copy link
Member

xedin commented Dec 5, 2017

nschum (JIRA User) I agree that it does look inconsistent but it actually makes sense as soon as you know what the rules are here (it all comes down to number of conversions which need to happen): It takes less conversions to convert Int? to Any vs. Any? because that would require converting Int to Any and converting Optional<Int> to Optional<Any>, regarding nil to Any - it can't be converted, but nil to Any? can be.

Same goes for collections - [42] is type-checked as [Int] because 42 is integral literal with default type of Int ,which means that it takes less conversions to get to Any vs. [Any], where both Int as to be converted to Any and array has to be upcast from [Int] to [Any].

@belkadan
Copy link
Contributor

belkadan commented Dec 5, 2017

I'm not sure I agree with that ranking, but it is what we have implemented.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 8, 2017

Comment by Nikolaj Schumacher (JIRA)

@xedin Those rules don't seem to quite match all the effects I've noticed.

If Any is preferable to Any? due to fewer conversions, why is this call ambiguous:

func foo(_: [Any]) {print(1)}
func foo(_: [Any?]) {print(2)}

let x: Int? = 42
foo([x]) // ambiguous

Also, in the following case Any?? is preferred. According to your description, it should need at least as many conversions as `Any?`.

func foo(_: Any?) {print(1)}
func foo(_: Any??) {print(2)}

let x: Int?? = 42
foo(x) // 2

(Not that `Any??` has many real world applications 😉)

@xedin
Copy link
Member

xedin commented Dec 8, 2017

If Any is preferable to Any? due to fewer conversions, why is this call ambiguous:

That example is no longer ambiguous with the master compiler, I think it was a bug I fixed recently related to array subtyping. It's actually going to prefer `[Any?]` in this case.

Also, in the following case Any?? is preferred. According to your description, it should need at least as many conversions as `Any?`.

Since Any?? and Int?? here have the same optionality level there is only one conversion which needs to happen this case -> Int to Any, but in case of Any? - it has to get promoted to one more level of optionality to become Any?? and then Int?? converts to Any??

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants