|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %gyb %s -o %t/generated.swift |
| 3 | +// RUN: %target-swift-frontend -typecheck -verify -dump-ast %t/generated.swift | %FileCheck %t/generated.swift |
| 4 | + |
| 5 | +// Test that optional promotion works for refutable patterns when matching |
| 6 | +// patterns. In 'switch' statements, additionally check for AST pattern |
| 7 | +// equivalence to show that an implicitly promoted pattern will have the same |
| 8 | +// space projection as the explicit counterpart. |
| 9 | +// |
| 10 | +// Note: We do not test 'catch' statements because patterns there are always |
| 11 | +// matched against a non-optional. |
| 12 | + |
| 13 | +%{ |
| 14 | + modes = ['regular'] |
| 15 | + def get_test_case_introducer(mode): |
| 16 | + if mode == 'regular': |
| 17 | + return 'do' |
| 18 | + |
| 19 | + assert False, 'unhandled mode!' |
| 20 | +}% |
| 21 | + |
| 22 | +enum E { |
| 23 | + case a |
| 24 | +} |
| 25 | + |
| 26 | +// Enum element patterns. |
| 27 | +do { |
| 28 | + var oe: E? |
| 29 | + var ooe: E?? |
| 30 | + // expected-warning@-2 {{variable 'oe' was never mutated; consider changing to 'let' constant}} |
| 31 | + // expected-warning@-2 {{variable 'ooe' was never mutated; consider changing to 'let' constant}} |
| 32 | + |
| 33 | +% for mode in modes: |
| 34 | +% test_case_introducer = get_test_case_introducer(mode) |
| 35 | + do { |
| 36 | + ${test_case_introducer} { |
| 37 | + if case .a = oe {} |
| 38 | + if case .a = ooe {} |
| 39 | + if case .a? = ooe {} |
| 40 | + if case .some(.a) = ooe {} |
| 41 | + |
| 42 | +% if mode == 'regular': |
| 43 | + guard case .a = oe else {} |
| 44 | + guard case .a = ooe else {} |
| 45 | + guard case .a? = ooe else {} |
| 46 | + guard case .some(.a) = ooe else {} |
| 47 | + |
| 48 | + while case .a = oe {} |
| 49 | + while case .a = ooe {} |
| 50 | + while case .a? = ooe {} |
| 51 | + while case .some(.a) = ooe {} |
| 52 | +% end |
| 53 | + |
| 54 | + for case .a in [oe] {} |
| 55 | + for case .a in [ooe] {} |
| 56 | + for case .a? in [ooe] {} |
| 57 | + for case .some(.a) in [ooe] {} |
| 58 | + |
| 59 | + switch oe { |
| 60 | + case nil: () |
| 61 | + case .a: () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 62 | + // CHECK: (case_label_item |
| 63 | + // CHECK-NEXT: (pattern_optional_some implicit type='E?' |
| 64 | + // CHECK-NEXT: (pattern_enum_element type='E' E.a |
| 65 | + } |
| 66 | + switch ooe { |
| 67 | + case nil, .some(nil): () |
| 68 | + case .a: () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 69 | + // CHECK: (case_label_item |
| 70 | + // CHECK-NEXT: (pattern_optional_some implicit type='E??' |
| 71 | + // CHECK-NEXT: (pattern_optional_some implicit type='E?' |
| 72 | + // CHECK-NEXT: (pattern_enum_element type='E' E.a |
| 73 | + } |
| 74 | + switch ooe { |
| 75 | + case nil, .some(nil): () |
| 76 | + case .a?: () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 77 | + // CHECK: (case_label_item |
| 78 | + // CHECK-NEXT: (pattern_optional_some type='E??' |
| 79 | + // CHECK-NEXT: (pattern_optional_some implicit type='E?' |
| 80 | + // CHECK-NEXT: (pattern_enum_element type='E' E.a |
| 81 | + } |
| 82 | + switch ooe { |
| 83 | + case nil, .some(nil): () |
| 84 | + case .some(.a): () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 85 | + // CHECK: (case_label_item |
| 86 | + // CHECK-NEXT: (pattern_enum_element type='E??' E??.some |
| 87 | + // CHECK-NEXT: (pattern_paren type='(E?)' |
| 88 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E?)' |
| 89 | + // CHECK-NEXT: (pattern_enum_element type='E' E.a |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + do { |
| 95 | + ${test_case_introducer} { |
| 96 | + switch oe { |
| 97 | + case nil: () |
| 98 | + // FIXME: '.a' is OK but 'E.a' is not? |
| 99 | + case E.a: () // expected-error {{enum case 'a' is not a member of type 'E?'}} |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +% end |
| 105 | +} |
| 106 | + |
| 107 | +// Tuple patterns. |
| 108 | +do { |
| 109 | + var ot: (E, E)? |
| 110 | + var oot: (E, E)?? |
| 111 | + var tot: ((E, E)?, E) |
| 112 | + |
| 113 | +% for mode in modes: |
| 114 | +% test_case_introducer = get_test_case_introducer(mode) |
| 115 | + do { |
| 116 | + ${test_case_introducer} { |
| 117 | + // FIXME: Works in a 'switch' but not an 'if'. |
| 118 | + if case (.a, .a) = ot {} |
| 119 | + // expected-error@-1 {{value of optional type '(E, E)?' must be unwrapped to a value of type '(E, E)'}} |
| 120 | + // expected-note@-2 {{coalesce}} |
| 121 | + // expected-note@-3 {{force-unwrap}} |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + do { |
| 126 | + ${test_case_introducer} { |
| 127 | + switch ot { |
| 128 | + case nil: () |
| 129 | + case (.a, .a): () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 130 | + // CHECK: (case_label_item |
| 131 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)?' |
| 132 | + // CHECK-NEXT: (pattern_tuple type='(E, E)' |
| 133 | + } |
| 134 | + switch ot { |
| 135 | + case nil: () |
| 136 | + case let (x, y): () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 137 | + // CHECK: (case_label_item |
| 138 | + // CHECK-NEXT: (pattern_let type='(E, E)?' |
| 139 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)?' |
| 140 | + // CHECK-NEXT: (pattern_tuple type='(E, E)' |
| 141 | + } |
| 142 | + switch oot { |
| 143 | + case nil, .some(nil): () |
| 144 | + case (.a, .a): () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 145 | + // CHECK: (case_label_item |
| 146 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)??' |
| 147 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)?' |
| 148 | + // CHECK-NEXT: (pattern_tuple type='(E, E)' |
| 149 | + } |
| 150 | + switch oot { |
| 151 | + case nil, .some(nil): () |
| 152 | + case (.a, .a)?: () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 153 | + // CHECK: (case_label_item |
| 154 | + // CHECK-NEXT: (pattern_optional_some type='(E, E)??' |
| 155 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)?' |
| 156 | + // CHECK-NEXT: (pattern_tuple type='(E, E)' |
| 157 | + } |
| 158 | + switch tot { |
| 159 | + case (nil, .a): () |
| 160 | + case ((.a, .a), .a): () // CHECK: (case_stmt range=[{{.+}}.swift:[[@LINE]] |
| 161 | + // CHECK: (case_label_item |
| 162 | + // CHECK-NEXT: (pattern_tuple type='((E, E)?, E)' |
| 163 | + // CHECK-NEXT: (pattern_optional_some implicit type='(E, E)?' |
| 164 | + // CHECK-NEXT: (pattern_tuple type='(E, E)' |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +% end |
| 170 | +} |
| 171 | + |
| 172 | +// Boolean patterns. |
| 173 | +do { |
| 174 | + var ob: Bool? // expected-warning {{variable 'ob' was never mutated; consider changing to 'let' constant}} |
| 175 | + |
| 176 | +% for mode in modes: |
| 177 | +% test_case_introducer = get_test_case_introducer(mode) |
| 178 | + do { |
| 179 | + ${test_case_introducer} { |
| 180 | + // FIXME: Works but the literal resolves to an expression pattern. |
| 181 | + if case true = ob {} |
| 182 | + |
| 183 | + // FIXME(https://github.com/apple/swift/issues/61817): This should be exhaustive |
| 184 | + switch ob { // expected-error {{switch must be exhaustive}} |
| 185 | + // expected-note@-1 {{add missing case: '.some(_)'}} |
| 186 | + case nil: () |
| 187 | + case true: () |
| 188 | + case false: () |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +% end |
| 194 | +} |
0 commit comments