Add help message for mutation though overloaded place operators

This commit is contained in:
Matthew Jasper 2019-07-07 14:16:58 +01:00
parent 5b86c604f9
commit 38306adf16
12 changed files with 149 additions and 140 deletions

View File

@ -653,6 +653,26 @@ impl BorrowedContentSource<'tcx> {
} }
} }
pub(super) fn describe_for_immutable_place(&self) -> String {
match *self {
BorrowedContentSource::DerefRawPointer => format!("a `*const` pointer"),
BorrowedContentSource::DerefSharedRef => format!("a `&` reference"),
BorrowedContentSource::DerefMutableRef => {
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
},
BorrowedContentSource::OverloadedDeref(ty) => {
if ty.is_rc() {
format!("an `Rc`")
} else if ty.is_arc() {
format!("an `Arc`")
} else {
format!("a dereference of `{}`", ty)
}
}
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
}
}
fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> { fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
match func.sty { match func.sty {
ty::FnDef(def_id, substs) => { ty::FnDef(def_id, substs) => {

View File

@ -1,17 +1,14 @@
use rustc::hir; use rustc::hir;
use rustc::hir::Node; use rustc::hir::Node;
use rustc::mir::{self, BindingForm, Constant, ClearCrossCrate, Local, Location, Body}; use rustc::mir::{self, BindingForm, ClearCrossCrate, Local, Location, Body};
use rustc::mir::{ use rustc::mir::{Mutability, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind};
Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind, use rustc::ty::{self, Ty, TyCtxt};
};
use rustc::mir::{Terminator, TerminatorKind};
use rustc::ty::{self, Const, DefIdTree, Ty, TyS, TyCtxt};
use rustc_data_structures::indexed_vec::Idx; use rustc_data_structures::indexed_vec::Idx;
use syntax_pos::Span; use syntax_pos::Span;
use syntax_pos::symbol::kw; use syntax_pos::symbol::kw;
use crate::dataflow::move_paths::InitLocation;
use crate::borrow_check::MirBorrowckCtxt; use crate::borrow_check::MirBorrowckCtxt;
use crate::borrow_check::error_reporting::BorrowedContentSource;
use crate::util::borrowck_errors::{BorrowckErrors, Origin}; use crate::util::borrowck_errors::{BorrowckErrors, Origin};
use crate::util::collect_writes::FindAssignments; use crate::util::collect_writes::FindAssignments;
use crate::util::suggest_ref_mut; use crate::util::suggest_ref_mut;
@ -43,6 +40,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let mut err; let mut err;
let item_msg; let item_msg;
let reason; let reason;
let mut opt_source = None;
let access_place_desc = self.describe_place(access_place); let access_place_desc = self.describe_place(access_place);
debug!("report_mutability_error: access_place_desc={:?}", access_place_desc); debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);
@ -103,23 +101,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
item_msg = format!("`{}`", access_place_desc.unwrap()); item_msg = format!("`{}`", access_place_desc.unwrap());
reason = ", as it is immutable for the pattern guard".to_string(); reason = ", as it is immutable for the pattern guard".to_string();
} else { } else {
let pointer_type = let source = self.borrowed_content_source(base);
if base.ty(self.body, self.infcx.tcx).ty.is_region_ptr() { let pointer_type = source.describe_for_immutable_place();
"`&` reference" opt_source = Some(source);
} else {
"`*const` pointer"
};
if let Some(desc) = access_place_desc { if let Some(desc) = access_place_desc {
item_msg = format!("`{}`", desc); item_msg = format!("`{}`", desc);
reason = match error_access { reason = match error_access {
AccessKind::Move | AccessKind::Move |
AccessKind::Mutate => format!(" which is behind a {}", pointer_type), AccessKind::Mutate => format!(" which is behind {}", pointer_type),
AccessKind::MutableBorrow => { AccessKind::MutableBorrow => {
format!(", as it is behind a {}", pointer_type) format!(", as it is behind {}", pointer_type)
} }
} }
} else { } else {
item_msg = format!("data in a {}", pointer_type); item_msg = format!("data in {}", pointer_type);
reason = String::new(); reason = String::new();
} }
} }
@ -457,59 +452,31 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
} }
Place::Projection(box Projection { Place::Projection(box Projection {
base: Place::Base(PlaceBase::Local(local)), base: _,
elem: ProjectionElem::Deref, elem: ProjectionElem::Deref,
}) if error_access == AccessKind::MutableBorrow => { }) => {
err.span_label(span, format!("cannot {ACT}", ACT = act)); err.span_label(span, format!("cannot {ACT}", ACT = act));
let mpi = self.move_data.rev_lookup.find_local(*local); match opt_source {
for i in self.move_data.init_path_map[mpi].iter() { Some(BorrowedContentSource::OverloadedDeref(ty)) => {
if let InitLocation::Statement(location) = self.move_data.inits[*i].location { err.help(
if let Some( &format!(
Terminator { "trait `DerefMut` is required to modify through a dereference, \
kind: TerminatorKind::Call { but it is not implemented for `{}`",
func: Operand::Constant(box Constant { ty,
literal: Const { ),
ty: &TyS { );
sty: ty::FnDef(id, substs), },
.. Some(BorrowedContentSource::OverloadedIndex(ty)) => {
}, err.help(
.. &format!(
}, "trait `IndexMut` is required to modify indexed content, \
.. but it is not implemented for `{}`",
}), ty,
.. ),
}, );
..
}
) = &self.body.basic_blocks()[location.block].terminator {
let index_trait = self.infcx.tcx.lang_items().index_trait();
if self.infcx.tcx.parent(id) == index_trait {
let mut found = false;
self.infcx.tcx.for_each_relevant_impl(
self.infcx.tcx.lang_items().index_mut_trait().unwrap(),
substs.type_at(0),
|_relevant_impl| {
found = true;
}
);
let extra = if found {
String::new()
} else {
format!(", but it is not implemented for `{}`",
substs.type_at(0))
};
err.help(
&format!(
"trait `IndexMut` is required to modify indexed content{}",
extra,
),
);
}
}
} }
_ => (),
} }
} }

