|
| 1 | +// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.50 -disable-objc-attr-requires-foundation-module -enable-parameterized-existential-types |
| 2 | +// RUN: not %target-swift-frontend -target %target-cpu-apple-macosx10.50 -disable-objc-attr-requires-foundation-module -enable-parameterized-existential-types -typecheck %s 2>&1 | %FileCheck %s '--implicit-check-not=<unknown>:0' |
| 3 | + |
| 4 | +// Make sure we do not emit availability errors or warnings when -disable-availability-checking is passed |
| 5 | +// RUN: not %target-swift-frontend -target %target-cpu-apple-macosx10.50 -typecheck -disable-objc-attr-requires-foundation-module -enable-parameterized-existential-types -disable-availability-checking %s 2>&1 | %FileCheck %s '--implicit-check-not=error:' |
| 6 | + |
| 7 | +func hedge() { |
| 8 | + struct Value {} |
| 9 | + |
| 10 | + // We rely on not allowing nesting of extensions, so test to make sure |
| 11 | + // this emits an error. |
| 12 | + // CHECK:error: declaration is only valid at file scope |
| 13 | + extension Value { } // expected-error {{declaration is only valid at file scope}} |
| 14 | +} |
| 15 | + |
| 16 | +protocol P<T> { |
| 17 | + associatedtype T |
| 18 | +} |
| 19 | + |
| 20 | +struct Wrapper<T> {} |
| 21 | + |
| 22 | +func identity<T>(_ x: any P<T>) -> any P<T> { return x } // OK |
| 23 | +func unwrapUnwrap<T>(_ x: (any P<T>)???) -> (any P<T>)? { return x!! } // OK |
| 24 | + |
| 25 | +func erase<T>(_ x: any P<T>) -> Any { return x } // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 26 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 27 | +// expected-note@-2 {{add 'if #available' version check}} |
| 28 | + |
| 29 | +func eraseOptional<T>(_ x: (any P<T>)?) -> Any { return x } |
| 30 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 31 | +// expected-error@-2 {{runtime support for parameterized protocol types is only available in}} |
| 32 | +// expected-note@-3 {{add 'if #available' version check}} |
| 33 | +// expected-warning@-4 {{expression implicitly coerced from '(any P<T>)?' to 'Any'}} |
| 34 | +// expected-note@-5 {{provide a default value to avoid this warning}} |
| 35 | +// expected-note@-6 {{force-unwrap the value to avoid this warning}} |
| 36 | +// expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}} |
| 37 | + |
| 38 | +func eraseOptional2<T>(_ x: (any P<T>)?) -> Any { return x as Any } |
| 39 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 40 | +// expected-error@-2 {{runtime support for parameterized protocol types is only available in}} |
| 41 | +// expected-note@-3 {{add 'if #available' version check}} |
| 42 | + |
| 43 | +func tupleOut<T>() -> (any P<T>, Int) { return tupleOut() } // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 44 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 45 | +func tupleIn<T>(_ xs: (any P<T>, Int)) -> Int { return tupleIn(xs) } // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 46 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 47 | +func wrap<T>(_ x: any P<T>) -> Wrapper<any P<T>> { return wrap(x) } // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 48 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 49 | +func optionalWrap<T>(_ x: any P<T>) -> Wrapper<(any P<T>)?> { return optionalWrap(x) } // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 50 | +// expected-note@-1 {{add @available attribute to enclosing global function}} |
| 51 | + |
| 52 | +struct UnavailableWitness: P { // expected-note {{add @available attribute to enclosing struct}} |
| 53 | + typealias T = any P<String> // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 54 | + // expected-note@-1 {{add @available attribute to enclosing type alias}} |
| 55 | +} |
| 56 | + |
| 57 | +struct UnavailableOptionalWitness: P { // expected-note {{add @available attribute to enclosing struct}} |
| 58 | + typealias T = (any P<String>)? // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 59 | + // expected-note@-1 {{add @available attribute to enclosing type alias}} |
| 60 | +} |
| 61 | + |
| 62 | +struct UnavailableWrappedWitness: P { // expected-note 2 {{add @available attribute to enclosing struct}} |
| 63 | + typealias T = Wrapper<any P<String>> // expected-error 2 {{runtime support for parameterized protocol types is only available in}} |
| 64 | + // expected-note@-1 2 {{add @available attribute to enclosing type alias}} |
| 65 | +} |
| 66 | + |
| 67 | +struct ParameterizedMembers { // expected-note {{add @available attribute to enclosing struct}} |
| 68 | + var ok: any P<String> |
| 69 | + var okOptional: (any P<String>)? |
| 70 | + |
| 71 | + var broken: Wrapper<(any P<String>)?> // expected-error {{runtime support for parameterized protocol types is only available in}} |
| 72 | +} |
| 73 | + |
| 74 | +func casts() { // expected-note 5 {{add @available attribute to enclosing global function}} |
| 75 | + struct Value: P { typealias T = String } |
| 76 | + |
| 77 | + let _ = Value() as any P<String> // OK |
| 78 | + let _ = Value() as! any P<String> |
| 79 | + // expected-warning@-1 {{forced cast from 'Value' to 'any P<String>' always succeeds; did you mean to use 'as'?}} |
| 80 | + // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} |
| 81 | + // expected-note@-3 {{add 'if #available' version check}} |
| 82 | + |
| 83 | + let _ = Value() is any P<String> |
| 84 | + // expected-warning@-1 {{'is' test is always true}} |
| 85 | + // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} |
| 86 | + // expected-note@-3 {{add 'if #available' version check}} |
| 87 | + |
| 88 | + let _ = Value() is (any P<String>)??? |
| 89 | + // expected-warning@-1 {{'is' test is always true}} |
| 90 | + // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} |
| 91 | + // expected-note@-3 {{add 'if #available' version check}} |
| 92 | + |
| 93 | + let _ = Value() as! (any P<String>, Int) |
| 94 | + // expected-warning@-1 {{cast from 'Value' to unrelated type '(any P<String>, Int)' always fails}} |
| 95 | + // expected-error@-2 2 {{runtime support for parameterized protocol types is only available in}} |
| 96 | + // expected-note@-3 2 {{add 'if #available' version check}} |
| 97 | +} |
| 98 | + |
| 99 | +// FIXME: It's a little aggressive to also ban metatypes. |
| 100 | +func metatypes<T>(_ x: T.Type) { // expected-note 2 {{add @available attribute to enclosing global function}} |
| 101 | + metatypes((any P<T>).self) |
| 102 | + // expected-error@-1 {{runtime support for parameterized protocol types is only available in}} |
| 103 | + // expected-note@-2 {{add 'if #available' version check}} |
| 104 | + |
| 105 | + metatypes((any P<T>.Type).self) |
| 106 | + // expected-error@-1 {{runtime support for parameterized protocol types is only available in}} |
| 107 | + // expected-note@-2 {{add 'if #available' version check}} |
| 108 | +} |
0 commit comments