|
| 1 | +// RUN: %target-swift-frontend %s -emit-sil -verify |
| 2 | + |
| 3 | +// MARK: Relocated Test Cases |
| 4 | +// Missing return diagnostics used to also be implemented during parsing/AST |
| 5 | +// construction in addition to the SIL passes. Some existing test cases have |
| 6 | +// been moved here after removing the earlier phases' diagnostics in favor of |
| 7 | +// those implemented via the SIL passes. |
| 8 | + |
| 9 | +// MARK: `decl/subscript/subscripting` |
| 10 | + |
| 11 | +struct MissingGetterSubscript1 { |
| 12 | + subscript (i : Int) -> Int { |
| 13 | + } // expected-error {{missing return in getter expected to return 'Int'}} |
| 14 | +} |
| 15 | + |
| 16 | +// MARK: `decl/var/properties` |
| 17 | + |
| 18 | +struct X {} |
| 19 | + |
| 20 | +var x13: X {} // expected-error {{missing return in getter expected to return 'X'}} |
| 21 | + |
| 22 | +struct X14 {} |
| 23 | +extension X14 { |
| 24 | + var x14: X { |
| 25 | + } // expected-error {{missing return in getter expected to return 'X'}} |
| 26 | +} |
| 27 | + |
| 28 | +// https://github.com/apple/swift/issues/57936 |
| 29 | + |
| 30 | +enum E1_57936 { |
| 31 | + var foo: Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 32 | +} |
| 33 | + |
| 34 | +enum E2_57936<T> { |
| 35 | + var foo: T {} // expected-error{{missing return in getter expected to return 'T'}} |
| 36 | +} |
| 37 | + |
| 38 | +// MARK: `decl/var/result_builders` |
| 39 | + |
| 40 | +@resultBuilder |
| 41 | +struct Maker { |
| 42 | + static func buildBlock() -> Int { 42 } |
| 43 | +} |
| 44 | + |
| 45 | +@Maker |
| 46 | +var globalWithEmptyImplicitGetter: Int {} |
| 47 | + |
| 48 | +// MARK: `Parse/omit_return` |
| 49 | + |
| 50 | +var fv_nop: () { |
| 51 | +} |
| 52 | + |
| 53 | +var fv_missing: String { |
| 54 | +} // expected-error {{missing return in getter expected to return 'String'}} |
| 55 | + |
| 56 | +enum S_nop { |
| 57 | + subscript() -> () { |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +enum S_missing { |
| 62 | + subscript() -> String { |
| 63 | + } // expected-error {{missing return in getter expected to return 'String'}} |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: `Sema/generic-subscript` |
| 67 | + |
| 68 | +struct S_generic_subscript_missing_return { |
| 69 | + subscript<Value>(x: Int) -> Value { |
| 70 | + } // expected-error {{missing return in getter expected to return 'Value'}} |
| 71 | +} |
| 72 | + |
| 73 | +// MARK: New Test Cases |
| 74 | + |
| 75 | +enum MyEmptyType {} |
| 76 | +extension MyEmptyType { |
| 77 | + var i: Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 78 | + var n: MyEmptyType {} // expected-error{{getter with uninhabited return type 'MyEmptyType' is missing call to another never-returning function on all paths}} |
| 79 | + |
| 80 | + static subscript<A>(root: MyEmptyType) -> A {} |
| 81 | + |
| 82 | + subscript(_ e: MyEmptyType) -> Int {} |
| 83 | + subscript<T>(_ e: MyEmptyType) -> T {} |
| 84 | + subscript(_ i: Int) -> Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 85 | + subscript<T>(_ p: Int) -> T {} // expected-error{{missing return in getter expected to return 'T'}} |
| 86 | + subscript(_ i: Int) -> Self {} // expected-error{{getter with uninhabited return type 'MyEmptyType' is missing call to another never-returning function on all paths}} |
| 87 | + subscript(_ s: Self) -> Self {} |
| 88 | + |
| 89 | + static func unreachable_static_implicit_return(_ e: MyEmptyType) -> Int {} |
| 90 | + func unreachable(_ e: MyEmptyType) -> Int { // expected-note{{'e' is of type 'MyEmptyType' which cannot be constructed because it is an enum with no cases}} |
| 91 | + 42 // expected-warning{{will never be executed}} |
| 92 | + } |
| 93 | + |
| 94 | + // FIXME: should these produce warnings since they implicity take an uninhabited 'self' param? |
| 95 | + func implicitly_unreachable() { _ = 42 } |
| 96 | + func implicitly_unreachable_implicit_return() -> Int { 42 } |
| 97 | +} |
| 98 | + |
| 99 | +extension Never { |
| 100 | + var i: Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 101 | + var n: Never {} // expected-error{{getter with uninhabited return type 'Never' is missing call to another never-returning function on all paths}} |
| 102 | + |
| 103 | + static subscript<A>(root: Never) -> A {} |
| 104 | + |
| 105 | + subscript(_ n: Never) -> Int {} |
| 106 | + subscript<T>(_ e: Never) -> T {} |
| 107 | + subscript(_ i: Int) -> Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 108 | + subscript<T>(_ p: Int) -> T {} // expected-error{{missing return in getter expected to return 'T'}} |
| 109 | + subscript(_ i: Int) -> Self {} // expected-error{{getter with uninhabited return type 'Never' is missing call to another never-returning function on all paths}} |
| 110 | + subscript(_ s: Self) -> Self {} |
| 111 | + |
| 112 | + static func unreachable_static_implicit_return(_ n: Never) -> Int {} |
| 113 | + func unreachable(_ n: Never) -> Int { // expected-note{{'n' is of type 'Never' which cannot be constructed because it is an enum with no cases}} |
| 114 | + 42 // expected-warning{{will never be executed}} |
| 115 | + } |
| 116 | + |
| 117 | + // FIXME: should these produce unreachable code warnings since they implicity take an uninhabited 'self' param? |
| 118 | + func implicitly_unreachable() { _ = 42 } |
| 119 | + func implicitly_unreachable_implicit_return() -> Int { 42 } |
| 120 | +} |
| 121 | + |
| 122 | +enum InhabitedType { |
| 123 | + case inhabitant |
| 124 | + |
| 125 | + // Uninhabited params |
| 126 | + subscript(_ n: Never) -> Int {} |
| 127 | + subscript<T>(_ e: Never) -> T {} |
| 128 | + subscript(_ v: MyEmptyType, e: Int) -> Never {} |
| 129 | + |
| 130 | + // Inhabited params |
| 131 | + subscript(_ i: Int) -> Int {} // expected-error{{missing return in getter expected to return 'Int'}} |
| 132 | + subscript(_ j: Int) -> Void {} |
| 133 | + subscript(_ k: Int) -> Never {} // expected-error{{getter with uninhabited return type 'Never' is missing call to another never-returning function on all paths}} |
| 134 | + // FIXME: ^ this diagnostic should probably use the word 'subscript' rather than 'getter' |
| 135 | +} |
0 commit comments