Skip to content

Commit 618cc91

Browse files
authored
Merge pull request #66774 from kavon/5.9-require-switch-consume
2 parents 9e00582 + e139ef1 commit 618cc91

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4595,7 +4595,7 @@ ERROR(unknown_case_multiple_patterns,none,
45954595
ERROR(unknown_case_must_be_last,none,
45964596
"'@unknown' can only be applied to the last case in a switch", ())
45974597

4598-
WARNING(move_only_pattern_match_not_consumed,none,
4598+
ERROR(move_only_pattern_match_not_consumed,none,
45994599
"noncopyable binding being pattern-matched must have the 'consume' operator applied", ())
46004600

46014601
WARNING(where_on_one_item, none,

test/SILGen/moveonly.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public func useNonTrivialOwnedStruct(_ s: __owned NonTrivialStruct) {
131131
public func useNonTrivialEnum(_ s: borrowing NonTrivialEnum) {
132132
borrowVal(s)
133133
let s2 = s
134-
switch s {
134+
switch consume s {
135135
case _:
136136
break
137137
}
@@ -149,7 +149,7 @@ public func useNonTrivialEnum(_ s: borrowing NonTrivialEnum) {
149149
public func useNonTrivialOwnedEnum(_ s: __owned NonTrivialEnum) {
150150
borrowVal(s)
151151
let s2 = s
152-
switch s {
152+
switch consume s {
153153
case _:
154154
break
155155
}
@@ -705,7 +705,13 @@ var booleanGuard2: Bool { false }
705705
// CHECK-LABEL: sil hidden [ossa] @$s8moveonly15enumSwitchTest1yyAA04EnumC5TestsO1EOF : $@convention(thin) (@guaranteed EnumSwitchTests.E) -> () {
706706
// CHECK: bb0([[ARG:%.*]] : @guaranteed
707707
// CHECK: [[COPY_ARG:%.*]] = copy_value [[ARG]]
708-
// CHECK: [[MARKED_VALUE:%.*]] = mark_must_check [no_consume_or_assign] [[COPY_ARG]]
708+
// CHECK: [[ARG_MARKED_VALUE:%.*]] = mark_must_check [no_consume_or_assign] [[COPY_ARG]]
709+
// -- code corresponding to the consume x --
710+
// CHECK: [[BORROW_ARG_MARKED_VALUE:%.*]] = begin_borrow [[ARG_MARKED_VALUE]]
711+
// CHECK: [[COPY_COPY_ARG:%.*]] = copy_value [[BORROW_ARG_MARKED_VALUE]]
712+
// CHECK: [[MOVE_COPY_COPY_ARG:%.*]] = move_value [allows_diagnostics] [[COPY_COPY_ARG]]
713+
// CHECK: [[MARKED_VALUE:%.*]] = mark_must_check [consumable_and_assignable] [[MOVE_COPY_COPY_ARG]]
714+
// -- now switching on the `consume x` --
709715
// CHECK: [[BORROWED_VALUE:%.*]] = begin_borrow [[MARKED_VALUE]]
710716
// CHECK: switch_enum [[BORROWED_VALUE]] : $EnumSwitchTests.E, case #EnumSwitchTests.E.first!enumelt: [[BB_E_1:bb[0-9]+]], case #EnumSwitchTests.E.second!enumelt: [[BB_E_2:bb[0-9]+]], case #EnumSwitchTests.E.third!enumelt: [[BB_E_3:bb[0-9]+]], case #EnumSwitchTests.E.fourth!enumelt: [[BB_E_4:bb[0-9]+]]
711717
//
@@ -758,10 +764,10 @@ var booleanGuard2: Bool { false }
758764
// CHECK: br [[BB_CONT]]
759765
//
760766
// CHECK: [[BB_CONT]]:
761-
// CHECK: destroy_value [[MARKED_VALUE]]
767+
// CHECK: destroy_value [[ARG_MARKED_VALUE]]
762768
// CHECK: } // end sil function '$s8moveonly15enumSwitchTest1yyAA04EnumC5TestsO1EOF'
763769
func enumSwitchTest1(_ e: borrowing EnumSwitchTests.E) {
764-
switch e {
770+
switch consume e {
765771
case .first:
766772
break
767773
case .second(let x):

test/Sema/moveonly_enum.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum Foo3 {
2222
}
2323

2424
func test_switch(x: consuming Foo3) {
25-
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
25+
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
2626
default:
2727
break
2828
}
@@ -32,7 +32,7 @@ func test_switch(x: consuming Foo3) {
3232
break
3333
}
3434

35-
switch (x) { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
35+
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
3636
default:
3737
break
3838
}
@@ -43,7 +43,7 @@ func test_switch(x: consuming Foo3) {
4343
}
4444

4545
let _: () -> () = {
46-
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
46+
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
4747
default:
4848
break
4949
}
@@ -57,7 +57,7 @@ func test_switch(x: consuming Foo3) {
5757
}
5858

5959
let _: () -> () = {
60-
switch (x) { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
60+
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
6161
default:
6262
break
6363
}
@@ -72,19 +72,19 @@ func test_switch(x: consuming Foo3) {
7272
}
7373

7474
func test_if_case(x: consuming Foo3) {
75-
if case .bar(let y) = x { _ = y } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
75+
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
7676

77-
guard case .bar(let y) = x else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
77+
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
7878
_ = y
7979

8080
if case .bar(let z) = consume x { _ = z }
8181

8282
guard case .bar(let z) = consume x else { return }
8383
_ = z
8484

85-
if case .bar(let a) = (x) { _ = a } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
85+
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
8686

87-
guard case .bar(let a) = (x) else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
87+
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
8888
_ = a
8989

9090
if case .bar(let b) = (consume x) { _ = b }
@@ -93,11 +93,11 @@ func test_if_case(x: consuming Foo3) {
9393
_ = b
9494

9595
let _: () -> () = {
96-
if case .bar(let y) = x { _ = y } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
96+
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
9797
}
9898

9999
let _: () -> () = {
100-
guard case .bar(let y) = x else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
100+
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
101101
_ = y
102102
}
103103

@@ -111,11 +111,11 @@ func test_if_case(x: consuming Foo3) {
111111
}
112112

113113
let _: () -> () = {
114-
if case .bar(let a) = (x) { _ = a } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
114+
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
115115
}
116116

117117
let _: () -> () = {
118-
guard case .bar(let a) = (x) else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
118+
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
119119
_ = a
120120
}
121121

@@ -130,7 +130,7 @@ func test_if_case(x: consuming Foo3) {
130130
}
131131

132132
func test_switch_b(x: __owned Foo3) {
133-
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
133+
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
134134
default:
135135
break
136136
}
@@ -140,7 +140,7 @@ func test_switch_b(x: __owned Foo3) {
140140
break
141141
}
142142

143-
switch (x) { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
143+
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{13-13=consume }}
144144
default:
145145
break
146146
}
@@ -151,7 +151,7 @@ func test_switch_b(x: __owned Foo3) {
151151
}
152152

153153
let _: () -> () = {
154-
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
154+
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{16-16=consume }}
155155
default:
156156
break
157157
}
@@ -165,7 +165,7 @@ func test_switch_b(x: __owned Foo3) {
165165
}
166166

167167
let _: () -> () = {
168-
switch (x) { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
168+
switch (x) { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{17-17=consume }}
169169
default:
170170
break
171171
}
@@ -180,19 +180,19 @@ func test_switch_b(x: __owned Foo3) {
180180
}
181181

182182
func test_if_case_b(x: __owned Foo3) {
183-
if case .bar(let y) = x { _ = y } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
183+
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{27-27=consume }}
184184

185-
guard case .bar(let y) = x else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
185+
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{30-30=consume }}
186186
_ = y
187187

188188
if case .bar(let z) = consume x { _ = z }
189189

190190
guard case .bar(let z) = consume x else { return }
191191
_ = z
192192

193-
if case .bar(let a) = (x) { _ = a } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
193+
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{28-28=consume }}
194194

195-
guard case .bar(let a) = (x) else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
195+
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
196196
_ = a
197197

198198
if case .bar(let b) = (consume x) { _ = b }
@@ -201,11 +201,11 @@ func test_if_case_b(x: __owned Foo3) {
201201
_ = b
202202

203203
let _: () -> () = {
204-
if case .bar(let y) = x { _ = y } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
204+
if case .bar(let y) = x { _ = y } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{31-31=consume }}
205205
}
206206

207207
let _: () -> () = {
208-
guard case .bar(let y) = x else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
208+
guard case .bar(let y) = x else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{34-34=consume }}
209209
_ = y
210210
}
211211

@@ -219,11 +219,11 @@ func test_if_case_b(x: __owned Foo3) {
219219
}
220220

221221
let _: () -> () = {
222-
if case .bar(let a) = (x) { _ = a } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
222+
if case .bar(let a) = (x) { _ = a } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{32-32=consume }}
223223
}
224224

225225
let _: () -> () = {
226-
guard case .bar(let a) = (x) else { return } // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
226+
guard case .bar(let a) = (x) else { return } // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{35-35=consume }}
227227
_ = a
228228
}
229229

0 commit comments

Comments
 (0)