From 3a07d3dbd6cdb2014369935b62b36c64d6c580a6 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 10 Sep 2018 11:47:28 +0200 Subject: [PATCH] On nightly with NLL, suggest `#![feature(bind_by_move_pattern_guards)]` when it might fix the code. --- src/librustc_mir/hair/pattern/check_match.rs | 26 +++++++++++++------ .../bind-by-move-no-guards.nll.stderr | 11 ++++++++ .../borrowck-mutate-in-guard.nll.stderr | 24 +++++++++++++++++ src/test/ui/error-codes/E0008.nll.stderr | 11 ++++++++ src/test/ui/error-codes/E0301.nll.stderr | 11 ++++++++ 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/bind-by-move/bind-by-move-no-guards.nll.stderr create mode 100644 src/test/ui/borrowck/borrowck-mutate-in-guard.nll.stderr create mode 100644 src/test/ui/error-codes/E0008.nll.stderr create mode 100644 src/test/ui/error-codes/E0301.nll.stderr diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 507019559fc30..23667d1b331a3 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -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, @@ -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 => {} } diff --git a/src/test/ui/bind-by-move/bind-by-move-no-guards.nll.stderr b/src/test/ui/bind-by-move/bind-by-move-no-guards.nll.stderr new file mode 100644 index 0000000000000..5f8b7007f304c --- /dev/null +++ b/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`. diff --git a/src/test/ui/borrowck/borrowck-mutate-in-guard.nll.stderr b/src/test/ui/borrowck/borrowck-mutate-in-guard.nll.stderr new file mode 100644 index 0000000000000..b363a78cbc206 --- /dev/null +++ b/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`. diff --git a/src/test/ui/error-codes/E0008.nll.stderr b/src/test/ui/error-codes/E0008.nll.stderr new file mode 100644 index 0000000000000..ce627cb741a37 --- /dev/null +++ b/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`. diff --git a/src/test/ui/error-codes/E0301.nll.stderr b/src/test/ui/error-codes/E0301.nll.stderr new file mode 100644 index 0000000000000..f060eb9043535 --- /dev/null +++ b/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`.