Skip to content

Commit 961a4da

Browse files
committed
Stabilize bind_by_move_pattern_guards in 1.39.0.
1 parent 43a5ff4 commit 961a4da

File tree

3 files changed

+6
-89
lines changed

3 files changed

+6
-89
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ use super::_match::WitnessPreference::*;
55
use super::{Pattern, PatternContext, PatternError, PatternKind};
66

77
use rustc::middle::borrowck::SignalledError;
8-
use rustc::middle::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor};
9-
use rustc::middle::expr_use_visitor::{LoanCause, MutateMode};
10-
use rustc::middle::expr_use_visitor as euv;
11-
use rustc::middle::mem_categorization::cmt_;
12-
use rustc::middle::region;
138
use rustc::session::Session;
149
use rustc::ty::{self, Ty, TyCtxt};
1510
use rustc::ty::subst::{InternalSubsts, SubstsRef};
@@ -36,9 +31,7 @@ crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) -> SignalledError {
3631

3732
let mut visitor = MatchVisitor {
3833
tcx,
39-
body_owner: def_id,
4034
tables: tcx.body_tables(body_id),
41-
region_scope_tree: &tcx.region_scope_tree(def_id),
4235
param_env: tcx.param_env(def_id),
4336
identity_substs: InternalSubsts::identity_for_item(tcx, def_id),
4437
signalled_error: SignalledError::NoErrorsSeen,
@@ -53,11 +46,9 @@ fn create_e0004(sess: &Session, sp: Span, error_message: String) -> DiagnosticBu
5346

5447
struct MatchVisitor<'a, 'tcx> {
5548
tcx: TyCtxt<'tcx>,
56-
body_owner: DefId,
5749
tables: &'a ty::TypeckTables<'tcx>,
5850
param_env: ty::ParamEnv<'tcx>,
5951
identity_substs: SubstsRef<'tcx>,
60-
region_scope_tree: &'a region::ScopeTree,
6152
signalled_error: SignalledError,
6253
}
6354

@@ -151,11 +142,8 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
151142

152143
// Second, if there is a guard on each arm, make sure it isn't
153144
// assigning or borrowing anything mutably.
154-
if let Some(ref guard) = arm.guard {
145+
if arm.guard.is_some() {
155146
self.signalled_error = SignalledError::SawSomeError;
156-
if !self.tcx.features().bind_by_move_pattern_guards {
157-
check_for_mutation_in_guard(self, &guard);
158-
}
159147
}
160148

161149
// Third, perform some lints.
@@ -582,19 +570,10 @@ fn check_legality_of_move_bindings(
582570
"cannot bind by-move with sub-bindings")
583571
.span_label(p.span, "binds an already bound by-move value by moving it")
584572
.emit();
585-
} else if has_guard {
586-
if !cx.tcx.features().bind_by_move_pattern_guards {
587-
let mut err = struct_span_err!(cx.tcx.sess, p.span, E0008,
588-
"cannot bind by-move into a pattern guard");
589-
err.span_label(p.span, "moves value into pattern guard");
590-
if cx.tcx.sess.opts.unstable_features.is_nightly_build() {
591-
err.help("add `#![feature(bind_by_move_pattern_guards)]` to the \
592-
crate attributes to enable");
593-
}
594-
err.emit();
573+
} else if !has_guard {
574+
if let Some(_by_ref_span) = by_ref_span {
575+
span_vec.push(p.span);
595576
}
596-
} else if let Some(_by_ref_span) = by_ref_span {
597-
span_vec.push(p.span);
598577
}
599578
};
600579

@@ -636,67 +615,6 @@ fn check_legality_of_move_bindings(
636615
}
637616
}
638617

639-
/// Ensures that a pattern guard doesn't borrow by mutable reference or assign.
640-
//
641-
// FIXME: this should be done by borrowck.
642-
fn check_for_mutation_in_guard(cx: &MatchVisitor<'_, '_>, guard: &hir::Guard) {
643-
let mut checker = MutationChecker {
644-
cx,
645-
};
646-
match guard {
647-
hir::Guard::If(expr) =>
648-
ExprUseVisitor::new(&mut checker,
649-
cx.tcx,
650-
cx.body_owner,
651-
cx.param_env,
652-
cx.region_scope_tree,
653-
cx.tables,
654-
None).walk_expr(expr),
655-
};
656-
}
657-
658-
struct MutationChecker<'a, 'tcx> {
659-
cx: &'a MatchVisitor<'a, 'tcx>,
660-
}
661-
662-
impl<'a, 'tcx> Delegate<'tcx> for MutationChecker<'a, 'tcx> {
663-
fn matched_pat(&mut self, _: &Pat, _: &cmt_<'_>, _: euv::MatchMode) {}
664-
fn consume(&mut self, _: hir::HirId, _: Span, _: &cmt_<'_>, _: ConsumeMode) {}
665-
fn consume_pat(&mut self, _: &Pat, _: &cmt_<'_>, _: ConsumeMode) {}
666-
fn borrow(&mut self,
667-
_: hir::HirId,
668-
span: Span,
669-
_: &cmt_<'_>,
670-
_: ty::Region<'tcx>,
671-
kind:ty:: BorrowKind,
672-
_: LoanCause) {
673-
match kind {
674-
ty::MutBorrow => {
675-
let mut err = struct_span_err!(self.cx.tcx.sess, span, E0301,
676-
"cannot mutably borrow in a pattern guard");
677-
err.span_label(span, "borrowed mutably in pattern guard");
678-
if self.cx.tcx.sess.opts.unstable_features.is_nightly_build() {
679-
err.help("add `#![feature(bind_by_move_pattern_guards)]` to the \
680-
crate attributes to enable");
681-
}
682-
err.emit();
683-
}
684-
ty::ImmBorrow | ty::UniqueImmBorrow => {}
685-
}
686-
}
687-
fn decl_without_init(&mut self, _: hir::HirId, _: Span) {}
688-
fn mutate(&mut self, _: hir::HirId, span: Span, _: &cmt_<'_>, mode: MutateMode) {
689-
match mode {
690-
MutateMode::JustWrite | MutateMode::WriteAndRead => {
691-
struct_span_err!(self.cx.tcx.sess, span, E0302, "cannot assign in a pattern guard")
692-
.span_label(span, "assignment in pattern guard")
693-
.emit();
694-
}
695-
MutateMode::Init => {}
696-
}
697-
}
698-
}
699-
700618
/// Forbids bindings in `@` patterns. This is necessary for memory safety,
701619
/// because of the way rvalues are handled in the borrow check. (See issue
702620
/// #14587.)

src/libsyntax/feature_gate/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ declare_features! (
241241
(accepted, underscore_const_names, "1.37.0", Some(54912), None),
242242
/// Allows free and inherent `async fn`s, `async` blocks, and `<expr>.await` expressions.
243243
(accepted, async_await, "1.39.0", Some(50547), None),
244+
/// Allows mixing bind-by-move in patterns and references to those identifiers in guards.
245+
(accepted, bind_by_move_pattern_guards, "1.39.0", Some(15287), None),
244246

245247
// -------------------------------------------------------------------------
246248
// feature-group-end: accepted features

src/libsyntax/feature_gate/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,6 @@ declare_features! (
461461
/// Allows non-builtin attributes in inner attribute position.
462462
(active, custom_inner_attributes, "1.30.0", Some(54726), None),
463463

464-
/// Allows mixing bind-by-move in patterns and references to those identifiers in guards.
465-
(active, bind_by_move_pattern_guards, "1.30.0", Some(15287), None),
466-
467464
/// Allows `impl Trait` in bindings (`let`, `const`, `static`).
468465
(active, impl_trait_in_bindings, "1.30.0", Some(63065), None),
469466

0 commit comments

Comments
 (0)