blob: 28ef400872fe83c41ce492c1f6ccdb4eb6da8515 [file] [log] [blame]
// RUN: %empty-directory(%t)
// RUN: %gyb -DOPT_KIND=None %s -o %t/pointer_conversion.swift
// RUN: %line-directive %t/pointer_conversion.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion.swift
// RUN: %gyb -DOPT_KIND=Optional %s -o %t/pointer_conversion_opt.swift
// RUN: %line-directive %t/pointer_conversion_opt.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion_opt.swift
// RUN: %gyb -DOPT_KIND=ImplicitlyUnwrappedOptional %s -o %t/pointer_conversion_iuo.swift
// RUN: %line-directive %t/pointer_conversion_iuo.swift -- %target-swift-frontend -typecheck -verify %t/pointer_conversion_iuo.swift
// REQUIRES: objc_interop
%{
if OPT_KIND == 'Optional':
suffix='?'
diag_suffix='?'
elif OPT_KIND == 'ImplicitlyUnwrappedOptional':
suffix='!'
diag_suffix='?'
else:
suffix=''
diag_suffix=''
}%
class C {}
class D {}
func takesMutablePointer(_ x: UnsafeMutablePointer<Int>${suffix}) {}
func takesMutableVoidPointer(_ x: UnsafeMutableRawPointer${suffix}) {}
@discardableResult
func takesConstPointer(_ x: UnsafePointer<Int>${suffix}) -> Character { return "x" }
func takesConstVoidPointer(_ x: UnsafeRawPointer${suffix}) {}
func takesAutoreleasingPointer(_ x: AutoreleasingUnsafeMutablePointer<C>${suffix}) {}
func pointerArgumentsObjC(ap: AutoreleasingUnsafeMutablePointer<Int>,
afp: AutoreleasingUnsafeMutablePointer<Float>) {
takesMutablePointer(ap) // expected-error{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<Int>${diag_suffix}'}}
takesMutableVoidPointer(ap) // expected-error{{cannot convert value of type 'AutoreleasingUnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutableRawPointer${diag_suffix}'}}
takesConstPointer(ap)
takesConstVoidPointer(ap)
takesConstVoidPointer(afp)
var x: UnsafeRawPointer
x = ap // expected-error{{cannot assign value of type 'AutoreleasingUnsafeMutablePointer<Int>' to type 'UnsafeRawPointer'}}
_ = x
}
func autoreleasingPointerArguments(p: UnsafeMutablePointer<Int>,
cp: UnsafePointer<Int>,
ap: AutoreleasingUnsafeMutablePointer<C>) {
takesAutoreleasingPointer(nil)
% if not suffix:
// expected-error@-2 {{'nil' is not compatible with expected argument type}}
% end
takesAutoreleasingPointer(p) // expected-error{{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'AutoreleasingUnsafeMutablePointer<C>${diag_suffix}'}}
takesAutoreleasingPointer(cp) // expected-error{{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'AutoreleasingUnsafeMutablePointer<C>${diag_suffix}'}}
takesAutoreleasingPointer(ap)
var c: C = C()
takesAutoreleasingPointer(&c)
takesAutoreleasingPointer(c) // expected-error{{cannot convert value of type 'C' to expected argument type 'AutoreleasingUnsafeMutablePointer<C>${diag_suffix}'}}
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<C>${diag_suffix}'}}
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> = &c // expected-error {{use of extraneous '&'}}
}