1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// RUN: %target-typecheck-verify-swift
enum E : String {
case foo = "foo"
case bar = "bar" // expected-note {{'bar' declared here}}
}
func fe(_: E) {}
func fe(_: Int) {}
func fe(_: Int, _: E) {}
func fe(_: Int, _: Int) {}
fe(E.baz) // expected-error {{type 'E' has no member 'baz'; did you mean 'bar'?}}
fe(.baz) // expected-error {{reference to member 'baz' cannot be resolved without a contextual type}}
fe(.nope, .nyet) // expected-error {{type 'Int' has no member 'nope'}}
// expected-error@-1 {{reference to member 'nyet' cannot be resolved without a contextual type}}
func fg<T>(_ f: (T) -> T) -> Void {}
fg({x in x}) // expected-error {{cannot infer type of closure parameter 'x' without a type annotation}}
struct S {
func f<T>(_ i: (T) -> T, _ j: Int) -> Void {}
func f(_ d: (Double) -> Double) -> Void {}
func test() -> Void {
f({x in x}, 2) // expected-error {{cannot infer type of closure parameter 'x' without a type annotation}}
}
func g<T>(_ a: T, _ b: Int) -> Void {}
func g(_ a: String) -> Void {}
func test2() -> Void {
g(.notAThing, 7) // expected-error {{cannot infer contextual base in reference to member 'notAThing'}}
}
func h(_ a: Int, _ b: Int) -> Void {}
func h(_ a: String) -> Void {}
func test3() -> Void {
h(.notAThing, 3) // expected-error {{type 'Int' has no member 'notAThing'}}
h(.notAThing) // expected-error {{type 'String' has no member 'notAThing'}}
}
}
struct School {
var name: String
}
func testDiagnoseForAmbiguityCrash(schools: [School]) {
schools.map({ $0.name }).sorted(by: { $0.nothing < $1.notAThing })
// expected-error@-1 {{value of type 'String' has no member 'nothing'}}
// expected-error@-2 {{value of type 'String' has no member 'notAThing'}}
}
class DefaultValue {
static func foo(_ a: Int) {}
static func foo(_ a: Int, _ b: Int = 1) {}
static func foo(_ a: Int, _ b: Int = 1, _ c: Int = 2) {}
}
DefaultValue.foo(1.0, 1) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
class Variadic {
static func foo(_ a: Int) {}
static func foo(_ a: Int, _ b: Double...) {}
}
Variadic.foo(1.0, 2.0, 3.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
// https://github.com/apple/swift/issues/50453
do {
class Suit {
static func foo<T: Any>(_ :inout T) {}
static func foo() {}
}
class RandomNumberGenerator {}
let myRNG = RandomNumberGenerator() // expected-note {{change 'let' to 'var' to make it mutable}}
_ = Suit.foo(&myRNG) // expected-error {{cannot pass immutable value as inout argument: 'myRNG' is a 'let' constant}}
}
// https://github.com/apple/swift/issues/50325
do {
struct S {
func foo() -> UInt { return 0 }
func foo<T: UnsignedInteger>(bar: T) -> T { // expected-note {{where 'T' = 'Int'}}
return bar
}
}
let s = S()
let a = s.foo()
let b = s.foo(bar: 123) // expected-error {{instance method 'foo(bar:)' requires that 'Int' conform to 'UnsignedInteger'}}
let c: UInt = s.foo(bar: 123)
let d = s.foo(bar: 123 as UInt)
}
// https://github.com/apple/swift/issues/49983
do {
struct ITunesGenre {
let genre: Int // expected-note {{'genre' declared here}}
let name: String
}
class Genre {
static func fetch<B: BinaryInteger>(genreID: B, name: String) {}
static func fetch(_ iTunesGenre: ITunesGenre) -> Genre {
return Genre.fetch(genreID: iTunesGenre.genreID, name: iTunesGenre.name)
// expected-error@-1 {{value of type 'ITunesGenre' has no member 'genreID'; did you mean 'genre'?}}
// expected-error@-2 {{cannot convert return expression of type '()' to return type 'Genre'}}
}
}
}
// https://github.com/apple/swift/issues/47730
protocol Scheduler {
func inBackground(run task: @escaping () -> Void)
}
extension Scheduler {
func inBackground(run task: @escaping () -> [Count],
completedBy resultHandler: @escaping ([Count]) -> Void) {}
}
struct Count { // expected-note {{'init(title:)' declared here}}
let title: String
}
func getCounts(_ scheduler: Scheduler, _ handler: @escaping ([Count]) -> Void) {
scheduler.inBackground(run: {
return [Count()] // expected-error {{missing argument for parameter 'title' in call}}
}, completedBy: { // expected-error {{contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored}} {{20-20= _ in}}
})
}
// https://github.com/apple/swift/issues/55133
do {
func f(_ u: UnsafeBufferPointer<UInt16>) {}
let array : [UInt16]
array.withUnsafeBufferPointer {
_ = f(UnsafeRawPointer($0).bindMemory(to: UInt16.self, capacity: 1)) // expected-error {{cannot convert value of type 'UnsafePointer<UInt16>' to expected argument type 'UnsafeBufferPointer<UInt16>'}}
// expected-error@-1 {{cannot convert value of type 'UnsafeBufferPointer<UInt16>' to expected argument type 'UnsafeMutableRawPointer'}}
}
array.withUnsafeBufferPointer {
_ = UnsafeRawPointer($0) as UnsafeBufferPointer<UInt16> // expected-error {{cannot convert value of type 'UnsafeRawPointer' to type 'UnsafeBufferPointer<UInt16>' in coercion}}
// expected-error@-1 {{cannot convert value of type 'UnsafeBufferPointer<UInt16>' to expected argument type 'UnsafeMutableRawPointer'}}
}
}
do {
func f1(_ u: Int) -> String {} // expected-note 2 {{candidate expects value of type 'Int' for parameter #1 (got 'Double')}}
func f1(_ u: String) -> Double {} // expected-note 2 {{candidate expects value of type 'String' for parameter #1 (got 'Double')}}
func f2(_ u: Int) {}
f2(f1(1 as Double)) // expected-error {{no exact matches in call to local function 'f1'}}
f1(1 as Double) as Int // expected-error {{no exact matches in call to local function 'f1'}}
}
do {
// Ambiguous OverloadRefExpr
func f1(_ p: Int) {} // expected-note {{found candidate with type '(Int) -> ()'}}
func f1(_ p: Double) {} // expected-note {{found candidate with type '(Double) -> ()'}}
func f2(_ param: (String)-> Void) {}
f2(f1) // expected-error {{no 'f1' candidates produce the expected type '(String) -> Void' for parameter #0}}
}
// https://forums.swift.org/t/1-x-type-inference/69417/15
protocol N { associatedtype D }
infix operator ++ : AdditionPrecedence
infix operator -- : AdditionPrecedence
func ++ <T: N> (lhs: T, rhs: T) -> T.D { fatalError() } // expected-note {{found this candidate}}
func ++ <T: N> (lhs: T, rhs: T) -> T { fatalError() } // expected-note {{found this candidate}}
func -- <T: N> (lhs: T, rhs: T) -> T { fatalError() } // expected-note {{found this candidate}}
func -- <T: N> (lhs: T, rhs: T) -> T.D { fatalError() } // expected-note {{found this candidate}}
do {
struct MyInt16: N { typealias D = MyInt32 }
struct MyInt32: N { typealias D = MyInt64 }
struct MyInt64 {}
var i16 = MyInt16()
let _ = i16 ++ i16
// expected-error@-1 {{ambiguous use of operator '++'}}
let _ = i16 -- i16
// expected-error@-1 {{ambiguous use of operator '--'}}
}
|