|
| 1 | +// RUN: %target-typecheck-verify-swift -disable-availability-checking -warn-redundant-requirements -enable-experimental-implicit-some |
| 2 | + |
| 3 | +// Access Return Type Constraint |
| 4 | +protocol Cafe { |
| 5 | + func breakfast() |
| 6 | + func treats() |
| 7 | +} |
| 8 | + |
| 9 | +class SeasonalMenu : Cafe { |
| 10 | + func breakfast() {} |
| 11 | + func treats() {} |
| 12 | +} |
| 13 | + |
| 14 | +func getCurrentMenu () -> some Cafe { |
| 15 | + return SeasonalMenu() |
| 16 | +} |
| 17 | + |
| 18 | +var cafe = getCurrentMenu() |
| 19 | +cafe.breakfast() |
| 20 | +cafe.treats() |
| 21 | + |
| 22 | +// Type alias + custom protocols |
| 23 | +typealias Snack = Shop & Cafe |
| 24 | +typealias Meal = Eatery |
| 25 | + |
| 26 | +struct CoffeeBar: Snack { |
| 27 | + func coffee(){ } |
| 28 | + func breakfast() { } |
| 29 | + func treats(){ } |
| 30 | +} |
| 31 | + |
| 32 | +class Best: Eatery { |
| 33 | + func lunch() { }; |
| 34 | + func dinner() { } |
| 35 | + func dessert() { } |
| 36 | +} |
| 37 | + |
| 38 | +class TopTier: Eatery { |
| 39 | + func lunch() { }; |
| 40 | + func dinner() { } |
| 41 | + func dessert() { } |
| 42 | +} |
| 43 | + |
| 44 | +func list(_: Snack) {} |
| 45 | + |
| 46 | +func find( _: Snack) -> Snack { } // expected-error {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}} |
| 47 | + |
| 48 | +// tuple types |
| 49 | +func list(_: (Meal, Meal)) {} |
| 50 | + |
| 51 | +func highestRated() -> (Eatery, Eatery) { |
| 52 | + return (Best(), TopTier()) |
| 53 | +} |
| 54 | + |
| 55 | +func highestRated() -> (some Snack, some Snack) { } // expected-error {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}} |
| 56 | + |
| 57 | +// opaque compostion types |
| 58 | +func find() -> Shop & Cafe { |
| 59 | + return CoffeeBar() |
| 60 | +} |
| 61 | + |
| 62 | +func find() -> AnyObject { |
| 63 | + return CoffeeBar() // expected-error {{Return expression of type 'CoffeeBar' expected to be an instance of a class or class-constrained type}} |
| 64 | +} |
| 65 | + |
| 66 | +func find() -> Any { |
| 67 | + return CoffeeBar() |
| 68 | +} |
| 69 | + |
| 70 | +// protocol with associated type constraint |
| 71 | +protocol Basket { |
| 72 | + associatedtype Fruit |
| 73 | + associatedtype MiniBasket: Basket where MiniBasket.Fruit == Fruit |
| 74 | + |
| 75 | + var fruits: [Fruit] { get set } |
| 76 | + var minibaskets: [MiniBasket] { get set } |
| 77 | +} |
| 78 | + |
| 79 | +struct MyFruit : Basket { |
| 80 | + var fruits: [String] |
| 81 | + var minibaskets: [FruitforFriend] |
| 82 | +} |
| 83 | + |
| 84 | +struct FruitforFriend : Basket { |
| 85 | + var fruits: [String] |
| 86 | + var minibaskets: [FruitforFriend] |
| 87 | +} |
| 88 | + |
| 89 | +func eat(_ myfruit: inout Basket) -> Basket { |
| 90 | + myfruit.fruits.removeLast() |
| 91 | + myfruit.minibaskets.removeLast() |
| 92 | + return myfruit |
| 93 | +} |
| 94 | + |
0 commit comments