// RUN: %target-parse-verify-swift // XFAIL: linux class C {} class D {} func takesMutablePointer(x: UnsafeMutablePointer) {} func takesMutableVoidPointer(x: UnsafeMutablePointer) {} func takesMutableInt8Pointer(x: UnsafeMutablePointer) {} func takesMutableArrayPointer(x: UnsafeMutablePointer<[Int]>) {} func takesConstPointer(x: UnsafePointer) -> Character { return "x" } func takesConstInt8Pointer(x: UnsafePointer) {} func takesConstUInt8Pointer(x: UnsafePointer) {} func takesConstVoidPointer(x: UnsafePointer) {} func takesAutoreleasingPointer(x: AutoreleasingUnsafeMutablePointer) {} func mutablePointerArguments(p: UnsafeMutablePointer, cp: UnsafePointer, ap: AutoreleasingUnsafeMutablePointer) { takesMutablePointer(nil) takesMutablePointer(p) takesMutablePointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafeMutablePointer'}} takesMutablePointer(ap) // expected-error{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'}} var i: Int = 0 var f: Float = 0 takesMutablePointer(&i) takesMutablePointer(&f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'Int'}} takesMutablePointer(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer'}} takesMutablePointer(f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'UnsafeMutablePointer'}} var ii: [Int] = [0, 1, 2] var ff: [Float] = [0, 1, 2] takesMutablePointer(&ii) takesMutablePointer(&ff) // expected-error{{cannot convert value of type '[Float]' to expected argument type 'Int'}} takesMutablePointer(ii) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutablePointer'}} takesMutablePointer(ff) // expected-error{{cannot convert value of type '[Float]' to expected argument type 'UnsafeMutablePointer'}} takesMutableArrayPointer(&i) // expected-error{{cannot convert value of type 'Int' to expected argument type '[Int]'}} takesMutableArrayPointer(&ii) // We don't allow these conversions outside of function arguments. var x: UnsafeMutablePointer = &i // expected-error{{cannot pass immutable value of type 'Int' as inout argument}} x = &ii // expected-error{{cannot assign value of type '[Int]' to type 'Int'}} _ = x } func mutableVoidPointerArguments(p: UnsafeMutablePointer, cp: UnsafePointer, ap: AutoreleasingUnsafeMutablePointer, fp: UnsafeMutablePointer) { takesMutableVoidPointer(nil) takesMutableVoidPointer(p) takesMutableVoidPointer(fp) takesMutableVoidPointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} takesMutableVoidPointer(ap) // expected-error{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} var i: Int = 0 var f: Float = 0 takesMutableVoidPointer(&i) takesMutableVoidPointer(&f) takesMutableVoidPointer(i) // expected-error{{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} takesMutableVoidPointer(f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} var ii: [Int] = [0, 1, 2] var dd: [CInt] = [1, 2, 3] var ff: [Int] = [0, 1, 2] takesMutableVoidPointer(&ii) takesMutableVoidPointer(&dd) takesMutableVoidPointer(&ff) takesMutableVoidPointer(ii) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} takesMutableVoidPointer(ff) // expected-error{{cannot convert value of type '[Int]' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} // We don't allow these conversions outside of function arguments. var x: UnsafeMutablePointer = &i // expected-error{{cannot convert value of type 'inout Int' to specified type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} x = &ii // expected-error{{cannot assign value of type 'inout [Int]' (aka 'inout Array') to type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} _ = x } func constPointerArguments(p: UnsafeMutablePointer, cp: UnsafePointer, ap: AutoreleasingUnsafeMutablePointer) { takesConstPointer(nil) takesConstPointer(p) takesConstPointer(cp) takesConstPointer(ap) var i: Int = 0 var f: Float = 0 takesConstPointer(&i) takesConstPointer(&f) // expected-error{{cannot convert value of type 'Float' to expected argument type 'Int'}} var ii: [Int] = [0, 1, 2] var ff: [Float] = [0, 1, 2] takesConstPointer(&ii) takesConstPointer(&ff) // expected-error{{cannot convert value of type '[Float]' to expected argument type 'Int'}} takesConstPointer(ii) takesConstPointer(ff) // expected-error{{cannot convert value of type '[Float]' to expected argument type 'UnsafePointer'}} takesConstPointer([0, 1, 2]) // QoI: CSDiags doesn't handle array -> pointer impl conversions well takesConstPointer([0.0, 1.0, 2.0]) // expected-error{{cannot convert value of type 'Double' to expected element type 'Int'}} // We don't allow these conversions outside of function arguments. var x: UnsafePointer = &i // expected-error{{cannot pass immutable value of type 'Int' as inout argument}} x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafePointer'}} x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafePointer'}} x = ap // expected-error{{cannot assign value of type 'AutoreleasingUnsafeMutablePointer' to type 'UnsafePointer'}} } func constVoidPointerArguments(p: UnsafeMutablePointer, fp: UnsafeMutablePointer, cp: UnsafePointer, cfp: UnsafePointer, ap: AutoreleasingUnsafeMutablePointer, afp: AutoreleasingUnsafeMutablePointer) { takesConstVoidPointer(nil) takesConstVoidPointer(p) takesConstVoidPointer(fp) takesConstVoidPointer(cp) takesConstVoidPointer(cfp) takesConstVoidPointer(ap) takesConstVoidPointer(afp) var i: Int = 0 var f: Float = 0 takesConstVoidPointer(&i) takesConstVoidPointer(&f) var ii: [Int] = [0, 1, 2] var ff: [Float] = [0, 1, 2] takesConstVoidPointer(&ii) takesConstVoidPointer(&ff) takesConstVoidPointer(ii) takesConstVoidPointer(ff) // TODO: These two should be accepted, tracked by rdar://17444930. takesConstVoidPointer([0, 1, 2]) // expected-error {{cannot convert value of type 'Int' to expected element type '()'}} takesConstVoidPointer([0.0, 1.0, 2.0]) // expected-error {{cannot convert value of type 'Double' to expected element type '()'}} // We don't allow these conversions outside of function arguments. var x: UnsafePointer = &i // expected-error{{cannot convert value of type 'inout Int' to specified type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = ii // expected-error{{cannot assign value of type '[Int]' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = p // expected-error{{cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = fp // expected-error{{cannot assign value of type 'UnsafeMutablePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = cp // expected-error{{cannot assign value of type 'UnsafePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = cfp // expected-error{{cannot assign value of type 'UnsafePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = ap // expected-error{{cannot assign value of type 'AutoreleasingUnsafeMutablePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} x = afp // expected-error{{cannot assign value of type 'AutoreleasingUnsafeMutablePointer' to type 'UnsafePointer' (aka 'UnsafePointer<()>')}} _ = x } func stringArguments(s: String) { var s = s takesConstVoidPointer(s) takesConstInt8Pointer(s) takesConstUInt8Pointer(s) takesConstPointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafePointer'}} takesMutableVoidPointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<()>')}} takesMutableInt8Pointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutablePointer'}} takesMutableInt8Pointer(&s) // expected-error{{cannot convert value of type 'String' to expected argument type 'Int8'}} takesMutablePointer(s) // expected-error{{cannot convert value of type 'String' to expected argument type 'UnsafeMutablePointer'}} takesMutablePointer(&s) // expected-error{{cannot convert value of type 'String' to expected argument type 'Int'}} } func autoreleasingPointerArguments(p: UnsafeMutablePointer, cp: UnsafePointer, ap: AutoreleasingUnsafeMutablePointer) { takesAutoreleasingPointer(nil) takesAutoreleasingPointer(p) // expected-error{{cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'AutoreleasingUnsafeMutablePointer'}} takesAutoreleasingPointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer' to expected argument type 'AutoreleasingUnsafeMutablePointer'}} takesAutoreleasingPointer(ap) var c: C = C() takesAutoreleasingPointer(&c) takesAutoreleasingPointer(c) // expected-error{{cannot convert value of type 'C' to expected argument type 'AutoreleasingUnsafeMutablePointer'}} var d: D = D() takesAutoreleasingPointer(&d) // expected-error{{cannot convert value of type 'D' to expected argument type 'C'}} takesAutoreleasingPointer(d) // expected-error{{cannot convert value of type 'D' to expected argument type 'AutoreleasingUnsafeMutablePointer'}} var cc: [C] = [C(), C()] var dd: [D] = [D(), D()] takesAutoreleasingPointer(&cc) // expected-error{{cannot convert value of type '[C]' to expected argument type 'C'}} takesAutoreleasingPointer(&dd) // expected-error{{cannot convert value of type '[D]' to expected argument type 'C'}} let _: AutoreleasingUnsafeMutablePointer = &c // expected-error{{cannot pass immutable value of type 'C' as inout argument}} } func pointerConstructor(x: UnsafeMutablePointer) -> UnsafeMutablePointer { return UnsafeMutablePointer(x) } func pointerArithmetic(x: UnsafeMutablePointer, y: UnsafeMutablePointer, i: Int) { _ = x + i _ = x - y } func genericPointerArithmetic(x: UnsafeMutablePointer, i: Int, t: T) -> UnsafeMutablePointer { let p = x + i p.initialize(with: t) } func passPointerToClosure(f: UnsafeMutablePointer -> Int) -> Int { } func pointerInClosure(f: UnsafeMutablePointer -> Int) -> Int { return passPointerToClosure { f(UnsafeMutablePointer($0)) } } struct NotEquatable {} func arrayComparison(x: [NotEquatable], y: [NotEquatable], p: UnsafeMutablePointer) { var x = x // Don't allow implicit array-to-pointer conversions in operators. let a: Bool = x == y // expected-error{{binary operator '==' cannot be applied to two '[NotEquatable]' operands}} // expected-note @-1 {{overloads for '==' exist with these partially matching parameter lists:}} let _: Bool = p == &x // Allowed! } func addressConversion(p: UnsafeMutablePointer, x: Int) { var x = x let _: Bool = p == &x } // QoI: poor error: '&' used with non-inout argument of type 'UnsafeMutablePointer' func f19478919() { var viewport: Int = 1 // intentionally incorrect type, not Int32 func GLKProject(a : UnsafeMutablePointer) {} GLKProject(&viewport) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Int32'}} func GLKProjectUP(a : UnsafePointer) {} func UP_Void(a : UnsafePointer) {} func UMP_Void(a : UnsafeMutablePointer) {} UP_Void(&viewport) UMP_Void(&viewport) let cst = 42 // expected-note 2 {{change 'let' to 'var' to make it mutable}} UP_Void(&cst) // expected-error {{cannot pass immutable value as inout argument: 'cst' is a 'let' constant}} UMP_Void(&cst) // expected-error {{cannot pass immutable value as inout argument: 'cst' is a 'let' constant}} } // QoI: Poor diagnostic with let vs. var passed to C function func f23202128() { func UP(p: UnsafePointer) {} func UMP(p: UnsafeMutablePointer) {} let pipe: [Int32] = [0, 0] // expected-note {{change 'let' to 'var' to make it mutable}}}} UMP(&pipe) // expected-error {{cannot pass immutable value as inout argument: 'pipe' is a 'let' constant}} var pipe2: [Int] = [0, 0] UMP(&pipe2) // expected-error {{cannot convert value of type '[Int]' to expected argument type 'Int32'}} UP(pipe) // ok UP(&pipe) // expected-error {{'&' is not allowed passing array value as 'UnsafePointer' argument}} {{6-7=}} }