mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
When comparing two functions for overload resolution, break apart the parameter lists to compare individual parameters rather than comparing the tuples. This allows us to prefer functions with fewer arguments to ones with more, defaulted or variadic arguments. That preference was already encoded in the constraint optimizer, which led to some strange behavior where the preference was expressed for function calls but not for calls to initializers. Fixes rdar://problem/24128153. The standard library change tweaks the anachronistic, unavailable "print" variants somewhat. The only behavior change here is a slight regression for cases like: print(a: 1, b: 2) where we used to produce a diagnostic: Please wrap your tuple argument in parentheses: 'print((...))' but we now get: argument labels '(a:, b:)' do not match any available overloads However, this regression will happen at some point *anyway*, if SE-0029 (or anything else that removes the implicit tuple splat operation) goes through.
82 lines
2.2 KiB
Swift
82 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 {{invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> 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{{extraneous argument label 'text:' in call}}{{7-12=}}
|
|
|
|
|
|
// <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}}
|
|
|
|
|
|
// SR-590 Passing two parameters to a function that takes one argument of type Any crashes the compiler
|
|
func sr590(x: Any) {}
|
|
sr590(3,4) // expected-error {{extra argument in call}}
|
|
|