Skip to content

Commit da700cb

Browse files
committed
Stabilize move_ref_pattern
1 parent 715e934 commit da700cb

File tree

49 files changed

+271
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+271
-492
lines changed

compiler/rustc_feature/src/accepted.rs

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ declare_features! (
270270
(accepted, track_caller, "1.46.0", Some(47809), None),
271271
/// Allows `#[doc(alias = "...")]`.
272272
(accepted, doc_alias, "1.48.0", Some(50146), None),
273+
/// Allows patterns with concurrent by-move and by-ref bindings.
274+
/// For example, you can write `Foo(a, ref b)` where `a` is by-move and `b` is by-ref.
275+
(accepted, move_ref_pattern, "1.48.0", Some(68354), None),
273276

274277
// -------------------------------------------------------------------------
275278
// feature-group-end: accepted features

compiler/rustc_feature/src/active.rs

-4
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,6 @@ declare_features! (
527527
/// For example, you can write `x @ Some(y)`.
528528
(active, bindings_after_at, "1.41.0", Some(65490), None),
529529

530-
/// Allows patterns with concurrent by-move and by-ref bindings.
531-
/// For example, you can write `Foo(a, ref b)` where `a` is by-move and `b` is by-ref.
532-
(active, move_ref_pattern, "1.42.0", Some(68354), None),
533-
534530
/// Allows `impl const Trait for T` syntax.
535531
(active, const_trait_impl, "1.42.0", Some(67792), None),
536532

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+4-66
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> {
7171
hir::LocalSource::AwaitDesugar => ("`await` future binding", None),
7272
};
7373
self.check_irrefutable(&loc.pat, msg, sp);
74-
self.check_patterns(false, &loc.pat);
74+
self.check_patterns(&loc.pat);
7575
}
7676

7777
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
7878
intravisit::walk_param(self, param);
7979
self.check_irrefutable(&param.pat, "function argument", None);
80-
self.check_patterns(false, &param.pat);
80+
self.check_patterns(&param.pat);
8181
}
8282
}
8383

@@ -119,10 +119,7 @@ impl PatCtxt<'_, '_> {
119119
}
120120

