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-1876] Overloaded generic methods missing default parameters don't select most specific function #44485

Open
swift-ci opened this issue Jun 22, 2016 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 3.0 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1876
Radar rdar://problem/27033947
Original Reporter sswezeystk (JIRA User)
Type Bug
Environment

Xcode 8 Beta 1

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, 3.0Regression, TypeChecker
Assignee @slavapestov
Priority Medium

md5: 36965762b7cc9100f831c2eb26c79e70

Issue Description:

When there are several functions with similar signatures, but differing generic constraints, omitting default parameters in the function call results in the wrong function being called. This works correctly with Swift 2.2 in Xcode 7.3

import Foundation

protocol A {
    init(int: Int)
}

extension Int: A {
    init(int: Int) {
        self.init(int)
    }
}
extension String: A {
    init(int: Int) {
        self.init(int.description)
    }
}

protocol B {
    init(string: String)
}

extension String: B {
    init(string: String) {
        self.init(string)
    }
}

func d<T where T: A, T: B>(key key: String, new: Bool = true) -> Array<T> {
    return [T(int:1), T(string:"b")]
}

func d<T where T: A>(key key: String, new: Bool = true) -> Array<T> {
    return [T(int: 3), T(int: 4)]
}

func d<T>(key key: String) -> Array<T> {
    return Array<T>()
}

let a: Array<Int> = d(key: "adf")
let aa: Array<Int> = d(key: "adf", new: false)
let b: Array<String> = d(key: "adfa")
let bb: Array<String> = d(key: "adfa", new: false)
let c: Array<Double> = d(key: "dfa")

print(a, aa, a == aa) // should be true
print(b, bb, b == bb) // should be true
print(c)
@swift-ci
Copy link
Collaborator Author

Comment by Andrii Chernenko (JIRA)

Somewhat simpler example that reproduces the same issue in Xcode 8.1:

protocol P1 {}
protocol P2 {}
class A: P1, P2 {}

func foo<T>(_: T.Type) where T: P1 {
  print("overload for P1")
}

func foo<T>(_: T.Type, a: Optional<Any> = nil) where T: P1 & P2 {
  print("overload for P1 and P2")
}

foo(A.self)

this prints

overload for P1 

instead of the expected

overload for P1 and P2

@swift-ci
Copy link
Collaborator Author

Comment by Chibundu Anwuna (JIRA)

@slavapestov what's the status of this bug as problem still exists in Xcode 11 with Swift 5.1.3 and Xcode 12 with Swift 5.3. However I tested it with Swift 4.2 and it works.

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

No branches or pull requests

2 participants