// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.50 -disable-objc-attr-requires-foundation-module // RUN: not %target-swift-frontend -target %target-cpu-apple-macosx10.50 -disable-objc-attr-requires-foundation-module -typecheck %s 2>&1 | %FileCheck %s '--implicit-check-not=:0' // Make sure we do not emit availability errors or warnings when -disable-availability-checking is passed // RUN: not %target-swift-frontend -target %target-cpu-apple-macosx10.50 -typecheck -disable-objc-attr-requires-foundation-module -disable-availability-checking %s -diagnostic-style llvm 2>&1 | %FileCheck %s '--implicit-check-not=error:' // REQUIRES: OS=macosx func hedge() { struct Value {} // We rely on not allowing nesting of extensions, so test to make sure // this emits an error. // CHECK:error: declaration is only valid at file scope extension Value { } // expected-error {{declaration is only valid at file scope}} } protocol P { associatedtype T } struct Wrapper {} func identity(_ x: any P) -> any P { return x } // OK func unwrapUnwrap(_ x: (any P)???) -> (any P)? { return x!! } // OK func erase(_ x: any P) -> Any { return x } func nonerasingFunction(_ f: @escaping (any P) -> ()) -> Any { return 0 } func eraseFunction(_ f: @escaping (any P) -> ()) -> Any { return f } // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 {{add '@available' attribute to enclosing global function}} // expected-note@-2 {{add 'if #available' version check}} // These are okay because we can convert between existentials without metadata. func eraseFunctionCovariant(_ f: @escaping () -> any P) -> (() -> Any) { return f } func eraseFunctionContravariant(_ f: @escaping (Any) -> ()) -> ((any P) -> Any) { return f } // We cannot convert from an optional existential to an existential without // metadata. func eraseFunctionCovariantOptional(_ f: @escaping () -> (any P)?) -> (() -> Any) { return f // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-2 {{add '@available' attribute to enclosing global function}} // expected-note@-2 {{add 'if #available' version check}} } func eraseFunctionContravariantOptional(_ f: @escaping (Any) -> ()) -> (((any P)?) -> Any) { return f // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-2 {{add '@available' attribute to enclosing global function}} // expected-note@-2 {{add 'if #available' version check}} } func eraseOptional(_ x: (any P)?) -> Any { return x } // expected-note@-1 {{add '@available' attribute to enclosing global function}} // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 {{add 'if #available' version check}} // expected-warning@-4 {{expression implicitly coerced from '(any P)?' to 'Any'}} // expected-note@-5 {{provide a default value to avoid this warning}} // expected-note@-6 {{force-unwrap the value to avoid this warning}} // expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}} func eraseOptional2(_ x: (any P)?) -> Any { return x as Any } // expected-note@-1 {{add '@available' attribute to enclosing global function}} // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 {{add 'if #available' version check}} func tupleOut() -> (any P, Int) { return tupleOut() } func tupleIn(_ xs: (any P, Int)) -> Int { return tupleIn(xs) } func wrap(_ x: any P) -> Wrapper> { return wrap(x) } // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 {{add '@available' attribute to enclosing global function}} func optionalWrap(_ x: any P) -> Wrapper<(any P)?> { return optionalWrap(x) } // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 {{add '@available' attribute to enclosing global function}} struct UnavailableWitness: P { // expected-note {{add '@available' attribute to enclosing struct}} typealias T = any P // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 {{add '@available' attribute to enclosing type alias}} } struct UnavailableOptionalWitness: P { // expected-note {{add '@available' attribute to enclosing struct}} typealias T = (any P)? // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 {{add '@available' attribute to enclosing type alias}} } struct UnavailableWrappedWitness: P { // expected-note 2 {{add '@available' attribute to enclosing struct}} typealias T = Wrapper> // expected-error 2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-1 2 {{add '@available' attribute to enclosing type alias}} } struct ParameterizedMembers { // expected-note {{add '@available' attribute to enclosing struct}} var ok: any P var okOptional: (any P)? var broken: Wrapper<(any P)?> // expected-error {{runtime support for parameterized protocol types is only available in}} } func casts() { // expected-note 4 {{add '@available' attribute to enclosing global function}} struct Value: P { typealias T = String } let _ = Value() as any P // OK let _ = Value() as! any P // expected-warning@-1 {{forced cast from 'Value' to 'any P' always succeeds; did you mean to use 'as'?}} // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 {{add 'if #available' version check}} let _ = Value() is any P // expected-warning@-1 {{'is' test is always true}} // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 {{add 'if #available' version check}} let _ = Value() is (any P)??? // expected-warning@-1 {{'is' test is always true}} // expected-error@-2 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 {{add 'if #available' version check}} let _ = Value() as! (any P, Int) // expected-warning@-1 {{cast from 'Value' to unrelated type '(any P, Int)' always fails}} // expected-error@-2 1 {{runtime support for parameterized protocol types is only available in}} // expected-note@-3 1 {{add 'if #available' version check}} } // FIXME: It's a little aggressive to also ban metatypes. func metatypes(_ x: T.Type) { // expected-note 2 {{add '@available' attribute to enclosing global function}} metatypes((any P).self) // expected-error@-1 {{runtime support for parameterized protocol types is only available in}} // expected-note@-2 {{add 'if #available' version check}} metatypes((any P.Type).self) // expected-error@-1 {{runtime support for parameterized protocol types is only available in}} // expected-note@-2 {{add 'if #available' version check}} } func tupleConversion1(_ tuple: (any P, Int)) { let converted: (any P, Int?) = tuple _ = converted } func tupleConversion2(_ tuple: (any P, Int)) { let converted: (Any, Int?) = tuple _ = converted } func tupleConversion3(_ tuple: ((any P)?, Int)) { // expected-note @-1 {{add '@available' attribute to enclosing global function}} let converted: (Any, Int?) = tuple // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note @-1 {{add 'if #available' version check}} // expected-warning @-3 {{expression implicitly coerced from '(any P)?' to 'Any'}} // expected-note @-4 {{explicitly cast to 'Any'}} // expected-note @-5 {{force-unwrap the value}} // expected-note @-6 {{provide a default value}} _ = converted } func tupleCovariantConversion1(fn: @escaping () -> (any P, Int)) -> (() -> (Any, Int)) { return fn } func tupleCovariantConversion2(fn: @escaping () -> ((any P)?, Int)) -> (() -> (Any, Int)) { // expected-note @-1 {{add '@available' attribute to enclosing global function}} return fn // expected-error {{runtime support for parameterized protocol types is only available in}} // expected-note @-1 {{add 'if #available' version check}} }