|
| 1 | +// RUN: %target-typecheck-verify-swift \ |
| 2 | +// RUN: -debug-diagnostic-names -target arm64-apple-macos14.4 \ |
| 3 | +// RUN: -enable-experimental-feature NonescapableTypes |
| 4 | + |
| 5 | +// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchOS || OS=xros |
| 6 | + |
| 7 | +protocol P {} |
| 8 | +struct NCG<T: ~Copyable> {} |
| 9 | +extension NCG: P where T: Copyable {} // expected-note 2{{requirement_implied_by_conditional_conformance}} |
| 10 | + |
| 11 | +struct NEG<T: ~Escapable> {} |
| 12 | +extension NEG: P {} // expected-note {{requirement_implied_by_conditional_conformance}} |
| 13 | + |
| 14 | +struct All {} |
| 15 | +struct NoCopy: ~Copyable {} |
| 16 | +struct NoEscape: ~Escapable {} |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +/// MARK: dynamic casts are gated by availability. Older runtimes don't check |
| 21 | +/// for conformance to Copyable so they'll give bogus results. |
| 22 | + |
| 23 | +// expected-note@+1 8{{availability_add_attribute}} |
| 24 | +func dyn_cast_errors<T: ~Copyable, V: ~Escapable>( |
| 25 | + _ generic: NCG<T>, _ concrete: NCG<NoCopy>, |
| 26 | + _ genericEsc: NEG<V>, _ concreteEsc: NEG<NoEscape>) { |
| 27 | + _ = concrete as? any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 28 | + _ = generic as? any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 29 | + |
| 30 | + _ = concrete is any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 31 | + _ = generic is any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 32 | + |
| 33 | + _ = concrete as! any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 34 | + _ = generic as! any P // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 35 | + |
| 36 | + _ = genericEsc as? any P // expected-error {{availability_escapable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 37 | + _ = concreteEsc is any P // expected-error {{availability_escapable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 38 | +} |
| 39 | + |
| 40 | +@available(SwiftStdlib 6.0, *) |
| 41 | +func FIXED_dyn_cast_errors<T: ~Copyable, V: ~Escapable>( |
| 42 | + _ generic: NCG<T>, _ concrete: NCG<NoCopy>, |
| 43 | + _ genericEsc: NEG<V>, _ concreteEsc: NEG<NoEscape>) { |
| 44 | + _ = concrete as? any P |
| 45 | + _ = generic as? any P |
| 46 | + |
| 47 | + _ = concrete is any P |
| 48 | + _ = generic is any P |
| 49 | + |
| 50 | + _ = concrete as! any P |
| 51 | + _ = generic as! any P |
| 52 | + |
| 53 | + _ = genericEsc as? any P |
| 54 | + _ = concreteEsc is any P |
| 55 | +} |
| 56 | + |
| 57 | +func noAvailabilityNeeded<T>(_ generic: NCG<T>, _ concrete: NCG<All>) { |
| 58 | + _ = concrete as? any P // expected-warning {{conditional_downcast_coercion}} |
| 59 | + _ = generic as? any P // expected-warning {{conditional_downcast_coercion}} |
| 60 | + |
| 61 | + _ = concrete is any P // expected-warning {{isa_is_always_true}} |
| 62 | + _ = generic is any P // expected-warning {{isa_is_always_true}} |
| 63 | + |
| 64 | + _ = concrete as! any P // expected-warning {{forced_downcast_coercion}} |
| 65 | + _ = generic as! any P // expected-warning {{forced_downcast_coercion}} |
| 66 | + |
| 67 | + _ = concrete as any P |
| 68 | + _ = generic as any P |
| 69 | + |
| 70 | + _ = concrete as Any |
| 71 | + _ = generic as Any |
| 72 | +} |
| 73 | + |
| 74 | +func expected_checked_cast_errors<T: ~Copyable>(_ generic: NCG<T>, _ concrete: NCG<NoCopy>, |
| 75 | + _ concreteEsc: NEG<NoEscape>) { |
| 76 | + _ = concrete as any P // expected-error {{type_does_not_conform_decl_owner}} |
| 77 | + _ = generic as any P // expected-error {{type_does_not_conform_decl_owner}} |
| 78 | + _ = concreteEsc as any P // expected-error {{type_does_not_conform_decl_owner}} |
| 79 | +} |
| 80 | + |
| 81 | +/// MARK: existential erasure requires availability, because later dynamic casts |
| 82 | +/// of that erased type will not correctly check for Copyable generic args. |
| 83 | + |
| 84 | +func eraseImplicit(_ a: Any) {} |
| 85 | + |
| 86 | +// expected-note@+1 9{{availability_add_attribute}} |
| 87 | +func erasure_cast_disallowed<T: ~Copyable>(_ generic: NCG<T>, _ concrete: NCG<NoCopy>, _ concreteEsc: NEG<NoEscape>) -> Any { |
| 88 | + _ = concrete as Any // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 89 | + _ = concreteEsc as Any // expected-error {{availability_escapable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 90 | + _ = generic as Any // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 91 | + |
| 92 | + let _: Any = concrete // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 93 | + let _: Any = generic // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 94 | + |
| 95 | + let _: Any = { concreteEsc }() // expected-error {{availability_escapable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 96 | + |
| 97 | + eraseImplicit(concrete) // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 98 | + eraseImplicit(generic) // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 99 | + |
| 100 | + return concrete // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 101 | +} |
| 102 | + |
| 103 | +struct Box<Wrapped: ~Copyable>: ~Copyable { // expected-note {{availability_add_attribute}} |
| 104 | + private let _pointer: UnsafeMutablePointer<Wrapped> |
| 105 | + |
| 106 | + init(_ element: consuming Wrapped) { // expected-note {{availability_add_attribute}} |
| 107 | + _pointer = .allocate(capacity: 1) |
| 108 | + print("allocating",_pointer) // expected-error {{availability_copyable_generics_casting_only_version_newer}} // expected-note {{availability_guard_with_version_check}} |
| 109 | + _pointer.initialize(to: element) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// MARK: misc. operations that are permitted |
| 114 | + |
| 115 | +public protocol Debatable: ~Copyable {} |
| 116 | + |
| 117 | +public func asExistential(_ t: consuming any Debatable & ~Copyable) {} |
| 118 | + |
| 119 | +public func hello<T: Debatable & ~Copyable>(_ t: consuming T) { |
| 120 | + asExistential(t) |
| 121 | +} |
| 122 | + |
| 123 | +extension UnsafeMutableRawPointer { |
| 124 | + public func blahInitializeMemory<T: ~Copyable>( |
| 125 | + as type: T.Type, from source: UnsafeMutablePointer<T>, count: Int |
| 126 | + ) { |
| 127 | + _ = UnsafeMutableRawPointer(source + count) |
| 128 | + } |
| 129 | +} |
0 commit comments