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-4304] Swift compiler unable to resolve correct overload of + operator for arrays in generic function #46887

Open
swift-ci opened this issue Mar 21, 2017 · 3 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

Previous ID SR-4304
Radar None
Original Reporter thm (JIRA User)
Type Bug
Environment

Xcode 8.2.1 (8C1002) with the included

$ swift --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 929e460ad9d2931ec9a446f7be03c731

is blocked by:

  • SR-4309 Type inferring fails in expression with ?? and +

Issue Description:

I've had several problems with the plus operator so I decided to investigate and ended up with the following minimal example. When I compile

func f<T>(t: T) -> [T]
{
    return [t] + [t]
}

everything is fine. The compiler chooses this overload of the plus operator:

public func +<RRC1 : RangeReplaceableCollection, RRC2 : RangeReplaceableCollection
    where RRC1.Iterator.Element == RRC2.Iterator.Element>
    (lhs: RRC1, rhs: RRC2) -> RRC1

However, when I add another `+` to the game, I get this:

func f<T>(t: T) -> [T]
{
    return [t] + [t] + [t]
}

main.swift:30:9: error: cannot convert value of type '[T]' to expected argument type '[_]' (aka 'Array<_>')
    return [t] + [t] + [t]
           ^~~
               as! [_]

I found several ways to make this work, like for instance

@belkadan
Copy link
Contributor

Still broken in master.

@rudkx
Copy link
Member

rudkx commented Mar 22, 2017

@swift-ci

@rudkx
Copy link
Member

rudkx commented Mar 22, 2017

There is a problem in the constraint optimizer that leads to our trying to force the type of the first '+' to T rather than [T].

@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