Skip to content

Don't suggest &mut mut #59808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
@@ -828,7 +828,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
match bm {
ty::BindByReference(..) =>
mode.lub(BorrowingMatch),
ty::BindByValue(..) => {
ty::BindByValue{..} => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ty::BindByValue{..} => {
ty::BindByValue { .. } => {

(here and elsewhere)

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);
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
@@ -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: _,
15 changes: 11 additions & 4 deletions src/librustc/ty/binding.rs
Original file line number Diff line number Diff line change
@@ -5,16 +5,23 @@ 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, }

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 },
});
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
@@ -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" {
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/unused.rs
Original file line number Diff line number Diff line change
@@ -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,
}

2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/mutability_errors.rs
Original file line number Diff line number Diff line change
@@ -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(
4 changes: 3 additions & 1 deletion src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 11 additions & 5 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 2 additions & 3 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
@@ -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(
10 changes: 6 additions & 4 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -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);