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-8951] Adding an init with matching type of another init should error with ambiguous reference #51456

Open
swift-ci opened this issue Oct 9, 2018 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.0

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 9, 2018

Previous ID SR-8951
Radar None
Original Reporter benasher44 (JIRA User)
Type Bug
Environment

macOS 10.13.6
Swift 4.2
Xcode 10.0 (10A255)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.0Regression
Assignee None
Priority Medium

md5: d8d439d344924ff347951eb1e0ab863e

Issue Description:

I went to add an extension to RangeReplaceableCollection today, and some tests started failing because some String s in our codebase started behaving funny. It turns out that a flatMap picked a different init without warning, which changed behavior and failed our tests.

Consider the following code:

import Foundation

//extension RangeReplaceableCollection {
//    public init(minimumCapacity: Int) {
//        self.init()
//        self.reserveCapacity(minimumCapacity)
//    }
//}

let num: Int? = 100
let str = num.flatMap(String.init) ?? ""

print("|\(str)|")

As is, this code prints |100|, but if you un-comment the extension, it prints ||. I would expect uncommenting the extension to cause a compiler error because that would make String.init ambiguous.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 9, 2018

Comment by Ben A (JIRA)

I worked around this by constraining the extension:

extension RangeReplaceableCollection where Self: ExpressibleByArrayLiteral

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 9, 2018

Comment by Ben A (JIRA)

In trying figure out if/when this regressed, I changed the example a bit (String wasn't a RangeReplaceableCollection until Swift 4):

extension String {
    public init(minimumCapacity: Int) {
        self.init()
    }
}

let num: Int? = 100
let str = num.flatMap(String.init) ?? ""

print("|\(str)|")

This tweaked sample has the same issue in Swift 4.0 (prints differently depending on the presence of this extension), but it errors as expected in Swift 3.1:

error: ambiguous use of 'init'

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Oct 12, 2018

@swift-ci
Copy link
Collaborator Author

Comment by Ben A (JIRA)

Apologies for the overly-specific title. If someone has a better understanding of the impact here and how it likely spans beyond inits, feel free to update it!

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

No branches or pull requests

2 participants