View File

@ -2,18 +2,7 @@
// Deref and not DerefMut is implemented. // Deref and not DerefMut is implemented.
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc;
struct Rc<T> {
value: *const T
}
impl<T> Deref for Rc<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.value }
}
}
struct Point { struct Point {
x: isize, x: isize,

View File

@ -1,86 +1,114 @@
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:47:19 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:36:19
| |
LL | let __isize = &mut x.y; LL | let __isize = &mut x.y;
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:51:19 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:40:19
| |
LL | let __isize = &mut x.y; LL | let __isize = &mut x.y;
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:59:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:48:5
| |
LL | &mut x.y LL | &mut x.y
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:63:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:52:5
| |
LL | &mut x.y LL | &mut x.y
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:67:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:56:5
| |
LL | x.y = 3; LL | x.y = 3;
| ^^^^^^^ cannot assign | ^^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:71:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:60:5
| |
LL | x.y = 3; LL | x.y = 3;
| ^^^^^^^ cannot assign | ^^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:75:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:64:5
| |
LL | x.y = 3; LL | x.y = 3;
| ^^^^^^^ cannot assign | ^^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:83:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:72:5
| |
LL | x.set(0, 0); LL | x.set(0, 0);
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:87:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:76:5
| |
LL | x.set(0, 0); LL | x.set(0, 0);
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:95:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:84:5
| |
LL | x.y_mut() LL | x.y_mut()
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:99:5 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:88:5
| |
LL | x.y_mut() LL | x.y_mut()
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:103:6 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:92:6
| |
LL | *x.y_mut() = 3; LL | *x.y_mut() = 3;
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:107:6 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:96:6
| |
LL | *x.y_mut() = 3; LL | *x.y_mut() = 3;
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-auto-deref.rs:111:6 --> $DIR/borrowck-borrow-overloaded-auto-deref.rs:100:6
| |
LL | *x.y_mut() = 3; LL | *x.y_mut() = 3;
| ^ cannot borrow as mutable | ^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<Point>`
error: aborting due to 14 previous errors error: aborting due to 14 previous errors

View File

@ -2,18 +2,7 @@
// Deref and not DerefMut is implemented. // Deref and not DerefMut is implemented.
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc;
struct Rc<T> {
value: *const T
}
impl<T> Deref for Rc<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { &*self.value }
}
}
fn deref_imm(x: Rc<isize>) { fn deref_imm(x: Rc<isize>) {
let __isize = &*x; let __isize = &*x;

View File

@ -1,44 +1,58 @@
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-deref.rs:23:19 --> $DIR/borrowck-borrow-overloaded-deref.rs:12:19
| |
LL | let __isize = &mut *x; LL | let __isize = &mut *x;
| ^^^^^^^ cannot borrow as mutable | ^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-deref.rs:27:19 --> $DIR/borrowck-borrow-overloaded-deref.rs:16:19
| |
LL | let __isize = &mut *x; LL | let __isize = &mut *x;
| ^^^^^^^ cannot borrow as mutable | ^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-deref.rs:35:5 --> $DIR/borrowck-borrow-overloaded-deref.rs:24:5
| |
LL | &mut **x LL | &mut **x
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an `Rc` as mutable
--> $DIR/borrowck-borrow-overloaded-deref.rs:39:5 --> $DIR/borrowck-borrow-overloaded-deref.rs:28:5
| |
LL | &mut **x LL | &mut **x
| ^^^^^^^^ cannot borrow as mutable | ^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-deref.rs:43:5 --> $DIR/borrowck-borrow-overloaded-deref.rs:32:5
| |
LL | *x = 3; LL | *x = 3;
| ^^^^^^ cannot assign | ^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-deref.rs:47:5 --> $DIR/borrowck-borrow-overloaded-deref.rs:36:5
| |
LL | **x = 3; LL | **x = 3;
| ^^^^^^^ cannot assign | ^^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an `Rc`
--> $DIR/borrowck-borrow-overloaded-deref.rs:51:5 --> $DIR/borrowck-borrow-overloaded-deref.rs:40:5
| |
LL | **x = 3; LL | **x = 3;
| ^^^^^^^ cannot assign | ^^^^^^^ cannot assign
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::rc::Rc<isize>`
error: aborting due to 7 previous errors error: aborting due to 7 previous errors

View File

@ -54,6 +54,6 @@ fn main() {
x: 1, x: 1,
}; };
s[2] = 20; s[2] = 20;
//~^ ERROR cannot assign to data in a `&` reference //~^ ERROR cannot assign to data in an index of `Bar`
drop(rs); drop(rs);
} }

