Skip to content

[SR-8951] Adding an init with matching type of another init should error with ambiguous reference #51456

Open
@swift-ci

Description

@swift-ci
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfregressionswift 4.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions