Skip to content

Commit

Permalink
On nightly with NLL, suggest `#![feature(bind_by_move_pattern_guards)…
Browse files Browse the repository at this point in the history
…]` when it might fix the code.
  • Loading branch information
pnkfelix committed Sep 17, 2018
1 parent c50884c commit 3a07d3d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/librustc_mir/hair/pattern/check_match.rs
Expand Up @@ -538,10 +538,14 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
.span_label(p.span, "binds an already bound by-move value by moving it")
.emit();
} else if has_guard && !cx.tcx.allow_bind_by_move_patterns_with_guards() {
struct_span_err!(cx.tcx.sess, p.span, E0008,
"cannot bind by-move into a pattern guard")
.span_label(p.span, "moves value into pattern guard")
.emit();
let mut err = struct_span_err!(cx.tcx.sess, p.span, E0008,
"cannot bind by-move into a pattern guard");
err.span_label(p.span, "moves value into pattern guard");
if cx.tcx.sess.opts.unstable_features.is_nightly_build() && cx.tcx.use_mir_borrowck() {
err.help("add #![feature(bind_by_move_pattern_guards)] to the \
crate attributes to enable");
}
err.emit();
} else if let Some(by_ref_span) = by_ref_span {
struct_span_err!(
cx.tcx.sess,
Expand Down Expand Up @@ -613,10 +617,16 @@ impl<'a, 'tcx> Delegate<'tcx> for MutationChecker<'a, 'tcx> {
_: LoanCause) {
match kind {
ty::MutBorrow => {
struct_span_err!(self.cx.tcx.sess, span, E0301,
"cannot mutably borrow in a pattern guard")
.span_label(span, "borrowed mutably in pattern guard")
.emit();
let mut err = struct_span_err!(self.cx.tcx.sess, span, E0301,
"cannot mutably borrow in a pattern guard");
err.span_label(span, "borrowed mutably in pattern guard");
if self.cx.tcx.sess.opts.unstable_features.is_nightly_build() &&
self.cx.tcx.use_mir_borrowck()
{
err.help("add #![feature(bind_by_move_pattern_guards)] to the \
crate attributes to enable");
}
err.emit();
}
ty::ImmBorrow | ty::UniqueImmBorrow => {}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/bind-by-move/bind-by-move-no-guards.nll.stderr
@@ -0,0 +1,11 @@
error[E0008]: cannot bind by-move into a pattern guard
--> $DIR/bind-by-move-no-guards.rs:8:14
|
LL | Some(z) if z.recv().unwrap() => { panic!() },
| ^ moves value into pattern guard
|
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0008`.
24 changes: 24 additions & 0 deletions src/test/ui/borrowck/borrowck-mutate-in-guard.nll.stderr
@@ -0,0 +1,24 @@
error[E0302]: cannot assign in a pattern guard
--> $DIR/borrowck-mutate-in-guard.rs:20:25
|
LL | Enum::A(_) if { x = Enum::B(false); false } => 1,
| ^^^^^^^^^^^^^^^^^^ assignment in pattern guard

error[E0301]: cannot mutably borrow in a pattern guard
--> $DIR/borrowck-mutate-in-guard.rs:22:38
|
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
| ^ borrowed mutably in pattern guard
|
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable

error[E0302]: cannot assign in a pattern guard
--> $DIR/borrowck-mutate-in-guard.rs:22:41
|
LL | Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
| ^^^^^^^^^^^^^^^^^^^ assignment in pattern guard

error: aborting due to 3 previous errors

Some errors occurred: E0301, E0302.
For more information about an error, try `rustc --explain E0301`.
11 changes: 11 additions & 0 deletions src/test/ui/error-codes/E0008.nll.stderr
@@ -0,0 +1,11 @@
error[E0008]: cannot bind by-move into a pattern guard
--> $DIR/E0008.rs:13:14
|
LL | Some(s) if s.len() == 0 => {},
| ^ moves value into pattern guard
|
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0008`.
11 changes: 11 additions & 0 deletions src/test/ui/error-codes/E0301.nll.stderr
@@ -0,0 +1,11 @@
error[E0301]: cannot mutably borrow in a pattern guard
--> $DIR/E0301.rs:14:19
|
LL | option if option.take().is_none() => {}, //~ ERROR E0301
| ^^^^^^ borrowed mutably in pattern guard
|
= help: add #![feature(bind_by_move_pattern_guards)] to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0301`.

0 comments on commit 3a07d3d

Please sign in to comment.