View File

@ -21,11 +21,13 @@ LL | f[&s] = 10;
LL | drop(rs); LL | drop(rs);
| -- mutable borrow later used here | -- mutable borrow later used here
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an index of `Bar`
--> $DIR/borrowck-overloaded-index-ref-index.rs:56:5 --> $DIR/borrowck-overloaded-index-ref-index.rs:56:5
| |
LL | s[2] = 20; LL | s[2] = 20;
| ^^^^^^^^^ cannot assign | ^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `Bar`
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -3,8 +3,6 @@ error[E0596]: cannot borrow data in a `&` reference as mutable
| |
LL | Index::index(&v, 1..2).make_ascii_uppercase(); LL | Index::index(&v, 1..2).make_ascii_uppercase();
| ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable | ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content
error: aborting due to previous error error: aborting due to previous error

View File

@ -1,4 +1,4 @@
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an index of `std::collections::HashMap<&str, std::string::String>` as mutable
--> $DIR/index-mut-help.rs:11:5 --> $DIR/index-mut-help.rs:11:5
| |
LL | map["peter"].clear(); LL | map["peter"].clear();
@ -6,13 +6,15 @@ LL | map["peter"].clear();
| |
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>` = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`
error[E0594]: cannot assign to data in a `&` reference error[E0594]: cannot assign to data in an index of `std::collections::HashMap<&str, std::string::String>`
--> $DIR/index-mut-help.rs:12:5 --> $DIR/index-mut-help.rs:12:5
| |
LL | map["peter"] = "0".to_string(); LL | map["peter"] = "0".to_string();
| ^^^^^^^^^^^^ cannot assign | ^^^^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an index of `std::collections::HashMap<&str, std::string::String>` as mutable
--> $DIR/index-mut-help.rs:13:13 --> $DIR/index-mut-help.rs:13:13
| |
LL | let _ = &mut map["peter"]; LL | let _ = &mut map["peter"];

View File

@ -2,6 +2,6 @@ use std::collections::HashMap;
fn main() { fn main() {
let things: HashMap<String, Vec<String>> = HashMap::new(); let things: HashMap<String, Vec<String>> = HashMap::new();
for src in things.keys() { for src in things.keys() {
things[src.as_str()].sort(); //~ ERROR cannot borrow data in a `&` reference as mutable things[src.as_str()].sort(); //~ ERROR cannot borrow data in an index of
} }
} }

View File

@ -1,4 +1,4 @@
error[E0596]: cannot borrow data in a `&` reference as mutable error[E0596]: cannot borrow data in an index of `std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>` as mutable
--> $DIR/issue-41726.rs:5:9 --> $DIR/issue-41726.rs:5:9
| |
LL | things[src.as_str()].sort(); LL | things[src.as_str()].sort();