Skip to content

Commit 3a07d3d

Browse files
committed
On nightly with NLL, suggest #![feature(bind_by_move_pattern_guards)] when it might fix the code.
1 parent c50884c commit 3a07d3d

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

src/librustc_mir/hair/pattern/check_match.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,14 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
538538
.span_label(p.span, "binds an already bound by-move value by moving it")
539539
.emit();
540540
} else if has_guard && !cx.tcx.allow_bind_by_move_patterns_with_guards() {
541-
struct_span_err!(cx.tcx.sess, p.span, E0008,
542-
"cannot bind by-move into a pattern guard")
543-
.span_label(p.span, "moves value into pattern guard")
544-
.emit();
541+
let mut err = struct_span_err!(cx.tcx.sess, p.span, E0008,
542+
"cannot bind by-move into a pattern guard");
543+
err.span_label(p.span, "moves value into pattern guard");
544+
if cx.tcx.sess.opts.unstable_features.is_nightly_build() && cx.tcx.use_mir_borrowck() {
545+
err.help("add #![feature(bind_by_move_pattern_guards)] to the \
546+
crate attributes to enable");
547+
}
548+
err.emit();
545549
} else if let Some(by_ref_span) = by_ref_span {
546550
struct_span_err!(
547551
cx.tcx.sess,
@@ -613,10 +617,16 @@ impl<'a, 'tcx> Delegate<'tcx> for MutationChecker<'a, 'tcx> {
613617
_: LoanCause) {
614618
match kind {
615619
ty::MutBorrow => {
616-
struct_span_err!(self.cx.tcx.sess, span, E0301,
617-
"cannot mutably borrow in a pattern guard")
618-
.span_label(span, "borrowed mutably in pattern guard")
619-
.emit();
620+
let mut err = struct_span_err!(self.cx.tcx.sess, span, E0301,
621+
"cannot mutably borrow in a pattern guard");
622+
err.span_label(span, "borrowed mutably in pattern guard");
623+
if self.cx.tcx.sess.opts.unstable_features.is_nightly_build() &&
624+
self.cx.tcx.use_mir_borrowck()
625+
{
626+
err.help("add #![feature(bind_by_move_pattern_guards)] to the \
627+
crate attributes to enable");
628+
}
629+
err.emit();
620630
}
621631
ty::ImmBorrow | ty::UniqueImmBorrow => {}
622632
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0008]: cannot bind by-move into a pattern guard
2+
--> $DIR/bind-by-move-no-guards.rs:8:14
3+
|
4+
LL | Some(z) if z.recv().unwrap() => { panic!() },
5+
| ^ moves value into pattern guard
6+
|
7+
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0008`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0302]: cannot assign in a pattern guard
2+
--> $DIR/borrowck-mutate-in-guard.rs:20:25
3+
|
4+
LL | Enum::A(_) if { x = Enum::B(false); false } => 1,
5+
| ^^^^^^^^^^^^^^^^^^ assignment in pattern guard
6+
7+
error[E0301]: cannot mutably borrow in a pattern guard
8+
--> $DIR/borrowck-mutate-in-guard.rs:22:38
9+
|
10+
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
11+
| ^ borrowed mutably in pattern guard
12+
|
13+
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable
14+
15+
error[E0302]: cannot assign in a pattern guard
16+
--> $DIR/borrowck-mutate-in-guard.rs:22:41
17+
|
18+
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
19+
| ^^^^^^^^^^^^^^^^^^^ assignment in pattern guard
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors occurred: E0301, E0302.
24+
For more information about an error, try `rustc --explain E0301`.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0008]: cannot bind by-move into a pattern guard
2+
--> $DIR/E0008.rs:13:14
3+
|
4+
LL | Some(s) if s.len() == 0 => {},
5+
| ^ moves value into pattern guard
6+
|
7+
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0008`.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0301]: cannot mutably borrow in a pattern guard
2+
--> $DIR/E0301.rs:14:19
3+
|
4+
LL | option if option.take().is_none() => {}, //~ ERROR E0301
5+
| ^^^^^^ borrowed mutably in pattern guard
6+
|
7+
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0301`.

0 commit comments

Comments
 (0)