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-8725] AnyObject Constraints Do Not Participate in Overload Resolution #51236

Open
swift-ci opened this issue Sep 10, 2018 · 2 comments
Open
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

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

md5: fd60ebca177c5310df43da5a61db8ade

Issue Description:

AnyObject constraints do not participate in overload resolution, as such an overloaded generic function whose type parameters differ only by an AnyObject constraint, where neither function is a subtype of the other, will cause an "ambiguous use" error when attempting to use the more constrained function.

MWE:

public func with<T>(_ initial: T, update: (inout T) throws -> Void) rethrows -> T {
    var copy = initial
    try update(&copy)
    return copy
}

public func with<T: AnyObject>(_ object: T, update: (T) throws -> Void) rethrows -> T {
    try update(object)
    return object
}

let x = with(1) {
    $0 += 2
    $0 *= 5
    $0 -= 1
}

final class Thing {
    var name: String = ""
    var position: (Int, Int) = (0, 0)
}

let y = with(Thing()) { // Ambiguous use of `with(_:update:)`
    $0.name = "Goblin Bandit"
    $0.position = (13, 2)
}
@belkadan
Copy link
Contributor

cc @rudkx, @DougGregor

@swift-ci
Copy link
Collaborator Author

Comment by Michael D. Morris (JIRA)

Okay, I don't know if this is new, but even though the test case above still has an ambiguity error, this works (Swift 5.5 Linux):

func foo<T>(_ value: T) {
    print("foo<T>(\(value))")
}

func foo<T: AnyObject>(_ object: T) {
    print("foo<T: AnyObject>(\(object))")
}

final class Class {

}


do {
    foo(42)
    foo(Class())
}

So, maybe this bug only applies when type checking closure parameters?

@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

2 participants