Swift 2 error for ambiguity of return type at call site is confusing and unhelpful

Originator:chaos42
Number:rdar://21675236 Date Originated:04-Jul-2015 02:29 PM
Status:Open Resolved:
Product:Developer Tools Product Version:7.0 beta (7A121l)
Classification:UI/Usability Reproducible:Always
 
Summary:
If you have a generic function that returns the generic type, in cases where the return type cannot be inferred from context, the Swift compiler correctly produces an error. Unfortunately, that error is confusing and unhelpful, emphasizing the arguments, and the info on the error is similarly unhelpful, telling you what arguments it expected rather than that the return type cannot be inferred (by, say, storing the result of the function into a typed var or let).

Though the steps to reproduce specifies a playground, the same error occurs in a normal project's Swift files as well.

Steps to Reproduce:
1. Install Xcode 7 and open a new Swift playground. 
2. Replace the contents with the following code: 

import Cocoa

func readFromData<T: UnsignedIntegerType>(data: NSData, inout cursor:Int) -> T {
    var temp : T = 0
    data.getBytes(&temp, range: NSMakeRange(cursor, sizeof(T)))
    cursor += sizeof(T)
    return temp
}

let data = "FooBar".dataUsingEncoding(NSUTF8StringEncoding)
var cursor : Int = 0
var temp4 = readFromData(data, cursor: &cursor)

3. Open the console for the playground.
4. Read the error.

Expected Results:
Error should read something like (most of the path removed for brevity):

"Playground execution failed: playground66.swift:14:13: error: cannot invoke 'readFromData' with ambiguous return type at call site
var temp4 = readFromData(data, cursor: &cursor)
            ^
playground66.swift:14:25: note: expected an unambiguous destination for return value. Assignment requires explicitly specified storage. Explicitly specify the type after the var name, e.g. var temp4 : Int = ...
var temp4 = readFromData(data, cursor: &cursor)
         ^
"

Actual Results:
Error reads (most of the path removed for brevity:)
"Playground execution failed: playground66.swift:14:13: error: cannot invoke 'readFromData' with an argument list of type '(NSData?, cursor: inout Int)'
var temp4 = readFromData(data, cursor: &cursor)
            ^
playground66.swift:14:25: note: expected an argument list of type '(NSData, cursor: inout Int)'
var temp4 = readFromData(data, cursor: &cursor)
                        ^
"

This is wrong and confusing because it makes it appear that there's an invisible problem with the argument list rather than an obvious problem with the return type. 

Version:
Xcode Version 7.0 beta (7A121l)
OS X 10.10.4 (14E46)

Notes:
No workaround exists. Unsure about regressions. 

Configuration:


Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!