From d543ae7dd36f51f28e9de0b5ac3210c163163d88 Mon Sep 17 00:00:00 2001 From: Saleem Jaffer Date: Tue, 9 Apr 2019 10:26:24 +0530 Subject: [PATCH] adding a bool field in BindingMode::BindByValue --- src/librustc/middle/expr_use_visitor.rs | 4 ++-- src/librustc/middle/mem_categorization.rs | 2 +- src/librustc/mir/mod.rs | 4 ++-- src/librustc/ty/binding.rs | 15 +++++++++++---- src/librustc_borrowck/borrowck/mod.rs | 4 ++-- src/librustc_borrowck/borrowck/unused.rs | 2 +- .../borrow_check/mutability_errors.rs | 2 +- src/librustc_mir/build/matches/mod.rs | 4 +++- src/librustc_mir/build/mod.rs | 16 +++++++++++----- src/librustc_mir/hair/pattern/check_match.rs | 5 ++--- src/librustc_mir/hair/pattern/mod.rs | 4 ++-- src/librustc_typeck/check/_match.rs | 10 ++++++---- src/librustc_typeck/check/mod.rs | 4 ++-- 13 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 6c8d9fe29d7a0..06a0063d70be6 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -828,7 +828,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { match bm { ty::BindByReference(..) => mode.lub(BorrowingMatch), - ty::BindByValue(..) => { + ty::BindByValue{..} => { match copy_or_move(&self.mc, self.param_env, &cmt_pat, PatBindingMove) { Copy => mode.lub(CopyingMatch), Move(..) => mode.lub(MovingMatch), @@ -877,7 +877,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { delegate.borrow(pat.hir_id, pat.span, &cmt_pat, r, bk, RefBinding); } } - ty::BindByValue(..) => { + ty::BindByValue{..} => { let mode = copy_or_move(mc, param_env, &cmt_pat, PatBindingMove); debug!("walk_pat binding consuming pat"); delegate.consume_pat(pat, &cmt_pat, mode); diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 4b169dea06c7c..3327c52d2a83f 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -344,7 +344,7 @@ impl MutabilityCategory { let bm = *tables.pat_binding_modes() .get(p.hir_id) .expect("missing binding mode"); - if bm == ty::BindByValue(hir::MutMutable) { + if let ty::BindByValue{mutability: hir::MutMutable, ..} = bm { McDeclared } else { McImmutable diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index d747f348ac990..965106cb80b89 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -876,7 +876,7 @@ impl<'tcx> LocalDecl<'tcx> { pub fn can_be_made_mutable(&self) -> bool { match self.is_user_variable { Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm { - binding_mode: ty::BindingMode::BindByValue(_), + binding_mode: ty::BindingMode::BindByValue{..}, opt_ty_info: _, opt_match_place: _, pat_span: _, @@ -895,7 +895,7 @@ impl<'tcx> LocalDecl<'tcx> { pub fn is_nonref_binding(&self) -> bool { match self.is_user_variable { Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm { - binding_mode: ty::BindingMode::BindByValue(_), + binding_mode: ty::BindingMode::BindByValue{..}, opt_ty_info: _, opt_match_place: _, pat_span: _, diff --git a/src/librustc/ty/binding.rs b/src/librustc/ty/binding.rs index 1290141b0a6b0..99b12053c9d6f 100644 --- a/src/librustc/ty/binding.rs +++ b/src/librustc/ty/binding.rs @@ -5,7 +5,10 @@ use crate::hir::Mutability; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] pub enum BindingMode { BindByReference(Mutability), - BindByValue(Mutability), + BindByValue { + coerced: bool, + mutability: Mutability, + }, } CloneTypeFoldableAndLiftImpls! { BindingMode, } @@ -13,8 +16,12 @@ CloneTypeFoldableAndLiftImpls! { BindingMode, } impl BindingMode { pub fn convert(ba: BindingAnnotation) -> BindingMode { match ba { - Unannotated => BindingMode::BindByValue(Mutability::MutImmutable), - Mutable => BindingMode::BindByValue(Mutability::MutMutable), + Unannotated => BindingMode::BindByValue{ + mutability: Mutability::MutImmutable, coerced: false + }, + Mutable => BindingMode::BindByValue{ + mutability: Mutability::MutMutable, coerced: false + }, Ref => BindingMode::BindByReference(Mutability::MutImmutable), RefMut => BindingMode::BindByReference(Mutability::MutMutable), } @@ -23,5 +30,5 @@ impl BindingMode { impl_stable_hash_for!(enum self::BindingMode { BindByReference(mutability), - BindByValue(mutability) + BindByValue{ mutability, coerced }, }); diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index fe39e3ae0c6cd..58b18b1bc8564 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -1252,7 +1252,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { ); } } - ty::BindByValue(..) => { + ty::BindByValue{..} => { if let (Some(local_ty), is_implicit_self) = self.local_ty(hir_id) { if let Some(msg) = self.suggest_mut_for_immutable(local_ty, is_implicit_self) { @@ -1285,7 +1285,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { borrowed_hir_id: hir::HirId, binding_hir_id: hir::HirId) { let let_span = self.tcx.hir().span_by_hir_id(binding_hir_id); - if let ty::BindByValue(..) = self.local_binding_mode(binding_hir_id) { + if let ty::BindByValue{..} = self.local_binding_mode(binding_hir_id) { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(let_span) { let (ty, is_implicit_self) = self.local_ty(binding_hir_id); if is_implicit_self && snippet != "self" { diff --git a/src/librustc_borrowck/borrowck/unused.rs b/src/librustc_borrowck/borrowck/unused.rs index 60a9c18e95ee9..0970f1bfda1f8 100644 --- a/src/librustc_borrowck/borrowck/unused.rs +++ b/src/librustc_borrowck/borrowck/unused.rs @@ -47,7 +47,7 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> { // for by-value bindings if let Some(&bm) = self.bccx.tables.pat_binding_modes().get(hir_id) { match bm { - ty::BindByValue(hir::MutMutable) => {} + ty::BindByValue{mutability: hir::MutMutable, ..} => {} _ => return, } diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index b8dae98ec64f2..44bcf4963173c 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -380,7 +380,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { } ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm { - binding_mode: ty::BindingMode::BindByValue(_), + binding_mode: ty::BindingMode::BindByValue{..}, opt_ty_info, .. })) => Some(suggest_ampmut( diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index be3d730c61a1d..ba0964f86c1d9 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -1714,7 +1714,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let tcx = self.hir.tcx(); let binding_mode = match mode { - BindingMode::ByValue => ty::BindingMode::BindByValue(mutability.into()), + BindingMode::ByValue => ty::BindingMode::BindByValue{ + mutability: mutability.into(), coerced: false, + }, BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability.into()), }; debug!("declare_binding: user_ty={:?}", user_ty); diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 7fe86d11c9ee4..d4070ffe040e0 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -652,10 +652,14 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>, if let hir::PatKind::Binding(_, _, ident, _) = pat.node { decl.debug_name = ident.name; if let Some(&bm) = hir.tables.pat_binding_modes().get(pat.hir_id) { - if bm == ty::BindByValue(hir::MutMutable) { - decl.mutability = Mutability::Mut; - } else { - decl.mutability = Mutability::Not; + match bm { + ty::BindByReference(_) => { decl.mutability = Mutability::Not; }, + ty::BindByValue{mutability: hir::MutImmutable, ..} => { + decl.mutability = Mutability::Not; + }, + ty::BindByValue{mutability: hir::MutMutable, ..} => { + decl.mutability = Mutability::Mut; + }, } } else { tcx.sess.delay_span_bug(pat.span, "missing binding mode"); @@ -906,7 +910,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { if let Some(kind) = self_binding { Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf(*kind))) } else { - let binding_mode = ty::BindingMode::BindByValue(mutability.into()); + let binding_mode = ty::BindingMode::BindByValue{ + mutability: mutability.into(), coerced: false, + }; Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm { binding_mode, opt_ty_info, diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 50df676aea9fb..6167781d7eeb8 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -304,8 +304,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa pat.walk(|p| { if let PatKind::Binding(_, _, ident, None) = p.node { if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) { - if bm != ty::BindByValue(hir::MutImmutable) { - // Nothing to check. + if let ty::BindByValue{mutability: hir::MutImmutable, coerced: _} = bm { return true; } let pat_ty = cx.tables.pat_ty(p); @@ -580,7 +579,7 @@ fn check_legality_of_move_bindings( if let PatKind::Binding(_, _, _, ref sub) = p.node { if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) { match bm { - ty::BindByValue(..) => { + ty::BindByValue{..} => { let pat_ty = cx.tables.node_type(p.hir_id); if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) { check_move(p, sub.as_ref().map(|p| &**p), span_vec); diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index 6c532fce57f0e..0b425640c2321 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -567,9 +567,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> { let bm = *self.tables.pat_binding_modes().get(pat.hir_id) .expect("missing binding mode"); let (mutability, mode) = match bm { - ty::BindByValue(hir::MutMutable) => + ty::BindByValue{mutability: hir::MutMutable, ..} => (Mutability::Mut, BindingMode::ByValue), - ty::BindByValue(hir::MutImmutable) => + ty::BindByValue{mutability: hir::MutImmutable, ..} => (Mutability::Not, BindingMode::ByValue), ty::BindByReference(hir::MutMutable) => (Mutability::Not, BindingMode::ByRef( diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index affd3a2d16af7..a539fdddecbe4 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -98,7 +98,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { def_bm = match def_bm { // If default binding mode is by value, make it `ref` or `ref mut` // (depending on whether we observe `&` or `&mut`). - ty::BindByValue(_) => + ty::BindByValue{..} => ty::BindByReference(inner_mutability), // Once a `ref`, always a `ref`. This is because a `& &mut` can't mutate @@ -132,7 +132,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // ``` // // cc #46688 - def_bm = ty::BindByValue(hir::MutImmutable); + def_bm = ty::BindByValue{mutability: hir::MutImmutable, coerced: false}; } // Lose mutability now that we know binding mode and discriminant type. @@ -256,7 +256,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { self.demand_eqtype_pat(pat.span, region_ty, local_ty, match_discrim_span); } // otherwise the type of x is the expected type T - ty::BindByValue(_) => { + ty::BindByValue{..} => { // As above, `T <: typeof(x)` is required but we // use equality, see (*) below. self.demand_eqtype_pat(pat.span, expected, local_ty, match_discrim_span); @@ -648,7 +648,9 @@ https://doc.rust-lang.org/reference/types.html#trait-objects"); self.check_pat_walk( &p, discrim_ty, - ty::BindingMode::BindByValue(hir::Mutability::MutImmutable), + ty::BindingMode::BindByValue{ + mutability: hir::Mutability::MutImmutable, coerced: false + }, Some(discrim.span), ); all_pats_diverge &= self.diverges.get(); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 94f76b03a643c..f7fadd547fb3d 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1079,7 +1079,7 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>, fcx.check_pat_walk( &arg.pat, arg_ty, - ty::BindingMode::BindByValue(hir::Mutability::MutImmutable), + ty::BindingMode::BindByValue{mutability: hir::Mutability::MutImmutable, coerced: false}, None, ); @@ -4876,7 +4876,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { self.check_pat_walk( &local.pat, t, - ty::BindingMode::BindByValue(hir::Mutability::MutImmutable), + ty::BindingMode::BindByValue{mutability: hir::Mutability::MutImmutable, coerced: false}, None, ); let pat_ty = self.node_ty(local.pat.hir_id);