2019-11-27 11:39:25 -06:00
|
|
|
//! Borrow checker diagnostics.
|
|
|
|
|
2021-12-09 22:42:17 +08:00
|
|
|
use rustc_const_eval::util::call_kind;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_errors::DiagnosticBuilder;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::Namespace;
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::GeneratorKind;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2021-03-25 23:03:12 -04:00
|
|
|
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
|
|
|
|
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
|
2018-09-09 19:34:39 +02:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::print::Print;
|
2020-07-25 07:04:13 -04:00
|
|
|
use rustc_middle::ty::{self, DefIdTree, Instance, Ty, TyCtxt};
|
2021-01-05 19:53:07 +01:00
|
|
|
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
|
2021-12-09 22:42:17 +08:00
|
|
|
use rustc_span::{symbol::sym, Span};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2017-12-10 21:15:52 +00:00
|
|
|
|
2018-06-19 21:22:52 -03:00
|
|
|
use super::borrow_set::BorrowData;
|
2019-10-10 22:20:57 +08:00
|
|
|
use super::MirBorrowckCtxt;
|
2017-12-06 20:27:38 +02:00
|
|
|
|
2021-03-15 15:09:06 -07:00
|
|
|
mod find_all_local_uses;
|
2019-11-27 11:45:05 -06:00
|
|
|
mod find_use;
|
2019-11-27 12:22:17 -06:00
|
|
|
mod outlives_suggestion;
|
2019-12-22 17:42:04 -05:00
|
|
|
mod region_name;
|
|
|
|
mod var_name;
|
2019-11-27 11:45:05 -06:00
|
|
|
|
2021-03-07 14:41:45 +00:00
|
|
|
mod bound_region_errors;
|
2019-12-02 17:05:25 -06:00
|
|
|
mod conflict_errors;
|
2019-12-22 17:42:04 -05:00
|
|
|
mod explain_borrow;
|
2019-12-02 17:05:25 -06:00
|
|
|
mod move_errors;
|
|
|
|
mod mutability_errors;
|
|
|
|
mod region_errors;
|
2019-11-27 11:39:25 -06:00
|
|
|
|
2021-03-07 14:41:45 +00:00
|
|
|
crate use bound_region_errors::{ToUniverseInfo, UniverseInfo};
|
2019-12-02 17:05:25 -06:00
|
|
|
crate use mutability_errors::AccessKind;
|
2019-11-27 12:22:17 -06:00
|
|
|
crate use outlives_suggestion::OutlivesSuggestionBuilder;
|
2019-12-20 23:59:31 -06:00
|
|
|
crate use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
|
2019-12-28 20:36:42 -06:00
|
|
|
crate use region_name::{RegionName, RegionNameSource};
|
2021-12-09 22:42:17 +08:00
|
|
|
crate use rustc_const_eval::util::CallKind;
|
2019-11-27 12:22:17 -06:00
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) struct IncludingDowncast(pub(super) bool);
|
2018-09-23 16:07:45 +01:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
2018-12-24 15:29:40 +01:00
|
|
|
/// Adds a suggestion when a closure is invoked twice with a moved variable or when a closure
|
|
|
|
/// is moved after being invoked.
|
2018-10-11 01:19:55 +02:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// note: closure cannot be invoked more than once because it moves the variable `dict` out of
|
|
|
|
/// its environment
|
|
|
|
/// --> $DIR/issue-42065.rs:16:29
|
|
|
|
/// |
|
|
|
|
/// LL | for (key, value) in dict {
|
|
|
|
/// | ^^^^
|
|
|
|
/// ```
|
2018-12-24 15:29:40 +01:00
|
|
|
pub(super) fn add_moved_or_invoked_closure_note(
|
2018-10-11 01:19:55 +02:00
|
|
|
&self,
|
|
|
|
location: Location,
|
2020-03-04 18:25:03 -03:00
|
|
|
place: PlaceRef<'tcx>,
|
2018-10-11 01:19:55 +02:00
|
|
|
diag: &mut DiagnosticBuilder<'_>,
|
|
|
|
) {
|
2018-12-24 15:29:40 +01:00
|
|
|
debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
|
2019-05-27 23:37:18 +02:00
|
|
|
let mut target = place.local_or_deref_local();
|
2019-11-06 12:00:46 -05:00
|
|
|
for stmt in &self.body[location.block].statements[location.statement_index..] {
|
2018-12-24 15:29:40 +01:00
|
|
|
debug!("add_moved_or_invoked_closure_note: stmt={:?} target={:?}", stmt, target);
|
2019-12-22 17:42:04 -05:00
|
|
|
if let StatementKind::Assign(box (into, Rvalue::Use(from))) = &stmt.kind {
|
2018-12-24 15:29:40 +01:00
|
|
|
debug!("add_fnonce_closure_note: into={:?} from={:?}", into, from);
|
2018-10-11 01:19:55 +02:00
|
|
|
match from {
|
2019-12-22 17:42:04 -05:00
|
|
|
Operand::Copy(ref place) | Operand::Move(ref place)
|
|
|
|
if target == place.local_or_deref_local() =>
|
|
|
|
{
|
|
|
|
target = into.local_or_deref_local()
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-10-11 01:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 15:29:40 +01:00
|
|
|
// Check if we are attempting to call a closure after it has been invoked.
|
2019-11-06 12:00:46 -05:00
|
|
|
let terminator = self.body[location.block].terminator();
|
2018-12-24 15:29:40 +01:00
|
|
|
debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
|
2018-10-11 01:19:55 +02:00
|
|
|
if let TerminatorKind::Call {
|
2021-03-08 16:18:03 +00:00
|
|
|
func: Operand::Constant(box Constant { literal, .. }),
|
2018-10-11 01:19:55 +02:00
|
|
|
args,
|
|
|
|
..
|
2019-12-22 17:42:04 -05:00
|
|
|
} = &terminator.kind
|
|
|
|
{
|
2021-03-08 16:18:03 +00:00
|
|
|
if let ty::FnDef(id, _) = *literal.ty().kind() {
|
2020-08-03 00:49:11 +02:00
|
|
|
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
|
|
|
|
if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
|
|
|
|
let closure = match args.first() {
|
|
|
|
Some(Operand::Copy(ref place)) | Some(Operand::Move(ref place))
|
|
|
|
if target == place.local_or_deref_local() =>
|
|
|
|
{
|
|
|
|
place.local_or_deref_local().unwrap()
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
2018-10-11 01:19:55 +02:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
|
|
|
|
if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind() {
|
|
|
|
let did = did.expect_local();
|
|
|
|
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
|
2018-10-16 19:37:01 +02:00
|
|
|
|
2020-12-02 22:25:22 -05:00
|
|
|
if let Some((span, hir_place)) =
|
2020-08-03 00:49:11 +02:00
|
|
|
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
|
|
|
|
{
|
|
|
|
diag.span_note(
|
|
|
|
*span,
|
|
|
|
&format!(
|
|
|
|
"closure cannot be invoked more than once because it moves the \
|
|
|
|
variable `{}` out of its environment",
|
2020-12-02 22:25:22 -05:00
|
|
|
ty::place_to_string_for_capture(self.infcx.tcx, hir_place)
|
2020-08-03 00:49:11 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-16 19:37:01 +02:00
|
|
|
}
|
2018-10-11 01:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 15:29:40 +01:00
|
|
|
|
|
|
|
// Check if we are just moving a closure after it has been invoked.
|
|
|
|
if let Some(target) = target {
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind() {
|
2020-04-17 15:17:01 +01:00
|
|
|
let did = did.expect_local();
|
2020-08-12 12:22:56 +02:00
|
|
|
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
|
2018-12-24 15:29:40 +01:00
|
|
|
|
2020-12-02 22:25:22 -05:00
|
|
|
if let Some((span, hir_place)) =
|
2020-07-17 08:47:04 +00:00
|
|
|
self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id)
|
2018-12-24 15:29:40 +01:00
|
|
|
{
|
|
|
|
diag.span_note(
|
|
|
|
*span,
|
|
|
|
&format!(
|
|
|
|
"closure cannot be moved more than once as it is not `Copy` due to \
|
|
|
|
moving the variable `{}` out of its environment",
|
2020-12-02 22:25:22 -05:00
|
|
|
ty::place_to_string_for_capture(self.infcx.tcx, hir_place)
|
2018-12-24 15:29:40 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 01:19:55 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
/// End-user visible description of `place` if one can be found.
|
2020-03-26 01:55:16 +01:00
|
|
|
/// If the place is a temporary for instance, `"value"` will be returned.
|
|
|
|
pub(super) fn describe_any_place(&self, place_ref: PlaceRef<'tcx>) -> String {
|
2020-03-25 11:17:06 +01:00
|
|
|
match self.describe_place(place_ref) {
|
2020-03-26 01:55:16 +01:00
|
|
|
Some(mut descr) => {
|
|
|
|
// Surround descr with `backticks`.
|
|
|
|
descr.reserve(2);
|
2020-09-10 13:57:40 +02:00
|
|
|
descr.insert(0, '`');
|
|
|
|
descr.push('`');
|
2020-03-26 01:55:16 +01:00
|
|
|
descr
|
|
|
|
}
|
2020-03-25 11:17:06 +01:00
|
|
|
None => "value".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// End-user visible description of `place` if one can be found.
|
|
|
|
/// If the place is a temporary for instance, None will be returned.
|
2020-03-04 18:25:03 -03:00
|
|
|
pub(super) fn describe_place(&self, place_ref: PlaceRef<'tcx>) -> Option<String> {
|
2019-07-19 19:04:01 +02:00
|
|
|
self.describe_place_with_options(place_ref, IncludingDowncast(false))
|
2018-06-20 23:51:18 -03:00
|
|
|
}
|
|
|
|
|
2018-10-11 01:19:55 +02:00
|
|
|
/// End-user visible description of `place` if one can be found. If the
|
|
|
|
/// place is a temporary for instance, None will be returned.
|
|
|
|
/// `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
|
|
|
|
/// `Downcast` and `IncludingDowncast` is true
|
2018-06-22 00:10:52 -03:00
|
|
|
pub(super) fn describe_place_with_options(
|
|
|
|
&self,
|
2020-03-04 18:25:03 -03:00
|
|
|
place: PlaceRef<'tcx>,
|
2018-06-22 00:10:52 -03:00
|
|
|
including_downcast: IncludingDowncast,
|
|
|
|
) -> Option<String> {
|
2017-12-06 20:27:38 +02:00
|
|
|
let mut buf = String::new();
|
2018-06-20 23:51:18 -03:00
|
|
|
match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
|
2017-12-06 20:27:38 +02:00
|
|
|
Ok(()) => Some(buf),
|
|
|
|
Err(()) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 01:19:55 +02:00
|
|
|
/// Appends end-user visible description of `place` to `buf`.
|
2017-12-06 20:27:38 +02:00
|
|
|
fn append_place_to_string(
|
|
|
|
&self,
|
2020-03-04 18:25:03 -03:00
|
|
|
place: PlaceRef<'tcx>,
|
2017-12-06 20:27:38 +02:00
|
|
|
buf: &mut String,
|
|
|
|
mut autoderef: bool,
|
2018-06-20 23:51:18 -03:00
|
|
|
including_downcast: &IncludingDowncast,
|
2017-12-06 20:27:38 +02:00
|
|
|
) -> Result<(), ()> {
|
2019-07-11 19:25:37 +02:00
|
|
|
match place {
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [] } => {
|
2020-01-14 02:10:05 -03:00
|
|
|
self.append_local_to_string(local, buf)?;
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [ProjectionElem::Deref] }
|
2020-01-14 02:10:05 -03:00
|
|
|
if self.body.local_decls[local].is_ref_for_guard() =>
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-11-18 23:04:06 +00:00
|
|
|
self.append_place_to_string(
|
2020-03-06 19:28:44 +01:00
|
|
|
PlaceRef { local, projection: &[] },
|
2019-11-18 23:04:06 +00:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [ProjectionElem::Deref] }
|
2020-01-14 02:10:05 -03:00
|
|
|
if self.body.local_decls[local].is_ref_to_static() =>
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2020-01-14 02:10:05 -03:00
|
|
|
let local_info = &self.body.local_decls[local].local_info;
|
2020-05-06 12:29:00 +10:00
|
|
|
if let Some(box LocalInfo::StaticRef { def_id, .. }) = *local_info {
|
2021-12-15 14:39:23 +11:00
|
|
|
buf.push_str(self.infcx.tcx.item_name(def_id).as_str());
|
2019-11-18 23:04:06 +00:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [proj_base @ .., elem] } => {
|
2019-07-30 00:07:28 +02:00
|
|
|
match elem {
|
2017-12-06 20:27:38 +02:00
|
|
|
ProjectionElem::Deref => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let upvar_field_projection = self.is_upvar_field_projection(place);
|
2018-07-20 17:30:31 +01:00
|
|
|
if let Some(field) = upvar_field_projection {
|
2017-12-06 20:27:38 +02:00
|
|
|
let var_index = field.index();
|
2021-03-17 02:51:27 -04:00
|
|
|
let name = self.upvars[var_index].place.to_string(self.infcx.tcx);
|
2018-05-16 15:38:32 +03:00
|
|
|
if self.upvars[var_index].by_ref {
|
2017-12-06 20:27:38 +02:00
|
|
|
buf.push_str(&name);
|
|
|
|
} else {
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push('*');
|
|
|
|
buf.push_str(&name);
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if autoderef {
|
2019-04-30 18:58:24 +02:00
|
|
|
// FIXME turn this recursion into iteration
|
2018-06-22 00:10:52 -03:00
|
|
|
self.append_place_to_string(
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: proj_base },
|
2018-06-22 00:10:52 -03:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2017-12-06 20:27:38 +02:00
|
|
|
} else {
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push('*');
|
2019-12-11 16:50:03 -03:00
|
|
|
self.append_place_to_string(
|
|
|
|
PlaceRef { local, projection: proj_base },
|
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::Downcast(..) => {
|
2018-06-22 00:10:52 -03:00
|
|
|
self.append_place_to_string(
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: proj_base },
|
2018-06-22 00:10:52 -03:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2018-06-20 23:51:18 -03:00
|
|
|
if including_downcast.0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
ProjectionElem::Field(field, _ty) => {
|
|
|
|
autoderef = true;
|
|
|
|
|
2021-01-12 14:54:12 -05:00
|
|
|
// FIXME(project-rfc_2229#36): print capture precisely here.
|
2019-12-22 17:42:04 -05:00
|
|
|
let upvar_field_projection = self.is_upvar_field_projection(place);
|
2018-07-20 17:30:31 +01:00
|
|
|
if let Some(field) = upvar_field_projection {
|
2017-12-06 20:27:38 +02:00
|
|
|
let var_index = field.index();
|
2021-03-17 02:51:27 -04:00
|
|
|
let name = self.upvars[var_index].place.to_string(self.infcx.tcx);
|
2017-12-06 20:27:38 +02:00
|
|
|
buf.push_str(&name);
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
let field_name = self
|
2019-12-11 16:50:03 -03:00
|
|
|
.describe_field(PlaceRef { local, projection: proj_base }, *field);
|
2018-06-22 00:10:52 -03:00
|
|
|
self.append_place_to_string(
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: proj_base },
|
2018-06-22 00:10:52 -03:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push('.');
|
|
|
|
buf.push_str(&field_name);
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::Index(index) => {
|
|
|
|
autoderef = true;
|
|
|
|
|
2018-06-22 00:10:52 -03:00
|
|
|
self.append_place_to_string(
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: proj_base },
|
2018-06-22 00:10:52 -03:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push('[');
|
2019-07-30 00:07:28 +02:00
|
|
|
if self.append_local_to_string(*index, buf).is_err() {
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push('_');
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push(']');
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
|
|
|
|
autoderef = true;
|
|
|
|
// Since it isn't possible to borrow an element on a particular index and
|
|
|
|
// then use another while the borrow is held, don't output indices details
|
|
|
|
// to avoid confusing the end-user
|
2018-06-22 00:10:52 -03:00
|
|
|
self.append_place_to_string(
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: proj_base },
|
2018-06-22 00:10:52 -03:00
|
|
|
buf,
|
|
|
|
autoderef,
|
|
|
|
&including_downcast,
|
|
|
|
)?;
|
2020-09-10 13:57:40 +02:00
|
|
|
buf.push_str("[..]");
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-10-11 01:19:55 +02:00
|
|
|
/// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
|
2019-05-03 22:24:52 +01:00
|
|
|
/// a name, or its name was generated by the compiler, then `Err` is returned
|
2018-05-16 18:58:54 +03:00
|
|
|
fn append_local_to_string(&self, local: Local, buf: &mut String) -> Result<(), ()> {
|
2019-11-06 12:00:46 -05:00
|
|
|
let decl = &self.body.local_decls[local];
|
2018-05-16 18:58:54 +03:00
|
|
|
match self.local_names[local] {
|
|
|
|
Some(name) if !decl.from_compiler_desugaring() => {
|
2021-12-15 14:39:23 +11:00
|
|
|
buf.push_str(name.as_str());
|
2017-12-06 20:27:38 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-05-03 22:24:52 +01:00
|
|
|
_ => Err(()),
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 01:19:55 +02:00
|
|
|
/// End-user visible description of the `field`nth field of `base`
|
2020-03-04 18:25:03 -03:00
|
|
|
fn describe_field(&self, place: PlaceRef<'tcx>, field: Field) -> String {
|
2019-04-30 18:58:24 +02:00
|
|
|
// FIXME Place2 Make this work iteratively
|
|
|
|
match place {
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [] } => {
|
2020-01-14 02:10:05 -03:00
|
|
|
let local = &self.body.local_decls[local];
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(local.ty, field, None)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
|
2019-07-30 00:07:28 +02:00
|
|
|
ProjectionElem::Deref => {
|
2019-12-11 16:50:03 -03:00
|
|
|
self.describe_field(PlaceRef { local, projection: proj_base }, field)
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2019-03-28 18:00:17 -07:00
|
|
|
ProjectionElem::Downcast(_, variant_index) => {
|
2021-01-09 23:33:38 -06:00
|
|
|
let base_ty = place.ty(self.body, self.infcx.tcx).ty;
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(base_ty, field, Some(*variant_index))
|
2019-03-28 18:00:17 -07:00
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
ProjectionElem::Field(_, field_type) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(*field_type, field, None)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2018-03-01 16:43:03 -03:00
|
|
|
ProjectionElem::Index(..)
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. } => {
|
2019-12-11 16:50:03 -03:00
|
|
|
self.describe_field(PlaceRef { local, projection: proj_base }, field)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 01:19:55 +02:00
|
|
|
/// End-user visible description of the `field_index`nth field of `ty`
|
2019-03-28 18:00:17 -07:00
|
|
|
fn describe_field_from_ty(
|
|
|
|
&self,
|
2019-04-26 14:26:49 +02:00
|
|
|
ty: Ty<'_>,
|
2019-03-28 18:00:17 -07:00
|
|
|
field: Field,
|
2019-12-22 17:42:04 -05:00
|
|
|
variant_index: Option<VariantIdx>,
|
2019-03-28 18:00:17 -07:00
|
|
|
) -> String {
|
2017-12-06 20:27:38 +02:00
|
|
|
if ty.is_box() {
|
|
|
|
// If the type is a box, the field is described from the boxed type
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index)
|
2017-12-06 20:27:38 +02:00
|
|
|
} else {
|
2020-08-03 00:49:11 +02:00
|
|
|
match *ty.kind() {
|
2019-03-28 18:00:17 -07:00
|
|
|
ty::Adt(def, _) => {
|
|
|
|
let variant = if let Some(idx) = variant_index {
|
|
|
|
assert!(def.is_enum());
|
|
|
|
&def.variants[idx]
|
|
|
|
} else {
|
|
|
|
def.non_enum_variant()
|
|
|
|
};
|
2022-01-02 22:37:05 -05:00
|
|
|
variant.fields[field.index()].name.to_string()
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Tuple(_) => field.index().to_string(),
|
|
|
|
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(ty, field, variant_index)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
self.describe_field_from_ty(ty, field, variant_index)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
|
2020-09-26 17:07:00 -04:00
|
|
|
// We won't be borrowck'ing here if the closure came from another crate,
|
|
|
|
// so it's safe to call `expect_local`.
|
|
|
|
//
|
|
|
|
// We know the field exists so it's safe to call operator[] and `unwrap` here.
|
2020-12-23 15:38:22 -05:00
|
|
|
let var_id = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.typeck(def_id.expect_local())
|
|
|
|
.closure_min_captures_flattened(def_id)
|
|
|
|
.nth(field.index())
|
|
|
|
.unwrap()
|
|
|
|
.get_root_variable();
|
2017-12-06 20:27:38 +02:00
|
|
|
|
2019-06-19 15:44:51 +02:00
|
|
|
self.infcx.tcx.hir().name(var_id).to_string()
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Might need a revision when the fields in trait RFC is implemented
|
|
|
|
// (https://github.com/rust-lang/rfcs/pull/1546)
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!("End-user description not implemented for field access on `{:?}`", ty);
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 22:28:22 +01:00
|
|
|
/// Add a note that a type does not implement `Copy`
|
|
|
|
pub(super) fn note_type_does_not_implement_copy(
|
|
|
|
&self,
|
2021-12-15 08:38:12 +01:00
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
2019-05-04 22:28:22 +01:00
|
|
|
place_desc: &str,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
span: Option<Span>,
|
2020-08-08 13:16:43 -04:00
|
|
|
move_prefix: &str,
|
2019-05-04 22:28:22 +01:00
|
|
|
) {
|
|
|
|
let message = format!(
|
2020-08-08 13:16:43 -04:00
|
|
|
"{}move occurs because {} has type `{}`, which does not implement the `Copy` trait",
|
|
|
|
move_prefix, place_desc, ty,
|
2019-05-04 22:28:22 +01:00
|
|
|
);
|
|
|
|
if let Some(span) = span {
|
|
|
|
err.span_label(span, message);
|
|
|
|
} else {
|
|
|
|
err.note(&message);
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 14:00:59 +01:00
|
|
|
|
|
|
|
pub(super) fn borrowed_content_source(
|
|
|
|
&self,
|
2020-03-04 18:25:03 -03:00
|
|
|
deref_base: PlaceRef<'tcx>,
|
2019-07-07 14:00:59 +01:00
|
|
|
) -> BorrowedContentSource<'tcx> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
|
|
|
// Look up the provided place and work out the move path index for it,
|
|
|
|
// we'll use this to check whether it was originally from an overloaded
|
|
|
|
// operator.
|
2019-07-19 20:53:31 +02:00
|
|
|
match self.move_data.rev_lookup.find(deref_base) {
|
2019-07-07 14:00:59 +01:00
|
|
|
LookupResult::Exact(mpi) | LookupResult::Parent(Some(mpi)) => {
|
|
|
|
debug!("borrowed_content_source: mpi={:?}", mpi);
|
|
|
|
|
|
|
|
for i in &self.move_data.init_path_map[mpi] {
|
|
|
|
let init = &self.move_data.inits[*i];
|
|
|
|
debug!("borrowed_content_source: init={:?}", init);
|
|
|
|
// We're only interested in statements that initialized a value, not the
|
|
|
|
// initializations from arguments.
|
|
|
|
let loc = match init.location {
|
|
|
|
InitLocation::Statement(stmt) => stmt,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let bbd = &self.body[loc.block];
|
2019-07-07 14:00:59 +01:00
|
|
|
let is_terminator = bbd.statements.len() == loc.statement_index;
|
|
|
|
debug!(
|
|
|
|
"borrowed_content_source: loc={:?} is_terminator={:?}",
|
2019-12-22 17:42:04 -05:00
|
|
|
loc, is_terminator,
|
2019-07-07 14:00:59 +01:00
|
|
|
);
|
|
|
|
if !is_terminator {
|
|
|
|
continue;
|
|
|
|
} else if let Some(Terminator {
|
2019-12-22 17:42:04 -05:00
|
|
|
kind: TerminatorKind::Call { ref func, from_hir_call: false, .. },
|
2019-07-07 14:00:59 +01:00
|
|
|
..
|
2019-12-22 17:42:04 -05:00
|
|
|
}) = bbd.terminator
|
|
|
|
{
|
|
|
|
if let Some(source) =
|
2020-04-12 10:28:41 -07:00
|
|
|
BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-07-07 14:00:59 +01:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Base is a `static` so won't be from an overloaded operator
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we didn't find an overloaded deref or index, then assume it's a
|
|
|
|
// built in deref and check the type of the base.
|
2021-01-09 23:33:38 -06:00
|
|
|
let base_ty = deref_base.ty(self.body, tcx).ty;
|
2019-07-07 14:00:59 +01:00
|
|
|
if base_ty.is_unsafe_ptr() {
|
|
|
|
BorrowedContentSource::DerefRawPointer
|
2019-07-25 00:57:33 +02:00
|
|
|
} else if base_ty.is_mutable_ptr() {
|
2019-07-07 14:00:59 +01:00
|
|
|
BorrowedContentSource::DerefMutableRef
|
|
|
|
} else {
|
|
|
|
BorrowedContentSource::DerefSharedRef
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 13:07:01 +02:00
|
|
|
}
|
2018-09-14 15:44:45 +02:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
2018-09-18 21:26:45 +02:00
|
|
|
/// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
|
|
|
|
/// name where required.
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
|
2019-01-11 13:07:01 +02:00
|
|
|
let mut s = String::new();
|
2019-01-25 12:11:50 +02:00
|
|
|
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
|
2019-01-11 13:07:01 +02:00
|
|
|
|
2018-09-14 15:44:45 +02:00
|
|
|
// We need to add synthesized lifetimes where appropriate. We do
|
|
|
|
// this by hooking into the pretty printer and telling it to label the
|
|
|
|
// lifetimes without names with the value `'0`.
|
2020-08-03 00:49:11 +02:00
|
|
|
match ty.kind() {
|
2020-04-16 17:38:52 -07:00
|
|
|
ty::Ref(
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::RegionKind::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
2020-04-16 17:38:52 -07:00
|
|
|
| ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }),
|
2018-09-27 17:57:35 -04:00
|
|
|
_,
|
|
|
|
_,
|
2019-01-11 13:07:01 +02:00
|
|
|
) => printer.region_highlight_mode.highlighting_bound_region(*br, counter),
|
|
|
|
_ => {}
|
2018-09-14 15:44:45 +02:00
|
|
|
}
|
2019-01-11 13:07:01 +02:00
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
let _ = ty.print(printer);
|
2019-01-11 13:07:01 +02:00
|
|
|
s
|
2018-09-14 15:44:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns the name of the provided `Ty` (that must be a reference)'s region with a
|
2018-09-18 21:26:45 +02:00
|
|
|
/// synthesized lifetime name where required.
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
|
2019-01-11 13:07:01 +02:00
|
|
|
let mut s = String::new();
|
2019-01-25 12:11:50 +02:00
|
|
|
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
|
2019-01-11 13:07:01 +02:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let region = match ty.kind() {
|
2019-04-01 00:02:46 +02:00
|
|
|
ty::Ref(region, _, _) => {
|
2019-01-11 13:07:01 +02:00
|
|
|
match region {
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::RegionKind::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
2019-01-11 13:07:01 +02:00
|
|
|
| ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
|
|
|
|
printer.region_highlight_mode.highlighting_bound_region(*br, counter)
|
|
|
|
}
|
|
|
|
_ => {}
|
2018-09-27 19:10:29 -07:00
|
|
|
}
|
2019-01-11 13:07:01 +02:00
|
|
|
|
|
|
|
region
|
|
|
|
}
|
2018-09-14 15:44:45 +02:00
|
|
|
_ => bug!("ty for annotation of borrow region is not a reference"),
|
2019-01-11 13:07:01 +02:00
|
|
|
};
|
|
|
|
|
2019-01-25 12:11:50 +02:00
|
|
|
let _ = region.print(printer);
|
2019-01-11 13:07:01 +02:00
|
|
|
s
|
2018-09-14 15:44:45 +02:00
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2018-08-01 20:38:02 +01:00
|
|
|
|
2020-06-11 13:48:46 -04:00
|
|
|
/// The span(s) associated to a use of a place.
|
2018-08-01 20:38:02 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2020-07-25 07:04:13 -04:00
|
|
|
pub(super) enum UseSpans<'tcx> {
|
2020-06-11 13:48:46 -04:00
|
|
|
/// The access is caused by capturing a variable for a closure.
|
2018-08-01 20:38:02 +01:00
|
|
|
ClosureUse {
|
2020-06-11 13:48:46 -04:00
|
|
|
/// This is true if the captured variable was from a generator.
|
2019-10-10 22:20:57 +08:00
|
|
|
generator_kind: Option<GeneratorKind>,
|
2020-06-11 13:48:46 -04:00
|
|
|
/// The span of the args of the closure, including the `move` keyword if
|
|
|
|
/// it's present.
|
2018-08-01 20:38:02 +01:00
|
|
|
args_span: Span,
|
2021-03-17 02:51:27 -04:00
|
|
|
/// The span of the use resulting in capture kind
|
|
|
|
/// Check `ty::CaptureInfo` for more details
|
|
|
|
capture_kind_span: Span,
|
|
|
|
/// The span of the use resulting in the captured path
|
|
|
|
/// Check `ty::CaptureInfo` for more details
|
|
|
|
path_span: Span,
|
2020-06-11 13:48:46 -04:00
|
|
|
},
|
|
|
|
/// The access is caused by using a variable as the receiver of a method
|
|
|
|
/// that takes 'self'
|
|
|
|
FnSelfUse {
|
|
|
|
/// The span of the variable being moved
|
2018-08-30 15:06:27 -03:00
|
|
|
var_span: Span,
|
2020-06-11 13:48:46 -04:00
|
|
|
/// The span of the method call on the variable
|
|
|
|
fn_call_span: Span,
|
|
|
|
/// The definition span of the method being called
|
|
|
|
fn_span: Span,
|
2021-12-09 22:42:17 +08:00
|
|
|
kind: CallKind<'tcx>,
|
2018-08-01 20:38:02 +01:00
|
|
|
},
|
2020-06-19 22:45:09 -07:00
|
|
|
/// This access is caused by a `match` or `if let` pattern.
|
|
|
|
PatUse(Span),
|
|
|
|
/// This access has a single span associated to it: common case.
|
2018-08-01 20:38:02 +01:00
|
|
|
OtherUse(Span),
|
|
|
|
}
|
|
|
|
|
2020-07-25 07:04:13 -04:00
|
|
|
impl UseSpans<'_> {
|
2018-08-01 20:38:02 +01:00
|
|
|
pub(super) fn args_or_use(self) -> Span {
|
|
|
|
match self {
|
2020-06-19 22:45:09 -07:00
|
|
|
UseSpans::ClosureUse { args_span: span, .. }
|
|
|
|
| UseSpans::PatUse(span)
|
|
|
|
| UseSpans::OtherUse(span) => span,
|
2021-12-09 22:42:17 +08:00
|
|
|
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
|
|
|
|
fn_call_span
|
|
|
|
}
|
2020-07-25 07:04:13 -04:00
|
|
|
UseSpans::FnSelfUse { var_span, .. } => var_span,
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:53:59 -04:00
|
|
|
/// Returns the span of `self`, in the case of a `ClosureUse` returns the `path_span`
|
2021-03-17 02:51:27 -04:00
|
|
|
pub(super) fn var_or_use_path_span(self) -> Span {
|
|
|
|
match self {
|
|
|
|
UseSpans::ClosureUse { path_span: span, .. }
|
|
|
|
| UseSpans::PatUse(span)
|
|
|
|
| UseSpans::OtherUse(span) => span,
|
2021-12-09 22:42:17 +08:00
|
|
|
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
|
|
|
|
fn_call_span
|
|
|
|
}
|
2021-03-17 02:51:27 -04:00
|
|
|
UseSpans::FnSelfUse { var_span, .. } => var_span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:53:59 -04:00
|
|
|
/// Returns the span of `self`, in the case of a `ClosureUse` returns the `capture_kind_span`
|
2018-08-01 20:38:02 +01:00
|
|
|
pub(super) fn var_or_use(self) -> Span {
|
|
|
|
match self {
|
2021-03-17 02:51:27 -04:00
|
|
|
UseSpans::ClosureUse { capture_kind_span: span, .. }
|
2020-06-19 22:45:09 -07:00
|
|
|
| UseSpans::PatUse(span)
|
|
|
|
| UseSpans::OtherUse(span) => span,
|
2021-12-09 22:42:17 +08:00
|
|
|
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
|
|
|
|
fn_call_span
|
|
|
|
}
|
2020-07-25 07:04:13 -04:00
|
|
|
UseSpans::FnSelfUse { var_span, .. } => var_span,
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:20:57 +08:00
|
|
|
pub(super) fn generator_kind(self) -> Option<GeneratorKind> {
|
|
|
|
match self {
|
|
|
|
UseSpans::ClosureUse { generator_kind, .. } => generator_kind,
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:38:02 +01:00
|
|
|
// Add a span label to the arguments of the closure, if it exists.
|
2019-02-08 06:28:15 +09:00
|
|
|
pub(super) fn args_span_label(
|
|
|
|
self,
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
|
|
|
message: impl Into<String>,
|
|
|
|
) {
|
2018-08-01 20:38:02 +01:00
|
|
|
if let UseSpans::ClosureUse { args_span, .. } = self {
|
|
|
|
err.span_label(args_span, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
// Add a span label to the use of the captured variable, if it exists.
|
|
|
|
// only adds label to the `path_span`
|
|
|
|
pub(super) fn var_span_label_path_only(
|
|
|
|
self,
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
|
|
|
message: impl Into<String>,
|
|
|
|
) {
|
|
|
|
if let UseSpans::ClosureUse { path_span, .. } = self {
|
|
|
|
err.span_label(path_span, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:38:02 +01:00
|
|
|
// Add a span label to the use of the captured variable, if it exists.
|
2019-02-08 06:28:15 +09:00
|
|
|
pub(super) fn var_span_label(
|
|
|
|
self,
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
|
|
|
message: impl Into<String>,
|
2021-03-17 02:51:27 -04:00
|
|
|
kind_desc: impl Into<String>,
|
2019-02-08 06:28:15 +09:00
|
|
|
) {
|
2021-03-17 02:51:27 -04:00
|
|
|
if let UseSpans::ClosureUse { capture_kind_span, path_span, .. } = self {
|
|
|
|
if capture_kind_span == path_span {
|
|
|
|
err.span_label(capture_kind_span, message);
|
|
|
|
} else {
|
|
|
|
let capture_kind_label =
|
|
|
|
format!("capture is {} because of use here", kind_desc.into());
|
|
|
|
let path_label = message;
|
|
|
|
err.span_label(capture_kind_span, capture_kind_label);
|
|
|
|
err.span_label(path_span, path_label);
|
|
|
|
}
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `false` if this place is not used in a closure.
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) fn for_closure(&self) -> bool {
|
2018-10-10 21:56:17 +02:00
|
|
|
match *self {
|
2019-10-10 22:20:57 +08:00
|
|
|
UseSpans::ClosureUse { generator_kind, .. } => generator_kind.is_none(),
|
2018-10-10 21:56:17 +02:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `false` if this place is not used in a generator.
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) fn for_generator(&self) -> bool {
|
2018-10-10 21:56:17 +02:00
|
|
|
match *self {
|
2019-10-10 22:20:57 +08:00
|
|
|
UseSpans::ClosureUse { generator_kind, .. } => generator_kind.is_some(),
|
2018-10-10 21:56:17 +02:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Describe the span associated with a use of a place.
|
2019-05-04 08:40:07 +01:00
|
|
|
pub(super) fn describe(&self) -> String {
|
2018-10-10 21:56:17 +02:00
|
|
|
match *self {
|
2019-12-22 17:42:04 -05:00
|
|
|
UseSpans::ClosureUse { generator_kind, .. } => {
|
|
|
|
if generator_kind.is_some() {
|
|
|
|
" in generator".to_string()
|
|
|
|
} else {
|
|
|
|
" in closure".to_string()
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 13:57:40 +02:00
|
|
|
_ => String::new(),
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn or_else<F>(self, if_other: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Self,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
closure @ UseSpans::ClosureUse { .. } => closure,
|
2020-06-19 22:45:09 -07:00
|
|
|
UseSpans::PatUse(_) | UseSpans::OtherUse(_) => if_other(),
|
2020-06-11 13:48:46 -04:00
|
|
|
fn_self @ UseSpans::FnSelfUse { .. } => fn_self,
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 14:00:59 +01:00
|
|
|
pub(super) enum BorrowedContentSource<'tcx> {
|
|
|
|
DerefRawPointer,
|
|
|
|
DerefMutableRef,
|
|
|
|
DerefSharedRef,
|
|
|
|
OverloadedDeref(Ty<'tcx>),
|
|
|
|
OverloadedIndex(Ty<'tcx>),
|
|
|
|
}
|
|
|
|
|
2021-12-15 08:38:12 +01:00
|
|
|
impl<'tcx> BorrowedContentSource<'tcx> {
|
2020-04-08 04:26:48 +03:00
|
|
|
pub(super) fn describe_for_unnamed_place(&self, tcx: TyCtxt<'_>) -> String {
|
2019-07-07 14:00:59 +01:00
|
|
|
match *self {
|
2020-02-29 00:35:24 +01:00
|
|
|
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
|
|
|
|
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
|
|
|
|
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
|
2021-11-16 13:12:38 -06:00
|
|
|
BorrowedContentSource::OverloadedDeref(ty) => ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
|
|
|
|
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
|
2019-07-07 14:00:59 +01:00
|
|
|
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn describe_for_named_place(&self) -> Option<&'static str> {
|
|
|
|
match *self {
|
|
|
|
BorrowedContentSource::DerefRawPointer => Some("raw pointer"),
|
|
|
|
BorrowedContentSource::DerefSharedRef => Some("shared reference"),
|
|
|
|
BorrowedContentSource::DerefMutableRef => Some("mutable reference"),
|
|
|
|
// Overloaded deref and index operators should be evaluated into a
|
|
|
|
// temporary. So we don't need a description here.
|
|
|
|
BorrowedContentSource::OverloadedDeref(_)
|
2019-12-22 17:42:04 -05:00
|
|
|
| BorrowedContentSource::OverloadedIndex(_) => None,
|
2019-07-07 14:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 04:26:48 +03:00
|
|
|
pub(super) fn describe_for_immutable_place(&self, tcx: TyCtxt<'_>) -> String {
|
2019-07-07 14:16:58 +01:00
|
|
|
match *self {
|
2020-02-29 00:35:24 +01:00
|
|
|
BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
|
|
|
|
BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
|
2019-07-07 14:16:58 +01:00
|
|
|
BorrowedContentSource::DerefMutableRef => {
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
|
|
|
|
}
|
2021-11-16 13:12:38 -06:00
|
|
|
BorrowedContentSource::OverloadedDeref(ty) => ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
|
|
|
|
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
|
2019-07-07 14:16:58 +01:00
|
|
|
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 14:00:59 +01:00
|
|
|
fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
|
2020-08-03 00:49:11 +02:00
|
|
|
match *func.kind() {
|
2019-07-07 14:00:59 +01:00
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let trait_id = tcx.trait_of_item(def_id)?;
|
|
|
|
|
|
|
|
let lang_items = tcx.lang_items();
|
|
|
|
if Some(trait_id) == lang_items.deref_trait()
|
|
|
|
|| Some(trait_id) == lang_items.deref_mut_trait()
|
|
|
|
{
|
|
|
|
Some(BorrowedContentSource::OverloadedDeref(substs.type_at(0)))
|
|
|
|
} else if Some(trait_id) == lang_items.index_trait()
|
|
|
|
|| Some(trait_id) == lang_items.index_mut_trait()
|
|
|
|
{
|
|
|
|
Some(BorrowedContentSource::OverloadedIndex(substs.type_at(0)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
2018-08-01 20:38:02 +01:00
|
|
|
/// Finds the spans associated to a move or copy of move_place at location.
|
|
|
|
pub(super) fn move_spans(
|
|
|
|
&self,
|
2020-03-04 18:25:03 -03:00
|
|
|
moved_place: PlaceRef<'tcx>, // Could also be an upvar.
|
2018-08-01 20:38:02 +01:00
|
|
|
location: Location,
|
2020-07-25 07:04:13 -04:00
|
|
|
) -> UseSpans<'tcx> {
|
2018-08-01 20:38:02 +01:00
|
|
|
use self::UseSpans::*;
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let stmt = match self.body[location.block].statements.get(location.statement_index) {
|
2018-08-01 20:38:02 +01:00
|
|
|
Some(stmt) => stmt,
|
2019-11-06 12:00:46 -05:00
|
|
|
None => return OtherUse(self.body.source_info(location).span),
|
2018-08-01 20:38:02 +01:00
|
|
|
};
|
|
|
|
|
2018-10-10 21:56:17 +02:00
|
|
|
debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
|
2019-12-22 17:42:04 -05:00
|
|
|
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) = stmt.kind {
|
2020-06-11 13:48:46 -04:00
|
|
|
match kind {
|
2019-10-10 22:20:57 +08:00
|
|
|
box AggregateKind::Closure(def_id, _)
|
2020-06-11 13:48:46 -04:00
|
|
|
| box AggregateKind::Generator(def_id, _, _) => {
|
|
|
|
debug!("move_spans: def_id={:?} places={:?}", def_id, places);
|
2021-03-17 02:51:27 -04:00
|
|
|
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
2020-06-11 13:48:46 -04:00
|
|
|
self.closure_span(*def_id, moved_place, places)
|
|
|
|
{
|
2021-03-17 02:51:27 -04:00
|
|
|
return ClosureUse {
|
|
|
|
generator_kind,
|
|
|
|
args_span,
|
|
|
|
capture_kind_span,
|
|
|
|
path_span,
|
|
|
|
};
|
2021-03-25 23:03:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatementKind::FakeRead only contains a def_id if they are introduced as a result
|
|
|
|
// of pattern matching within a closure.
|
2021-03-29 22:48:44 -04:00
|
|
|
if let StatementKind::FakeRead(box (cause, ref place)) = stmt.kind {
|
2021-03-25 23:03:12 -04:00
|
|
|
match cause {
|
|
|
|
FakeReadCause::ForMatchedPlace(Some(closure_def_id))
|
|
|
|
| FakeReadCause::ForLet(Some(closure_def_id)) => {
|
|
|
|
debug!("move_spans: def_id={:?} place={:?}", closure_def_id, place);
|
|
|
|
let places = &[Operand::Move(*place)];
|
2021-03-17 02:51:27 -04:00
|
|
|
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
2021-03-25 23:03:12 -04:00
|
|
|
self.closure_span(closure_def_id, moved_place, places)
|
|
|
|
{
|
2021-03-17 02:51:27 -04:00
|
|
|
return ClosureUse {
|
|
|
|
generator_kind,
|
|
|
|
args_span,
|
|
|
|
capture_kind_span,
|
|
|
|
path_span,
|
|
|
|
};
|
2020-06-11 13:48:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let normal_ret =
|
|
|
|
if moved_place.projection.iter().any(|p| matches!(p, ProjectionElem::Downcast(..))) {
|
|
|
|
PatUse(stmt.source_info.span)
|
|
|
|
} else {
|
|
|
|
OtherUse(stmt.source_info.span)
|
2020-06-21 16:28:19 -04:00
|
|
|
};
|
2020-06-11 13:48:46 -04:00
|
|
|
|
2020-06-11 13:48:46 -04:00
|
|
|
// We are trying to find MIR of the form:
|
|
|
|
// ```
|
|
|
|
// _temp = _moved_val;
|
|
|
|
// ...
|
|
|
|
// FnSelfCall(_temp, ...)
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// where `_moved_val` is the place we generated the move error for,
|
|
|
|
// `_temp` is some other local, and `FnSelfCall` is a function
|
|
|
|
// that has a `self` parameter.
|
|
|
|
|
|
|
|
let target_temp = match stmt.kind {
|
|
|
|
StatementKind::Assign(box (temp, _)) if temp.as_local().is_some() => {
|
|
|
|
temp.as_local().unwrap()
|
2020-06-11 13:48:46 -04:00
|
|
|
}
|
2020-06-11 13:48:46 -04:00
|
|
|
_ => return normal_ret,
|
|
|
|
};
|
2020-06-21 16:28:19 -04:00
|
|
|
|
2020-06-11 13:48:46 -04:00
|
|
|
debug!("move_spans: target_temp = {:?}", target_temp);
|
|
|
|
|
|
|
|
if let Some(Terminator {
|
2020-08-10 07:16:30 -04:00
|
|
|
kind: TerminatorKind::Call { fn_span, from_hir_call, .. }, ..
|
2020-06-11 13:48:46 -04:00
|
|
|
}) = &self.body[location.block].terminator
|
|
|
|
{
|
2021-01-05 20:08:11 +01:00
|
|
|
let (method_did, method_substs) = if let Some(info) =
|
|
|
|
rustc_const_eval::util::find_self_call(
|
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
target_temp,
|
|
|
|
location.block,
|
|
|
|
) {
|
2020-07-25 07:04:13 -04:00
|
|
|
info
|
2020-08-10 07:16:30 -04:00
|
|
|
} else {
|
|
|
|
return normal_ret;
|
|
|
|
};
|
2020-06-11 13:48:46 -04:00
|
|
|
|
2021-12-09 22:42:17 +08:00
|
|
|
let kind = call_kind(
|
|
|
|
self.infcx.tcx,
|
|
|
|
self.param_env,
|
|
|
|
method_did,
|
|
|
|
method_substs,
|
|
|
|
*fn_span,
|
|
|
|
*from_hir_call,
|
|
|
|
Some(self.infcx.tcx.fn_arg_names(method_did)[0]),
|
2020-07-25 07:04:13 -04:00
|
|
|
);
|
|
|
|
|
2020-08-10 07:16:30 -04:00
|
|
|
return FnSelfUse {
|
|
|
|
var_span: stmt.source_info.span,
|
2021-12-09 22:42:17 +08:00
|
|
|
fn_call_span: *fn_span,
|
2020-08-10 07:16:30 -04:00
|
|
|
fn_span: self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.sess
|
|
|
|
.source_map()
|
|
|
|
.guess_head_span(self.infcx.tcx.def_span(method_did)),
|
|
|
|
kind,
|
|
|
|
};
|
2020-06-19 22:45:09 -07:00
|
|
|
}
|
2020-08-08 00:39:38 +02:00
|
|
|
normal_ret
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the span of arguments of a closure (within `maybe_closure_span`)
|
|
|
|
/// and its usage of the local assigned at `location`.
|
|
|
|
/// This is done by searching in statements succeeding `location`
|
|
|
|
/// and originating from `maybe_closure_span`.
|
2020-07-25 07:04:13 -04:00
|
|
|
pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans<'tcx> {
|
2018-08-01 20:38:02 +01:00
|
|
|
use self::UseSpans::*;
|
2018-10-10 21:56:17 +02:00
|
|
|
debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
|
2018-08-01 20:38:02 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let target = match self.body[location.block].statements.get(location.statement_index) {
|
|
|
|
Some(&Statement { kind: StatementKind::Assign(box (ref place, _)), .. }) => {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
local
|
|
|
|
} else {
|
|
|
|
return OtherUse(use_span);
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 20:38:02 +01:00
|
|
|
_ => return OtherUse(use_span),
|
|
|
|
};
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
if self.body.local_kind(target) != LocalKind::Temp {
|
2018-08-01 20:38:02 +01:00
|
|
|
// operands are always temporaries.
|
|
|
|
return OtherUse(use_span);
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
for stmt in &self.body[location.block].statements[location.statement_index + 1..] {
|
2019-12-22 17:42:04 -05:00
|
|
|
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) =
|
|
|
|
stmt.kind
|
|
|
|
{
|
2018-10-10 21:56:17 +02:00
|
|
|
let (def_id, is_generator) = match kind {
|
|
|
|
box AggregateKind::Closure(def_id, _) => (def_id, false),
|
|
|
|
box AggregateKind::Generator(def_id, _, _) => (def_id, true),
|
|
|
|
_ => continue,
|
|
|
|
};
|
2018-08-01 20:38:02 +01:00
|
|
|
|
2018-10-10 21:56:17 +02:00
|
|
|
debug!(
|
|
|
|
"borrow_spans: def_id={:?} is_generator={:?} places={:?}",
|
|
|
|
def_id, is_generator, places
|
|
|
|
);
|
2021-03-17 02:51:27 -04:00
|
|
|
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
2019-12-22 17:42:04 -05:00
|
|
|
self.closure_span(*def_id, Place::from(target).as_ref(), places)
|
|
|
|
{
|
2021-03-17 02:51:27 -04:00
|
|
|
return ClosureUse { generator_kind, args_span, capture_kind_span, path_span };
|
2018-10-10 21:56:17 +02:00
|
|
|
} else {
|
|
|
|
return OtherUse(use_span);
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if use_span != stmt.source_info.span {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OtherUse(use_span)
|
|
|
|
}
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
/// Finds the spans of a captured place within a closure or generator.
|
|
|
|
/// The first span is the location of the use resulting in the capture kind of the capture
|
|
|
|
/// The second span is the location the use resulting in the captured path of the capture
|
2018-10-10 21:56:17 +02:00
|
|
|
fn closure_span(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
2020-03-04 18:25:03 -03:00
|
|
|
target_place: PlaceRef<'tcx>,
|
2020-12-30 12:59:07 +01:00
|
|
|
places: &[Operand<'tcx>],
|
2021-03-17 02:51:27 -04:00
|
|
|
) -> Option<(Span, Option<GeneratorKind>, Span, Span)> {
|
2018-10-10 21:56:17 +02:00
|
|
|
debug!(
|
|
|
|
"closure_span: def_id={:?} target_place={:?} places={:?}",
|
|
|
|
def_id, target_place, places
|
|
|
|
);
|
2020-08-25 23:58:58 -04:00
|
|
|
let local_did = def_id.as_local()?;
|
|
|
|
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(local_did);
|
2019-09-26 14:39:48 +01:00
|
|
|
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
|
2019-03-04 09:00:30 +01:00
|
|
|
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
|
2019-12-22 17:42:04 -05:00
|
|
|
if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr {
|
2021-03-17 02:51:27 -04:00
|
|
|
for (captured_place, place) in self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
|
|
|
.typeck(def_id.expect_local())
|
|
|
|
.closure_min_captures_flattened(def_id)
|
|
|
|
.zip(places)
|
|
|
|
{
|
2019-05-04 03:47:16 +03:00
|
|
|
match place {
|
2019-12-22 17:42:04 -05:00
|
|
|
Operand::Copy(place) | Operand::Move(place)
|
|
|
|
if target_place == place.as_ref() =>
|
|
|
|
{
|
2019-05-04 03:47:16 +03:00
|
|
|
debug!("closure_span: found captured local {:?}", place);
|
2019-10-10 22:20:57 +08:00
|
|
|
let body = self.infcx.tcx.hir().body(*body_id);
|
|
|
|
let generator_kind = body.generator_kind();
|
2020-08-25 23:58:58 -04:00
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
return Some((
|
|
|
|
*args_span,
|
|
|
|
generator_kind,
|
|
|
|
captured_place.get_capture_kind_span(self.infcx.tcx),
|
|
|
|
captured_place.get_path_span(self.infcx.tcx),
|
|
|
|
));
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 21:56:17 +02:00
|
|
|
}
|
2019-05-04 03:47:16 +03:00
|
|
|
None
|
2018-10-10 21:56:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:38:02 +01:00
|
|
|
/// Helper to retrieve span(s) of given borrow from the current MIR
|
|
|
|
/// representation
|
2020-07-25 07:04:13 -04:00
|
|
|
pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData<'_>) -> UseSpans<'tcx> {
|
2019-11-06 12:00:46 -05:00
|
|
|
let span = self.body.source_info(borrow.reserve_location).span;
|
2018-08-01 20:38:02 +01:00
|
|
|
self.borrow_spans(span, borrow.reserve_location)
|
|
|
|
}
|
|
|
|
}
|