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-7524] Non-accurate ambiguity error #50066

Open
swift-ci opened this issue Apr 24, 2018 · 3 comments
Open

[SR-7524] Non-accurate ambiguity error #50066

swift-ci opened this issue Apr 24, 2018 · 3 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-7524
Radar None
Original Reporter gmosx (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @xedin
Priority Medium

md5: 1ff3c4a77acedeb5a72b9815c51ccc1d

Issue Description:

Reproduction scenario:

extension Sequence where Element: Numeric {
    public func sum() -> Element {
        return reduce(0, +)
    }
}

extension Sequence {
    public func sum<T>() -> T where T: Numeric, Element == T? {
        var sum: T = 0

        for value in self {
            if let value = value {
                sum += value
            }
        }

        return sum
    }
}

extension DataSeries: BidirectionalCollection {
    public typealias Value = Any?
    public typealias Element = Value
    ...
}

let series: DataSeries

let s = series.sum()

yields the following not very helpful error:

*****:70:24: error: ambiguous reference to member 'sum()'
        XCTAssertEqual(series.sum(), 948.872)
                       ^~~~~~
Statistics.Sequence:2:17: note: found this candidate
    public func sum() -> Self.Element
                ^
Statistics.Sequence:2:17: note: found this candidate
    public func sum<T>() -> T where T : Numeric, Self.Element == T?

I suspect the real error is that series is a Sequence of Any? instead of Numeric?

For more context, please see this thread on forums.swift.org:

https://forums.swift.org/t/generic-method-over-a-sequence-of-optionals/12106

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented May 1, 2018

cc @jckarter

@jckarter
Copy link
Member

jckarter commented May 1, 2018

@DougGregor, @rudkx, or @xedin would be a better judge of the intended behavior here. It seems like the extension `sum` ought to be more specific in this case, but is the type checker set up to score constrained generics in this way (or expected to be)?

@xedin
Copy link
Member

xedin commented May 3, 2018

I think my changes in #15946 are going to fix this case because first extension is supposed to be more specific than other one one with generic signature in `sum` method.

@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