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-13847] Wrong generic used in extensions #56245

Open
swift-ci opened this issue Nov 12, 2020 · 2 comments
Open

[SR-13847] Wrong generic used in extensions #56245

swift-ci opened this issue Nov 12, 2020 · 2 comments
Labels
bug A deviation from expected or documented behavior

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 12, 2020

Previous ID SR-13847
Radar rdar://problem/71330233
Original Reporter David2X (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 1db7e674f8f1fcaff55a47d1eb4e85a0

Issue Description:

For some reason when calling initializer in extension, compiler tries to match unrelated generics.

Example with Array:
Works in function:

func enumeratedWithIndex(array: [Int]) {
    let zipped = zip(array.indices, array)
    let ret = Array(zipped)
}

Does not work in extension

extension Array where Element == Int {
    func enumeratedWithIndex() /*-> [(Int, Element)]*/ {
        let array = self
        let zipped = zip(array.indices, array)
        let ret = Array(zipped)
    }
}

Error on line

let ret = Array(zipped)
Initializer 'init(_:)' requires the types 'Int' and 'Zip2Sequence<Range<Array<Int>.Index>, [Int]>.Element' (aka '(Int, Int)') be equivalent

💡 The only workaround is to manually specify generics:

extension Array where Element == Int {
    func enumeratedWithIndex() /*-> [(Int, Element)]*/ {
        let array = self
        let zipped = zip(array.indices, array)
        let ret = Array<(Int,Int)>(zipped)
    }
}
@typesanitizer
Copy link

typesanitizer commented Nov 12, 2020

Doesn't seem to be specific to array. Reduced:

struct S<T> { let t: T }
extension S where T == Int {
  func f() {
    let s1 = S(t:1) // OK
    let s2 = S(t:"X") // error: cannot convert value of type 'String' to expected argument type 'Int'
  }
} 

@typesanitizer
Copy link

typesanitizer commented Nov 12, 2020

@swift-ci create

@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
Projects
None yet
Development

No branches or pull requests

2 participants