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-2942] Errant ambiguous overloaded method when return type optionality differs #45536

Open
swift-ci opened this issue Oct 13, 2016 · 0 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-2942
Radar None
Original Reporter rmann (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 8, macOS 10.11.6

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

md5: e004ab1ad03ba6dc82c0ac692a3aa1f9

Issue Description:

The attached Xcode playground demonstrates this behavior. If two versions of a method exist that differ only in the optionality of the return type, the compiler should be able to choose the optional one when called within an if-let context. In Xcode 8 (8A218a), it's ambiguous without more context.

import Foundation

extension
String
{
    public
    func
    deleting(prefix inPrefix: String)
        -> String
    {
        //  If it's not a substring, return self…
        
        guard
            let r = self.range(of: inPrefix)
        else
        {
            return self
        }
        
        //  If it's not the prefix, return self…
        
        if r.lowerBound != self.startIndex
        {
            return self
        }
        
        return self.substring(from: r.upperBound)
    }

    public
    func
    deleting(prefix inPrefix: String)
        -> String?
    {
        //  If it's not a substring, return self…
        
        guard
            let r = self.range(of: inPrefix)
        else
        {
            return nil
        }
        
        //  If it's not the prefix, return self…
        
        if r.lowerBound != self.startIndex
        {
            return nil
        }
        
        return self.substring(from: r.upperBound)
    }
}

let s = "Hello there"

//  Case 1 (works)

let a: String = s.deleting(prefix: "Hello ")
print("a: \(a)")

//  Case 2 (works)

if let b: String = s.deleting(prefix: "Bar")
{
    print("b: \(b) (Oops)")
}
else
{
    print("b: \(s) (Correct)")
}

//  Case 3 (ambiguous, but shouldn’t be)

if let c = s.deleting(prefix: "Bar")
{
    print("b: \(b) (Oops)")
}
else
{
    print("b: \(s) (Correct)")
}
@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

1 participant