Skip to content

require consume x for noncopyable pattern bindings since they're consuming #66713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4672,7 +4672,7 @@ ERROR(unknown_case_multiple_patterns,none,
ERROR(unknown_case_must_be_last,none,
"'@unknown' can only be applied to the last case in a switch", ())

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

WARNING(where_on_one_item, none,
Expand Down
16 changes: 11 additions & 5 deletions test/SILGen/moveonly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public func useNonTrivialOwnedStruct(_ s: __owned NonTrivialStruct) {
public func useNonTrivialEnum(_ s: borrowing NonTrivialEnum) {
borrowVal(s)
let s2 = s
switch s {
switch consume s {
case _:
break
}
Expand All @@ -149,7 +149,7 @@ public func useNonTrivialEnum(_ s: borrowing NonTrivialEnum) {
public func useNonTrivialOwnedEnum(_ s: __owned NonTrivialEnum) {
borrowVal(s)
let s2 = s
switch s {
switch consume s {
case _:
break
}
Expand Down Expand Up @@ -705,7 +705,13 @@ var booleanGuard2: Bool { false }
// CHECK-LABEL: sil hidden [ossa] @$s8moveonly15enumSwitchTest1yyAA04EnumC5TestsO1EOF : $@convention(thin) (@guaranteed EnumSwitchTests.E) -> () {
// CHECK: bb0([[ARG:%.*]] : @guaranteed
// CHECK: [[COPY_ARG:%.*]] = copy_value [[ARG]]
// CHECK: [[MARKED_VALUE:%.*]] = mark_must_check [no_consume_or_assign] [[COPY_ARG]]
// CHECK: [[ARG_MARKED_VALUE:%.*]] = mark_must_check [no_consume_or_assign] [[COPY_ARG]]
// -- code corresponding to the consume x --
// CHECK: [[BORROW_ARG_MARKED_VALUE:%.*]] = begin_borrow [[ARG_MARKED_VALUE]]
// CHECK: [[COPY_COPY_ARG:%.*]] = copy_value [[BORROW_ARG_MARKED_VALUE]]
// CHECK: [[MOVE_COPY_COPY_ARG:%.*]] = move_value [allows_diagnostics] [[COPY_COPY_ARG]]
// CHECK: [[MARKED_VALUE:%.*]] = mark_must_check [consumable_and_assignable] [[MOVE_COPY_COPY_ARG]]
// -- now switching on the `consume x` --
// CHECK: [[BORROWED_VALUE:%.*]] = begin_borrow [[MARKED_VALUE]]
// 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]+]]
//
Expand Down Expand Up @@ -758,10 +764,10 @@ var booleanGuard2: Bool { false }
// CHECK: br [[BB_CONT]]
//
// CHECK: [[BB_CONT]]:
// CHECK: destroy_value [[MARKED_VALUE]]
// CHECK: destroy_value [[ARG_MARKED_VALUE]]
// CHECK: } // end sil function '$s8moveonly15enumSwitchTest1yyAA04EnumC5TestsO1EOF'
func enumSwitchTest1(_ e: borrowing EnumSwitchTests.E) {
switch e {
switch consume e {
case .first:
break
case .second(let x):
Expand Down
48 changes: 24 additions & 24 deletions test/Sema/moveonly_enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum Foo3 {
}

func test_switch(x: consuming Foo3) {
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
default:
break
}
Expand All @@ -32,7 +32,7 @@ func test_switch(x: consuming Foo3) {
break
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func test_switch_b(x: __owned Foo3) {
switch x { // expected-warning{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
switch x { // expected-error{{noncopyable binding being pattern-matched must have the 'consume' operator applied}} {{12-12=consume }}
default:
break
}
Expand All @@ -140,7 +140,7 @@ func test_switch_b(x: __owned Foo3) {
break
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down