-
Notifications
You must be signed in to change notification settings - Fork 13k
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
NLL fails to suggest "try removing &mut
here"
#54720
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -583,11 +583,27 @@ pub enum BindingForm<'tcx> { | |
/// This is a binding for a non-`self` binding, or a `self` that has an explicit type. | ||
Var(VarBindingForm<'tcx>), | ||
/// Binding for a `self`/`&self`/`&mut self` binding where the type is implicit. | ||
ImplicitSelf, | ||
ImplicitSelf(ImplicitSelfKind), | ||
/// Reference used in a guard expression to ensure immutability. | ||
RefForGuard, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the ideal of lowering away pattern-matching completely is too naive in the face of diagnostics 😭 |
||
|
||
/// Represents what type of implicit self a function has, if any. | ||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)] | ||
pub enum ImplicitSelfKind { | ||
/// Represents a `fn x(self);`. | ||
Imm, | ||
/// Represents a `fn x(mut self);`. | ||
Mut, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't these two be distinguished by |
||
/// Represents a `fn x(&self);`. | ||
ImmRef, | ||
/// Represents a `fn x(&mut self);`. | ||
MutRef, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't these two be distinguished by type? (maybe even distinguish |
||
/// Represents when a function does not have a self argument or | ||
/// when a function has a `self: X` argument. | ||
None | ||
} | ||
|
||
CloneTypeFoldableAndLiftImpls! { BindingForm<'tcx>, } | ||
|
||
impl_stable_hash_for!(struct self::VarBindingForm<'tcx> { | ||
|
@@ -597,6 +613,14 @@ impl_stable_hash_for!(struct self::VarBindingForm<'tcx> { | |
pat_span | ||
}); | ||
|
||
impl_stable_hash_for!(enum self::ImplicitSelfKind { | ||
Imm, | ||
Mut, | ||
ImmRef, | ||
MutRef, | ||
None | ||
}); | ||
|
||
mod binding_form_impl { | ||
use ich::StableHashingContext; | ||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult}; | ||
|
@@ -612,7 +636,7 @@ mod binding_form_impl { | |
|
||
match self { | ||
Var(binding) => binding.hash_stable(hcx, hasher), | ||
ImplicitSelf => (), | ||
ImplicitSelf(kind) => kind.hash_stable(hcx, hasher), | ||
RefForGuard => (), | ||
} | ||
} | ||
|
@@ -775,10 +799,9 @@ impl<'tcx> LocalDecl<'tcx> { | |
pat_span: _, | ||
}))) => true, | ||
|
||
// FIXME: might be able to thread the distinction between | ||
// `self`/`mut self`/`&self`/`&mut self` into the | ||
// `BindingForm::ImplicitSelf` variant, (and then return | ||
// true here for solely the first case). | ||
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf(ImplicitSelfKind::Imm))) | ||
=> true, | ||
|
||
_ => false, | ||
} | ||
} | ||
|
@@ -795,7 +818,7 @@ impl<'tcx> LocalDecl<'tcx> { | |
pat_span: _, | ||
}))) => true, | ||
|
||
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf)) => true, | ||
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf(_))) => true, | ||
|
||
_ => false, | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use rustc::mir::TerminatorKind; | |
use rustc::ty::{self, Const, DefIdTree, TyS, TyKind, TyCtxt}; | ||
use rustc_data_structures::indexed_vec::Idx; | ||
use syntax_pos::Span; | ||
use syntax_pos::symbol::keywords; | ||
|
||
use dataflow::move_paths::InitLocation; | ||
use borrow_check::MirBorrowckCtxt; | ||
|
@@ -217,6 +218,40 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { | |
debug!("report_mutability_error: act={:?}, acted_on={:?}", act, acted_on); | ||
|
||
match the_place_err { | ||
// Suggest removing a `&mut` from the use of a mutable reference. | ||
Place::Local(local) | ||
if { | ||
self.mir.local_decls.get(*local).map(|local_decl| { | ||
davidtwco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let ClearCrossCrate::Set( | ||
mir::BindingForm::ImplicitSelf(kind) | ||
) = local_decl.is_user_variable.as_ref().unwrap() { | ||
// Check if the user variable is a `&mut self` and we can therefore | ||
// suggest removing the `&mut`. | ||
// | ||
// Deliberately fall into this case for all implicit self types, | ||
// so that we don't fall in to the next case with them. | ||
*kind == mir::ImplicitSelfKind::MutRef | ||
} else if Some(keywords::SelfValue.name()) == local_decl.name { | ||
// Otherwise, check if the name is the self kewyord - in which case | ||
// we have an explicit self. Do the same thing in this case and check | ||
// for a `self: &mut Self` to suggest removing the `&mut`. | ||
if let ty::TyKind::Ref( | ||
_, _, hir::Mutability::MutMutable | ||
) = local_decl.ty.sty { | ||
true | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
}).unwrap_or(false) | ||
} => | ||
{ | ||
err.span_label(span, format!("cannot {ACT}", ACT = act)); | ||
err.span_label(span, "try removing `&mut` here"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that if we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be misunderstanding but, wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you're right, I misread the code. It'd be nice to get the actual span to get the behavior I wanted, but it might be too hard to do in this case. Don't let it be a blocker. |
||
}, | ||
|
||
// We want to suggest users use `let mut` for local (user | ||
// variable) mutations... | ||
Place::Local(local) if self.mir.local_decls[*local].can_be_made_mutable() => { | ||
|
@@ -316,7 +351,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { | |
{ | ||
let local_decl = &self.mir.local_decls[*local]; | ||
let suggestion = match local_decl.is_user_variable.as_ref().unwrap() { | ||
ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf) => { | ||
ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf(_)) => { | ||
Some(suggest_ampmut_self(self.infcx.tcx, local_decl)) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(nll)] | ||
|
||
struct Struct; | ||
|
||
impl Struct { | ||
fn bar(self: &mut Self) { | ||
(&mut self).bar(); | ||
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] | ||
} | ||
|
||
fn imm(self) { | ||
(&mut self).bar(); | ||
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] | ||
} | ||
|
||
fn mtbl(mut self) { | ||
(&mut self).bar(); | ||
} | ||
|
||
fn immref(&self) { | ||
(&mut self).bar(); | ||
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] | ||
//~^^ ERROR cannot borrow data in a `&` reference as mutable [E0596] | ||
} | ||
|
||
fn mtblref(&mut self) { | ||
(&mut self).bar(); | ||
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] | ||
} | ||
} | ||
|
||
fn main () {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit sad, I was hoping this could stay out of the HIR permanently.