Skip to content

Commit 88c39db

Browse files
committed
[Diagnostics] NFC: Fix improved test-cases related to argument-to-parameter mismatches
1 parent afc3408 commit 88c39db

16 files changed

+87
-85
lines changed

test/ClangImporter/foreign_errors.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ func testBlockFinal() throws {
7777
#if !EMIT_SIL
7878
func testNonBlockFinal() throws {
7979
ErrorProne.runWithError(count: 0) // expected-error {{missing argument for parameter #1 in call}}
80+
// TODO(diagnostics): For situations where both label and type where incorrect, we should produce a single error
81+
// which would say something like `cannot invoke 'bar' with argument list (count: Int)`.
8082
ErrorProne.run(count: 0) // expected-error {{incorrect argument label in call (have 'count:', expected 'callback:')}}
83+
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type '(() -> Void)?'}}
8184
}
8285
#endif
8386

test/Constraints/bridging.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ func rdar19831698() {
265265
var v71 = true + 1.0 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Double'}}
266266
// expected-note@-1{{overloads for '+'}}
267267
var v72 = true + true // expected-error{{binary operator '+' cannot be applied to two 'Bool' operands}}
268-
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists:}}
269-
var v73 = true + [] // expected-error{{cannot convert value of type 'Bool' to expected argument type 'Array<Any>'}}
268+
var v73 = true + [] // expected-error{{cannot convert value of type 'Bool' to expected argument type 'Array<Bool>'}}
270269
var v75 = true + "str" // expected-error {{cannot convert value of type 'Bool' to expected argument type 'String'}}
271270
}
272271

test/Constraints/closures.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ let u = rdar33296619().element //expected-error {{use of unresolved identifier '
583583

584584
[1].forEach { _ in
585585
_ = "\(u)"
586-
_ = 1 + "hi" // expected-error {{no exact matches in call to operator function '+'}}
586+
_ = 1 + "hi" // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}}
587+
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
587588
}
588589

