Files
swift-mirror/test/Constraints/function.swift
Chris Lattner c652c62f88 When sorting through a list of candidates in a call overload set, introduce the
notion of a "near miss" for an argument type mismatch.  This allows us to prune
the candidate set down in some cases.  For example, in the testcase in
rdar://22243469 we are able to go from:

t.swift:6:3: error: cannot invoke 'process' with an argument list of type '(() throws -> ())'
  process {
  ^
t.swift:6:3: note: overloads for 'process' exist with these partially matching parameter lists: (UInt, fn: () -> Void), (UInt)

down to:
t.swift:6:3: note: expected an argument list of type '(UInt, () -> Void)'

This paves the way for producing a better error in cases like this, but there are
other bits of weirdness that need to be untangled first.
2015-11-15 20:50:05 -08:00

79 lines
2.2 KiB
Swift

// RUN: %target-parse-verify-swift
func f0(x: Float) -> Float {}
func f1(x: Float) -> Float {}
func f2(@autoclosure x: () -> Float) {}
var f : Float
f0(f0(f))
f0(1)
f1(f1(f))
f2(f)
f2(1.0)
func call_lvalue(@autoclosure rhs: ()->Bool) -> Bool {
return rhs()
}
// Function returns
func weirdCast<T, U>(x: T) -> U {}
func ff() -> (Int) -> (Float) { return weirdCast }
// Block <-> function conversions
var funct: String -> String = { $0 }
var block: @convention(block) String -> String = funct
funct = block
block = funct
// Application of implicitly unwrapped optional functions
var optFunc: (String -> String)! = { $0 }
var s: String = optFunc("hi")
// <rdar://problem/17652759> Default arguments cause crash with tuple permutation
func testArgumentShuffle(first: Int = 7, third: Int = 9) {
}
testArgumentShuffle(third: 1, 2)
func rejectsAssertStringLiteral() {
assert("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
precondition("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
}
// <rdar://problem/22243469> QoI: Poor error message with throws, default arguments, & overloads
func process(line: UInt = __LINE__, _ fn: () -> Void) {}
func process(line: UInt = __LINE__) -> Int { return 0 }
func dangerous() throws {}
func test() {
process { // expected-error {{cannot invoke 'process' with an argument list of type '(() throws -> ())'}}
// expected-note @-1 {{expected an argument list of type '(UInt, () -> Void)'}}
try dangerous()
test()
}
}
// <rdar://problem/19962010> QoI: argument label mismatches produce not-great diagnostic
class A {
func a(text:String) {
}
func a(text:String, something:Int?=nil) {
}
}
A().a(text:"sometext") // expected-error {{argument labels '(text:)' do not match any available overloads}}
// expected-note @-1 {{overloads for 'a' exist with these partially matching parameter lists: (String), (String, something: Int?)}}
// <rdar://problem/22451001> QoI: incorrect diagnostic when argument to print has the wrong type
func r22451001() -> AnyObject {}
print(r22451001(5)) // expected-error {{argument passed to call that takes no arguments}}