121121
impl<'tcx> MatchVisitor<'_, 'tcx> {
122-
fn check_patterns(&mut self, has_guard: bool, pat: &Pat<'_>) {
123-
if !self.tcx.features().move_ref_pattern {
124-
check_legality_of_move_bindings(self, has_guard, pat);
125-
}
122+
fn check_patterns(&mut self, pat: &Pat<'_>) {
126123
pat.walk_always(|pat| check_borrow_conflicts_in_at_patterns(self, pat));
127124
if !self.tcx.features().bindings_after_at {
128125
check_legality_of_bindings_in_at_patterns(self, pat);
@@ -165,7 +162,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
165162
) {
166163
for arm in arms {
167164
// Check the arm for some things unrelated to exhaustiveness.
168-
self.check_patterns(arm.guard.is_some(), &arm.pat);
165+
self.check_patterns(&arm.pat);
169166
}
170167

171168
let mut cx = self.new_cx(scrut.hir_id);
@@ -601,65 +598,6 @@ fn is_binding_by_move(cx: &MatchVisitor<'_, '_>, hir_id: HirId, span: Span) -> b
601598
!cx.typeck_results.node_type(hir_id).is_copy_modulo_regions(cx.tcx.at(span), cx.param_env)
602599
}
603600

604-
/// Check the legality of legality of by-move bindings.
605-
fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: bool, pat: &Pat<'_>) {
606-
let sess = cx.tcx.sess;
607-
let typeck_results = cx.typeck_results;
608-
609-
// Find all by-ref spans.
610-
let mut by_ref_spans = Vec::new();
611-
pat.each_binding(|_, hir_id, span, _| {
612-
if let Some(ty::BindByReference(_)) =
613-
typeck_results.extract_binding_mode(sess, hir_id, span)
614-
{
615-
by_ref_spans.push(span);
616-
}
617-
});
618-
619-
// Find bad by-move spans:
620-
let by_move_spans = &mut Vec::new();
621-
let mut check_move = |p: &Pat<'_>, sub: Option<&Pat<'_>>| {
622-
// Check legality of moving out of the enum.
623-
//
624-
// `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
625-
if sub.map_or(false, |p| p.contains_bindings()) {
626-
struct_span_err!(sess, p.span, E0007, "cannot bind by-move with sub-bindings")
627-
.span_label(p.span, "binds an already bound by-move value by moving it")
628-
.emit();
629-
} else if !has_guard && !by_ref_spans.is_empty() {
630-
by_move_spans.push(p.span);
631-
}
632-
};
633-
pat.walk_always(|p| {
634-
if let hir::PatKind::Binding(.., sub) = &p.kind {
635-
if let Some(ty::BindByValue(_)) =
636-
typeck_results.extract_binding_mode(sess, p.hir_id, p.span)
637-
{
638-
if is_binding_by_move(cx, p.hir_id, p.span) {
639-
check_move(p, sub.as_deref());
640-
}
641-
}
642-
}
643-
});
644-
645-
// Found some bad by-move spans, error!
646-
if !by_move_spans.is_empty() {
647-
let mut err = feature_err(
648-
&sess.parse_sess,
649-
sym::move_ref_pattern,
650-
by_move_spans.clone(),
651-
"binding by-move and by-ref in the same pattern is unstable",
652-
);
653-
for span in by_ref_spans.iter() {
654-
err.span_label(*span, "by-ref pattern here");
655-
}
656-
for span in by_move_spans.iter() {
657-
err.span_label(*span, "by-move pattern here");
658-
}
659-
err.emit();
660-
}
661-
}
662-
663601
/// Check that there are no borrow or move conflicts in `binding @ subpat` patterns.
664602
///
665603
/// For example, this would reject:

src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#![feature(or_patterns)]
44
#![feature(box_patterns)]
55

6-
#![feature(move_ref_pattern)]
7-
86
enum Test {
97
Foo,
108
Bar,

src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: cannot borrow value as mutable because it is also borrowed as immutable
2-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:40:9
2+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:38:9
33
|
44
LL | ref foo @ [.., ref mut bar] => (),
55
| -------^^^^^^^^-----------^
@@ -8,7 +8,7 @@ LL | ref foo @ [.., ref mut bar] => (),
88
| immutable borrow, by `foo`, occurs here
99

1010
error: cannot borrow value as mutable because it is also borrowed as immutable
11-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:124:9
11+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:122:9
1212
|
1313
LL | ref foo @ Some(box ref mut s) => (),
1414
| -------^^^^^^^^^^^^---------^
@@ -17,7 +17,7 @@ LL | ref foo @ Some(box ref mut s) => (),
1717
| immutable borrow, by `foo`, occurs here
1818

1919
error[E0382]: borrow of moved value: `x`
20-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:22:5
20+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:20:5
2121
|
2222
LL | fn bindings_after_at_slice_patterns_move_binding(x: [String; 4]) {
2323
| - move occurs because `x` has type `[String; 4]`, which does not implement the `Copy` trait
@@ -29,7 +29,7 @@ LL | &x;
2929
| ^^ value borrowed here after move
3030

3131
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
32-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:32:5
32+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:30:5
3333
|
3434
LL | ref mut foo @ [.., _] => Some(foo),
3535
| --------------------- mutable borrow occurs here
@@ -41,7 +41,7 @@ LL | drop(r);
4141
| - mutable borrow later used here
4242

4343
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
44-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:54:5
44+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:52:5
4545
|
4646
LL | [ref foo @ .., ref bar] => Some(foo),
4747
| ------------ immutable borrow occurs here
@@ -53,7 +53,7 @@ LL | drop(r);
5353
| - immutable borrow later used here
5454

5555
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
56-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:66:5
56+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:64:5
5757
|
5858
LL | ref foo @ [.., ref bar] => Some(foo),
5959
| ----------------------- immutable borrow occurs here
@@ -65,7 +65,7 @@ LL | drop(r);
6565
| - immutable borrow later used here
6666

6767
error[E0382]: borrow of moved value: `x`
68-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:80:5
68+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:78:5
6969
|
7070
LL | fn bindings_after_at_or_patterns_move(x: Option<Test>) {
7171
| - move occurs because `x` has type `Option<Test>`, which does not implement the `Copy` trait
@@ -80,7 +80,7 @@ LL | &x;
8080
| ^^ value borrowed here after move
8181

8282
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
83-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:90:5
83+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:88:5
8484
|
8585
LL | ref foo @ Some(Test::Foo | Test::Bar) => Some(foo),
8686
| ------------------------------------- immutable borrow occurs here
@@ -92,7 +92,7 @@ LL | drop(r);
9292
| - immutable borrow later used here
9393

9494
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
95-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:102:5
95+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:100:5
9696
|
9797
LL | ref mut foo @ Some(Test::Foo | Test::Bar) => Some(foo),
9898
| ----------------------------------------- mutable borrow occurs here
@@ -104,7 +104,7 @@ LL | drop(r);
104104
| - mutable borrow later used here
105105

106106
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
107-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:116:5
107+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:114:5
108108
|
109109
LL | ref foo @ Some(box ref s) => Some(foo),
110110
| ------------------------- immutable borrow occurs here
@@ -116,7 +116,7 @@ LL | drop(r);
116116
| - immutable borrow later used here
117117

118118
error[E0382]: borrow of moved value: `x`
119-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:138:5
119+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:136:5
120120
|
121121
LL | fn bindings_after_at_slice_patterns_or_patterns_moves(x: [Option<Test>; 4]) {
122122
| - move occurs because `x` has type `[Option<Test>; 4]`, which does not implement the `Copy` trait
@@ -131,7 +131,7 @@ LL | &x;
131131
| ^^ value borrowed here after move
132132

133133
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
134-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:148:5
134+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:146:5
135135
|
136136
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(a),
137137
| ------------------------------------------------- immutable borrow occurs here
@@ -143,7 +143,7 @@ LL | drop(r);
143143
| - immutable borrow later used here
144144

145145
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
146-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:160:5
146+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:158:5
147147
|
148148
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(b),
149149
| ---------- immutable borrow occurs here
@@ -155,7 +155,7 @@ LL | drop(r);
155155
| - immutable borrow later used here
156156

157157
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
158-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:174:5
158+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:172:5
159159
|
160160
LL | [_, ref a @ Some(box ref b), ..] => Some(a),
161161
| ----------------------- immutable borrow occurs here
@@ -167,7 +167,7 @@ LL | drop(r);
167167
| - immutable borrow later used here
168168

169169
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
170-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:190:5
170+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:188:5
171171
|
172172
LL | [_, ref a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
173173
| ------------------------------------------- immutable borrow occurs here
@@ -179,7 +179,7 @@ LL | drop(r);
179179
| - immutable borrow later used here
180180

181181
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
182-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:204:5
182+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:202:5
183183
|
184184
LL | [_, ref mut a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
185185
| ----------------------------------------------- mutable borrow occurs here
@@ -191,7 +191,7 @@ LL | drop(r);
191191
| - mutable borrow later used here
192192

193193
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
194-
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:218:5
194+
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:216:5
195195
|
196196
LL | ref a @ [_, ref b @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
197197
| ------------------------------------------------------------ immutable borrow occurs here

src/test/ui/drop/dynamic-drop-async.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// edition:2018
88
// ignore-wasm32-bare compiled with panic=abort by default
99

10-
#![feature(move_ref_pattern)]
11-
1210
#![allow(unused)]
1311

1412
use std::{

src/test/ui/drop/dynamic-drop.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// ignore-wasm32-bare compiled with panic=abort by default
33

44
#![feature(generators, generator_trait, untagged_unions)]
5-
#![feature(move_ref_pattern)]
65
#![feature(bindings_after_at)]
76

87
#![allow(unused_assignments)]

src/test/ui/error-codes/E0007.rs

-11
This file was deleted.

src/test/ui/error-codes/E0007.stderr

-22
This file was deleted.

src/test/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// where one side is by-ref and the other is by-move.
44

55
#![feature(bindings_after_at)]
6-
#![feature(move_ref_pattern)]
76

87
struct X {
98
x: (),

src/test/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: cannot move out of value because it is borrowed
2-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:15:14
2+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:14:14
33
|
44
LL | Some(ref _y @ _z) => {}
55
| ------^^^--
@@ -8,7 +8,7 @@ LL | Some(ref _y @ _z) => {}
88
| value borrowed, by `_y`, here
99

1010
error: borrow of moved value
11-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:21:14
11+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:20:14
1212
|
1313
LL | Some(_z @ ref _y) => {}
1414
| --^^^------
@@ -18,7 +18,7 @@ LL | Some(_z @ ref _y) => {}
1818
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
1919

2020
error: cannot move out of value because it is borrowed
21-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:29:14
21+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:28:14
2222
|
2323
LL | Some(ref mut _y @ _z) => {}
2424
| ----------^^^--
@@ -27,7 +27,7 @@ LL | Some(ref mut _y @ _z) => {}
2727
| value borrowed, by `_y`, here
2828

2929
error: borrow of moved value
30-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:35:14
30+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:34:14
3131
|
3232
LL | Some(_z @ ref mut _y) => {}
3333
| --^^^----------
@@ -37,7 +37,7 @@ LL | Some(_z @ ref mut _y) => {}
3737
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
3838

3939
error[E0382]: borrow of moved value
40-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:21:19
40+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:20:19
4141
|
4242
LL | Some(_z @ ref _y) => {}
4343
| -----^^^^^^
@@ -52,7 +52,7 @@ LL | Some(ref _z @ ref _y) => {}
5252
| ^^^
5353

5454
error[E0382]: borrow of moved value
55-
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:35:19
55+
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:34:19
5656
|
5757
LL | Some(_z @ ref mut _y) => {}
5858
| -----^^^^^^^^^^

0 commit comments

Comments
 (0)