589590
class SR5666 {

test/Constraints/diagnostics.swift

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ func recArea(_ h: Int, w : Int) {
182182

183183
// <rdar://problem/17224804> QoI: Error In Ternary Condition is Wrong
184184
func r17224804(_ monthNumber : Int) {
185-
// expected-error @+2 {{binary operator '+' cannot be applied to operands of type 'String' and 'Int'}}
186-
// expected-note @+1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
185+
// expected-error @+1 {{cannot convert value of type 'Int' to expected argument type 'String'}}
187186
let monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
188187
}
189188

@@ -334,13 +333,15 @@ _ = f7(1)(1)
334333
f7(1.0)(2) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
335334

336335
f7(1)(1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
337-
f7(1)(b: 1.0) // expected-error{{extraneous argument label 'b:' in call}}
336+
f7(1)(b: 1.0) // expected-error{{extraneous argument label 'b:' in call}}
337+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
338338

339339
let f8 = f7(2)
340340
_ = f8(1)
341341
f8(10) // expected-warning {{result of call to function returning 'Int' is unused}}
342342
f8(1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
343343
f8(b: 1.0) // expected-error {{extraneous argument label 'b:' in call}}
344+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
344345

345346

346347
class CurriedClass {
@@ -357,9 +358,11 @@ _ = c.method2(1.0) // expected-error {{cannot convert value of type 'Double' t
357358
c.method2(1)(2)
358359
c.method2(1)(c: 2) // expected-error {{extraneous argument label 'c:' in call}}
359360
c.method2(1)(c: 2.0) // expected-error {{extraneous argument label 'c:' in call}}
361+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
360362
c.method2(1)(2.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
361363
c.method2(1.0)(2) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
362364
c.method2(1.0)(2.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
365+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
363366

364367
CurriedClass.method1(c)()
365368
_ = CurriedClass.method1(c)
@@ -371,8 +374,10 @@ _ = CurriedClass.method2(c)
371374
_ = CurriedClass.method2(c)(32)
372375
_ = CurriedClass.method2(1,2) // expected-error {{extra argument in call}}
373376
CurriedClass.method2(c)(1.0)(b: 1) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
377+
// expected-error@-1 {{extraneous argument label 'b:' in call}}
374378
CurriedClass.method2(c)(1)(1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
375379
CurriedClass.method2(c)(2)(c: 1.0) // expected-error {{extraneous argument label 'c:'}}
380+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
376381

377382
CurriedClass.method3(c)(32, b: 1)
378383
_ = CurriedClass.method3(c)
@@ -435,6 +440,7 @@ let _: (Int) -> (Int, Color) = { ($0, .Unknown("")) } // expected-error {{missin
435440
let _: Color = .Unknown("") // expected-error {{missing argument label 'description:' in call}} {{25-25=description: }}
436441
let _: Color = .Unknown // expected-error {{member 'Unknown' expects argument of type '(description: String)'}}
437442
let _: Color = .Unknown(42) // expected-error {{missing argument label 'description:' in call}}
443+
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type 'String'}}
438444
let _ : Color = .rainbow(42) // expected-error {{argument passed to call that takes no arguments}}
439445

440446
let _ : (Int, Float) = (42.0, 12) // expected-error {{cannot convert value of type 'Double' to specified type 'Int'}}
@@ -446,6 +452,7 @@ let _: Color = .overload(1.0) // expected-error {{ambiguous reference to member
446452
// expected-note @-1 {{overloads for 'overload' exist with these partially matching parameter lists: (a: Int), (b: Int)}}
447453
let _: Color = .overload(1) // expected-error {{no exact matches in call to static method 'overload'}}
448454
let _: Color = .frob(1.0, &i) // expected-error {{missing argument label 'b:' in call}}
455+
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Int'}}
449456
let _: Color = .frob(1.0, b: &i) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
450457
let _: Color = .frob(1, i) // expected-error {{missing argument label 'b:' in call}}
451458
// expected-error@-1 {{passing value of type 'Int' to an inout parameter requires explicit '&'}}
@@ -711,9 +718,9 @@ func r24251022() {
711718
var a = 1
712719
var b: UInt32 = 2
713720
_ = a + b // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'UInt32'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (UInt32, UInt32)}}
714-
a += a + // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'UInt32'}} expected-note {{overloads for '+' exist with these partially matching parameter lists:}}
715-
b
716-
a += b // expected-error {{binary operator '+=' cannot be applied to operands of type 'Int' and 'UInt32'}} expected-note {{overloads for '+=' exist with these partially matching parameter lists: (inout Int, Int), (inout UInt32, UInt32)}}
721+
a += a +
722+
b // expected-error {{cannot convert value of type 'UInt32' to expected argument type 'Int'}}
723+
a += b // expected-error {{cannot convert value of type 'UInt32' to expected argument type 'Int'}}
717724
}
718725

719726
func overloadSetResultType(_ a : Int, b : Int) -> Int {
@@ -802,7 +809,7 @@ func read2(_ p: UnsafeMutableRawPointer, maxLength: Int) {}
802809
func read<T : BinaryInteger>() -> T? {
803810
var buffer : T
804811
let n = withUnsafeMutablePointer(to: &buffer) { (p) in
805-
read2(UnsafePointer(p), maxLength: MemoryLayout<T>.size) // expected-error {{cannot convert value of type 'UnsafePointer<Pointee>' to expected argument type 'UnsafeMutableRawPointer'}}
812+
read2(UnsafePointer(p), maxLength: MemoryLayout<T>.size) // expected-error {{cannot convert value of type 'UnsafePointer<T>' to expected argument type 'UnsafeMutableRawPointer'}}
806813
}
807814
}
808815

@@ -960,12 +967,10 @@ func SR_6272_a() {
960967
case bar
961968
}
962969

963-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Int' and 'Float'}} {{35-35=Int(}} {{43-43=)}}
964-
// expected-note@+1 {{expected an argument list of type '(Int, Int)'}}
970+
// expected-error@+1 {{cannot convert value of type 'Float' to expected argument type 'Int'}} {{35-35=Int(}} {{43-43=)}}
965971
let _: Int = Foo.bar.rawValue * Float(0)
966972

967-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Int' and 'Float'}} {{18-18=Float(}} {{34-34=)}}
968-
// expected-note@+1 {{expected an argument list of type '(Float, Float)'}}
973+
// expected-error@+1 {{cannot convert value of type 'Int' to expected argument type 'Float'}} {{18-18=Float(}} {{34-34=)}}
969974
let _: Float = Foo.bar.rawValue * Float(0)
970975

971976
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Int' and 'Float'}} {{none}}
@@ -977,12 +982,10 @@ func SR_6272_b() {
977982
let lhs = Float(3)
978983
let rhs = Int(0)
979984

980-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Float' and 'Int'}} {{24-24=Float(}} {{27-27=)}}
981-
// expected-note@+1 {{expected an argument list of type '(Float, Float)'}}
985+
// expected-error@+1 {{cannot convert value of type 'Int' to expected argument type 'Float'}} {{24-24=Float(}} {{27-27=)}}
982986
let _: Float = lhs * rhs
983987

984-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Float' and 'Int'}} {{16-16=Int(}} {{19-19=)}}
985-
// expected-note@+1 {{expected an argument list of type '(Int, Int)'}}
988+
// expected-error@+1 {{cannot convert value of type 'Float' to expected argument type 'Int'}} {{16-16=Int(}} {{19-19=)}}
986989
let _: Int = lhs * rhs
987990

988991
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Float' and 'Int'}} {{none}}
@@ -991,20 +994,18 @@ func SR_6272_b() {
991994
}
992995

993996
func SR_6272_c() {
994-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Int' and 'String'}} {{none}}
995-
// expected-note@+1 {{expected an argument list of type '(Int, Int)'}}
997+
// expected-error@+1 {{cannot convert value of type 'String' to expected argument type 'Int'}} {{none}}
996998
Int(3) * "0"
997999

9981000
struct S {}
999-
// expected-error@+2 {{binary operator '*' cannot be applied to operands of type 'Int' and 'S'}} {{none}}
1000-
// expected-note@+1 {{expected an argument list of type '(Int, Int)'}}
1001+
// expected-error@+1 {{cannot convert value of type 'S' to expected argument type 'Int'}} {{none}}
10011002
Int(10) * S()
10021003
}
10031004

10041005
struct SR_6272_D: ExpressibleByIntegerLiteral {
10051006
typealias IntegerLiteralType = Int
10061007
init(integerLiteral: Int) {}
1007-
static func +(lhs: SR_6272_D, rhs: Int) -> Float { return 42.0 }
1008+
static func +(lhs: SR_6272_D, rhs: Int) -> Float { return 42.0 } // expected-note 2 {{candidate expects value of type 'Int' at position #1}}
10081009
}
10091010

10101011
func SR_6272_d() {
@@ -1014,8 +1015,7 @@ func SR_6272_d() {
10141015
// expected-note@+1 {{overloads for '+' exist with these partially matching parameter lists: (Float, Float), (SR_6272_D, Int)}}
10151016
let _: Float = SR_6272_D(integerLiteral: 42) + x
10161017

1017-
// expected-error@+2 {{binary operator '+' cannot be applied to operands of type 'SR_6272_D' and 'Double'}} {{none}}
1018-
// expected-note@+1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (SR_6272_D, Int)}}
1018+
// expected-error@+1 {{cannot convert value of type 'Double' to expected argument type 'Int'}} {{50-50=Int(}} {{54-54=)}}
10191019
let _: Float = SR_6272_D(integerLiteral: 42) + 42.0
10201020

10211021
// expected-error@+2 {{binary operator '+' cannot be applied to operands of type 'SR_6272_D' and 'Float'}} {{none}}
@@ -1183,24 +1183,23 @@ func sr5081() {
11831183
b = a[2...4] // expected-error {{cannot assign value of type 'ArraySlice<String>' to type '[String]'}}
11841184
}
11851185

1186+
/**
1187+
* TODO(diagnostics):Figure out what to do when expressions are complex and completely broken
1188+
*
11861189
func rdar17170728() {
11871190
var i: Int? = 1
11881191
var j: Int?
11891192
var k: Int? = 2
11901193

11911194
let _ = [i, j, k].reduce(0 as Int?) {
11921195
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1193-
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Bool'}} {{18-19=}}
1194-
// expected-error@-2 {{cannot force unwrap value of non-optional type 'Bool'}} {{24-25=}}
1195-
// expected-error@-3 {{cannot force unwrap value of non-optional type 'Bool'}} {{36-37=}}
1196-
// expected-error@-4 {{cannot force unwrap value of non-optional type 'Bool'}} {{48-49=}}
11971196
}
11981197

11991198
let _ = [i, j, k].reduce(0 as Int?) {
12001199
$0 && $1 ? $0 + $1 : ($0 ? $0 : ($1 ? $1 : nil))
1201-
// expected-error@-1 {{'nil' cannot be used in context expecting type 'Bool'}}
12021200
}
12031201
}
1202+
*/
12041203

12051204
// https://bugs.swift.org/browse/SR-5934 - failure to emit diagnostic for bad
12061205
// generic constraints
@@ -1237,24 +1236,27 @@ let baz3: (Swift.Error) = (Error()) //expected-error {{value of type 'diagnostic
12371236
let baz4: ((Swift.Error)) = (Error()) //expected-error {{value of type 'diagnostics.Error' does not conform to specified type 'Swift.Error'}}
12381237

12391238
// SyntaxSugarTypes with unresolved types
1240-
// TODO(diagnostics): It should be easy to improve messages here e.g. infer type of `Element` with a fix which would
1241-
// add as many brackets as required by parameter type. It's a bit harder for dictionaries but should be doable.
12421239
func takesGenericArray<T>(_ x: [T]) {}
1243-
takesGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
1240+
takesGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[Int]'}}
12441241
func takesNestedGenericArray<T>(_ x: [[T]]) {}
1245-
takesNestedGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
1242+
takesNestedGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[[Int]]'}}
12461243
func takesSetOfGenericArrays<T>(_ x: Set<[T]>) {}
1247-
takesSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Set<Element>'}}
1244+
takesSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Set<[Int]>'}}
12481245
func takesArrayOfSetOfGenericArrays<T>(_ x: [Set<[T]>]) {}
1249-
takesArrayOfSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
1246+
takesArrayOfSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[Set<[Int]>]'}}
12501247
func takesArrayOfGenericOptionals<T>(_ x: [T?]) {}
1251-
takesArrayOfGenericOptionals(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
1252-
func takesGenericDictionary<T, U>(_ x: [T : U]) {}
1253-
takesGenericDictionary(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<Key, Value>'}}
1248+
takesArrayOfGenericOptionals(1) // expected-error {{cannot convert value of type 'Int' to expected argument type '[Int?]'}}
1249+
func takesGenericDictionary<T, U>(_ x: [T : U]) {} // expected-note {{in call to function 'takesGenericDictionary'}}
1250+
takesGenericDictionary(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '[Any : Any]'}}
1251+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
1252+
// expected-error@-2 {{generic parameter 'U' could not be inferred}}
12541253
typealias Z = Int
1255-
func takesGenericDictionaryWithTypealias<T>(_ x: [T : Z]) {}
1256-
takesGenericDictionaryWithTypealias(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<Key, Value>'}}
1257-
func takesGenericFunction<T>(_ x: ([T]) -> Void) {}
1258-
takesGenericFunction(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '(Array<_>) -> Void'}}
1259-
func takesTuple<T>(_ x: ([T], [T])) {}
1260-
takesTuple(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '(Array<_>, Array<_>)'}}
1254+
func takesGenericDictionaryWithTypealias<T>(_ x: [T : Z]) {} // expected-note {{in call to function 'takesGenericDictionaryWithTypealias'}}
1255+
takesGenericDictionaryWithTypealias(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '[Any : Z]' (aka 'Dictionary<Any, Int>'}}
1256+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
1257+
func takesGenericFunction<T>(_ x: ([T]) -> Void) {} // expected-note {{in call to function 'takesGenericFunction'}}
1258+
takesGenericFunction(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '([Any]) -> Void'}}
1259+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
1260+
func takesTuple<T>(_ x: ([T], [T])) {} // expected-note {{in call to function 'takesTuple'}}
1261+
takesTuple(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '([Any], [Any])'}}
1262+
// expected-error@-1 {{generic parameter 'T' could not be inferred}}

test/Constraints/rdar40002266.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import Foundation
55

66
struct S {
7-
init<T: NSNumber>(_ num: T) {
8-
self.init(num != 0) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'NSNumber'}}
7+
init<T: NSNumber>(_ num: T) { // expected-note {{where 'T' = 'Bool'}}
8+
self.init(num != 0) // expected-error {{initializer 'init(_:)' requires that 'Bool' inherit from 'NSNumber'}}
9+
// expected-error@-1 {{argument type 'T' does not conform to expected type 'BinaryInteger'}}
910
}
1011
}

test/Constraints/tuple.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ variadicWithTrailingClosure(fn: +)
181181
func gcd_23700031<T>(_ a: T, b: T) {
182182
var a = a
183183
var b = b
184-
(a, b) = (b, a % b) // expected-error {{binary operator '%' cannot be applied to two 'T' operands}}
185-
// expected-note @-1 {{overloads for '%' exist with these partially matching parameter lists: (Int, Int), (Int16, Int16), (Int32, Int32), (Int64, Int64), (Int8, Int8), (Self, Self.Scalar), (Self.Scalar, Self), (UInt, UInt), (UInt16, UInt16), (UInt32, UInt32), (UInt64, UInt64), (UInt8, UInt8)}}
184+
(a, b) = (b, a % b) // expected-error {{argument type 'T' does not conform to expected type 'BinaryInteger'}}
186185
}
187186

188187
// <rdar://problem/24210190>

test/Generics/deduction.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ protocol Addable {
245245
static func +(x: Self, y: Self) -> Self
246246
}
247247
func addAddables<T : Addable, U>(_ x: T, y: T, u: U) -> T {
248-
u + u // expected-error{{binary operator '+' cannot be applied to two 'U' operands}}
249-
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: }}
248+
u + u // expected-error{{argument type 'U' does not conform to expected type 'Addable'}}
250249
return x+y
251250
}
252251

0 commit comments

Comments
 (0)