2019-11-27 11:39:25 -06:00
|
|
|
//! Borrow checker diagnostics.
|
|
|
|
|
2022-05-06 12:11:42 +08:00
|
|
|
use itertools::Itertools;
|
2022-03-02 04:30:16 +00:00
|
|
|
use rustc_const_eval::util::{call_kind, CallDesugaringKind};
|
|
|
|
use rustc_errors::{Applicability, Diagnostic};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_hir as hir;
|
2022-07-23 00:35:35 +03:00
|
|
|
use rustc_hir::def::{CtorKind, Namespace};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_hir::GeneratorKind;
|
2022-12-25 16:51:11 -08:00
|
|
|
use rustc_infer::infer::{LateBoundRegionConversionTime, TyCtxtInferExt};
|
2022-07-12 09:10:22 -05:00
|
|
|
use rustc_middle::mir::tcx::PlaceTy;
|
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};
|
2022-07-12 09:10:22 -05:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2022-07-23 00:35:35 +03:00
|
|
|
use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2022-12-25 16:51:11 -08:00
|
|
|
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
|
|
|
|
use rustc_trait_selection::traits::{
|
|
|
|
type_known_to_meet_bound_modulo_regions, Obligation, ObligationCause,
|
|
|
|
};
|
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;
|
|
|
|
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;
|
|
|
|
mod explain_borrow;
|
|
|
|
mod move_errors;
|
|
|
|
mod mutability_errors;
|
|
|
|
mod region_errors;
|
2019-11-27 11:39:25 -06:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) use bound_region_errors::{ToUniverseInfo, UniverseInfo};
|
|
|
|
pub(crate) use mutability_errors::AccessKind;
|
|
|
|
pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
|
|
|
|
pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
|
|
|
|
pub(crate) use region_name::{RegionName, RegionNameSource};
|
|
|
|
pub(crate) use rustc_const_eval::util::CallKind;
|
2019-11-27 12:22:17 -06:00
|
|
|
|
2022-07-23 00:35:35 +03:00
|
|
|
pub(super) struct DescribePlaceOpt {
|
|
|
|
pub including_downcast: bool,
|
|
|
|
|
|
|
|
/// Enable/Disable tuple fields.
|
|
|
|
/// For example `x` tuple. if it's `true` `x.0`. Otherwise `x`
|
|
|
|
pub including_tuple_field: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) struct IncludingTupleField(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>,
|
2022-01-23 20:41:46 +00:00
|
|
|
diag: &mut Diagnostic,
|
2022-11-15 19:43:33 -08:00
|
|
|
) -> bool {
|
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-09-11 16:05:45 -03: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 {
|
2022-12-02 19:07:57 +00:00
|
|
|
Operand::Copy(place) | Operand::Move(place)
|
2019-05-27 23:37:18 +02:00
|
|
|
if target == place.local_or_deref_local() =>
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-05-27 23:37:18 +02:00
|
|
|
target = into.local_or_deref_local()
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
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,
|
|
|
|
..
|
|
|
|
} = &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);
|
2022-04-25 22:08:45 +03:00
|
|
|
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
|
2020-08-03 00:49:11 +02:00
|
|
|
let closure = match args.first() {
|
2022-12-02 19:07:57 +00:00
|
|
|
Some(Operand::Copy(place) | Operand::Move(place))
|
2020-08-03 00:49:11 +02:00
|
|
|
if target == place.local_or_deref_local() =>
|
|
|
|
{
|
|
|
|
place.local_or_deref_local().unwrap()
|
|
|
|
}
|
2022-11-15 19:43:33 -08:00
|
|
|
_ => return false,
|
2020-08-03 00:49:11 +02:00
|
|
|
};
|
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
|
|
|
),
|
|
|
|
);
|
2022-11-15 19:43:33 -08:00
|
|
|
return true;
|
2020-08-03 00:49:11 +02:00
|
|
|
}
|
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
|
|
|
),
|
|
|
|
);
|
2022-11-15 19:43:33 -08:00
|
|
|
return true;
|
2018-12-24 15:29:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 19:43:33 -08:00
|
|
|
false
|
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.
|
2022-05-06 12:11:42 +08:00
|
|
|
/// 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> {
|
2022-07-23 00:35:35 +03:00
|
|
|
self.describe_place_with_options(
|
|
|
|
place_ref,
|
|
|
|
DescribePlaceOpt { including_downcast: false, including_tuple_field: true },
|
|
|
|
)
|
2018-06-20 23:51:18 -03:00
|
|
|
}
|
|
|
|
|
2022-05-06 12:11:42 +08: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 `None` if `ProjectionElem` is
|
2018-10-11 01:19:55 +02:00
|
|
|
/// `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>,
|
2022-07-23 00:35:35 +03:00
|
|
|
opt: DescribePlaceOpt,
|
2018-06-22 00:10:52 -03:00
|
|
|
) -> Option<String> {
|
2022-05-06 12:11:42 +08:00
|
|
|
let local = place.local;
|
|
|
|
let mut autoderef_index = None;
|
2017-12-06 20:27:38 +02:00
|
|
|
let mut buf = String::new();
|
2022-05-06 12:11:42 +08:00
|
|
|
let mut ok = self.append_local_to_string(local, &mut buf);
|
2017-12-06 20:27:38 +02:00
|
|
|
|
2022-05-06 12:11:42 +08:00
|
|
|
for (index, elem) in place.projection.into_iter().enumerate() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
if index == 0 {
|
|
|
|
if self.body.local_decls[local].is_ref_for_guard() {
|
|
|
|
continue;
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
if let Some(box LocalInfo::StaticRef { def_id, .. }) =
|
|
|
|
&self.body.local_decls[local].local_info
|
|
|
|
{
|
|
|
|
buf.push_str(self.infcx.tcx.item_name(*def_id).as_str());
|
|
|
|
ok = Ok(());
|
|
|
|
continue;
|
2018-06-20 23:51:18 -03:00
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
|
|
|
|
local,
|
|
|
|
projection: place.projection.split_at(index + 1).0,
|
|
|
|
}) {
|
|
|
|
let var_index = field.index();
|
|
|
|
buf = self.upvars[var_index].place.to_string(self.infcx.tcx);
|
|
|
|
ok = Ok(());
|
|
|
|
if !self.upvars[var_index].by_ref {
|
|
|
|
buf.insert(0, '*');
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
} else {
|
|
|
|
if autoderef_index.is_none() {
|
|
|
|
autoderef_index =
|
|
|
|
match place.projection.into_iter().rev().find_position(|elem| {
|
|
|
|
!matches!(
|
|
|
|
elem,
|
|
|
|
ProjectionElem::Deref | ProjectionElem::Downcast(..)
|
|
|
|
)
|
|
|
|
}) {
|
|
|
|
Some((index, _)) => Some(place.projection.len() - index),
|
|
|
|
None => Some(0),
|
|
|
|
};
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
if index >= autoderef_index.unwrap() {
|
|
|
|
buf.insert(0, '*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 00:35:35 +03:00
|
|
|
ProjectionElem::Downcast(..) if opt.including_downcast => return None,
|
2022-05-06 12:11:42 +08:00
|
|
|
ProjectionElem::Downcast(..) => (),
|
2022-07-27 11:58:34 +00:00
|
|
|
ProjectionElem::OpaqueCast(..) => (),
|
2022-05-06 12:11:42 +08:00
|
|
|
ProjectionElem::Field(field, _ty) => {
|
|
|
|
// FIXME(project-rfc_2229#36): print capture precisely here.
|
|
|
|
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
|
|
|
|
local,
|
|
|
|
projection: place.projection.split_at(index + 1).0,
|
|
|
|
}) {
|
|
|
|
buf = self.upvars[field.index()].place.to_string(self.infcx.tcx);
|
|
|
|
ok = Ok(());
|
|
|
|
} else {
|
|
|
|
let field_name = self.describe_field(
|
|
|
|
PlaceRef { local, projection: place.projection.split_at(index).0 },
|
|
|
|
*field,
|
2022-07-23 00:35:35 +03:00
|
|
|
IncludingTupleField(opt.including_tuple_field),
|
2022-05-06 12:11:42 +08:00
|
|
|
);
|
2022-07-23 00:35:35 +03:00
|
|
|
if let Some(field_name_str) = field_name {
|
|
|
|
buf.push('.');
|
|
|
|
buf.push_str(&field_name_str);
|
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
}
|
|
|
|
ProjectionElem::Index(index) => {
|
|
|
|
buf.push('[');
|
|
|
|
if self.append_local_to_string(*index, &mut buf).is_err() {
|
|
|
|
buf.push('_');
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
buf.push(']');
|
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
|
|
|
|
// 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
|
|
|
|
buf.push_str("[..]");
|
|
|
|
}
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 12:11:42 +08:00
|
|
|
ok.ok().map(|_| buf)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 00:35:35 +03:00
|
|
|
fn describe_name(&self, place: PlaceRef<'tcx>) -> Option<Symbol> {
|
|
|
|
for elem in place.projection.into_iter() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Downcast(Some(name), _) => {
|
|
|
|
return Some(*name);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
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`
|
2022-07-23 00:35:35 +03:00
|
|
|
fn describe_field(
|
|
|
|
&self,
|
|
|
|
place: PlaceRef<'tcx>,
|
|
|
|
field: Field,
|
|
|
|
including_tuple_field: IncludingTupleField,
|
|
|
|
) -> Option<String> {
|
2022-04-25 16:09:36 +08:00
|
|
|
let place_ty = match place {
|
|
|
|
PlaceRef { local, projection: [] } => PlaceTy::from_ty(self.body.local_decls[local].ty),
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
|
2022-04-25 16:09:36 +08:00
|
|
|
ProjectionElem::Deref
|
|
|
|
| ProjectionElem::Index(..)
|
2018-03-01 16:43:03 -03:00
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. } => {
|
2022-04-25 16:09:36 +08:00
|
|
|
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-04-25 16:09:36 +08:00
|
|
|
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
|
2022-07-27 11:58:34 +00:00
|
|
|
ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(*ty),
|
2022-04-25 16:09:36 +08:00
|
|
|
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
|
2017-12-06 20:27:38 +02:00
|
|
|
},
|
2022-04-25 16:09:36 +08:00
|
|
|
};
|
2022-07-23 00:35:35 +03:00
|
|
|
self.describe_field_from_ty(
|
|
|
|
place_ty.ty,
|
|
|
|
field,
|
|
|
|
place_ty.variant_index,
|
|
|
|
including_tuple_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,
|
|
|
|
variant_index: Option<VariantIdx>,
|
2022-07-23 00:35:35 +03:00
|
|
|
including_tuple_field: IncludingTupleField,
|
|
|
|
) -> Option<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
|
2022-07-23 00:35:35 +03:00
|
|
|
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index, including_tuple_field)
|
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());
|
2022-03-05 07:28:41 +11:00
|
|
|
&def.variant(idx)
|
2019-03-28 18:00:17 -07:00
|
|
|
} else {
|
|
|
|
def.non_enum_variant()
|
|
|
|
};
|
2022-10-25 20:15:15 +04:00
|
|
|
if !including_tuple_field.0 && variant.ctor_kind() == Some(CtorKind::Fn) {
|
2022-07-23 00:35:35 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(variant.fields[field.index()].name.to_string())
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2022-07-23 00:35:35 +03:00
|
|
|
ty::Tuple(_) => Some(field.index().to_string()),
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
|
2022-07-23 00:35:35 +03:00
|
|
|
self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
|
2017-12-06 20:27:38 +02:00
|
|
|
}
|
2019-03-28 18:00:17 -07:00
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => {
|
2022-07-23 00:35:35 +03:00
|
|
|
self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
|
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.
|
2022-07-12 09:10:22 -05:00
|
|
|
let def_id = def_id.expect_local();
|
2020-12-23 15:38:22 -05:00
|
|
|
let var_id = self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
2022-07-12 09:10:22 -05:00
|
|
|
.typeck(def_id)
|
2020-12-23 15:38:22 -05:00
|
|
|
.closure_min_captures_flattened(def_id)
|
|
|
|
.nth(field.index())
|
|
|
|
.unwrap()
|
|
|
|
.get_root_variable();
|
2017-12-06 20:27:38 +02:00
|
|
|
|
2022-07-23 00:35:35 +03:00
|
|
|
Some(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)
|
|
|
|
bug!("End-user description not implemented for field access on `{:?}`", ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2022-01-23 20:41:46 +00:00
|
|
|
err: &mut Diagnostic,
|
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.
|
2022-02-19 00:48:49 +01:00
|
|
|
let InitLocation::Statement(loc) = init.location else { continue };
|
2019-07-07 14:00:59 +01:00
|
|
|
|
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={:?}",
|
|
|
|
loc, is_terminator,
|
|
|
|
);
|
|
|
|
if !is_terminator {
|
|
|
|
continue;
|
|
|
|
} else if let Some(Terminator {
|
2022-12-02 19:07:57 +00:00
|
|
|
kind: TerminatorKind::Call { func, from_hir_call: false, .. },
|
2019-07-07 14:00:59 +01:00
|
|
|
..
|
2022-12-02 19:07:57 +00:00
|
|
|
}) = &bbd.terminator
|
2019-10-04 00:55:28 -04:00
|
|
|
{
|
2019-10-28 18:16:25 -04:00
|
|
|
if let Some(source) =
|
2020-04-12 10:28:41 -07:00
|
|
|
BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
|
2019-10-28 18:16:25 -04: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
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 15:44:45 +02:00
|
|
|
|
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 {
|
2022-02-18 16:15:29 -05:00
|
|
|
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, 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`.
|
2022-01-28 11:25:15 +11:00
|
|
|
if let ty::Ref(region, ..) = ty.kind() {
|
|
|
|
match **region {
|
|
|
|
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
|
|
|
| ty::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
|
|
|
|
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
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
ty.print(printer).unwrap().into_buffer()
|
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 {
|
2022-02-18 16:15:29 -05:00
|
|
|
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
|
2019-01-11 13:07:01 +02:00
|
|
|
|
2022-01-28 11:25:15 +11:00
|
|
|
let region = if let ty::Ref(region, ..) = ty.kind() {
|
|
|
|
match **region {
|
|
|
|
ty::ReLateBound(_, ty::BoundRegion { kind: br, .. })
|
|
|
|
| ty::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
|
|
|
|
printer.region_highlight_mode.highlighting_bound_region(br, counter)
|
2018-09-27 19:10:29 -07:00
|
|
|
}
|
2022-01-28 11:25:15 +11:00
|
|
|
_ => {}
|
2019-01-11 13:07:01 +02:00
|
|
|
}
|
2022-01-28 11:25:15 +11:00
|
|
|
region
|
|
|
|
} else {
|
|
|
|
bug!("ty for annotation of borrow region is not a reference");
|
2019-01-11 13:07:01 +02:00
|
|
|
};
|
|
|
|
|
2022-02-18 16:15:29 -05:00
|
|
|
region.print(printer).unwrap().into_buffer()
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// Add a span label to the arguments of the closure, if it exists.
|
2022-01-23 20:41:46 +00:00
|
|
|
pub(super) fn args_span_label(self, err: &mut Diagnostic, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:09:14 +08: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_path_only_subdiag(
|
|
|
|
self,
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
action: crate::InitializationRequiringAction,
|
|
|
|
) {
|
|
|
|
use crate::session_diagnostics::CaptureVarPathUseCause::*;
|
|
|
|
use crate::InitializationRequiringAction::*;
|
|
|
|
if let UseSpans::ClosureUse { generator_kind, path_span, .. } = self {
|
|
|
|
match generator_kind {
|
|
|
|
Some(_) => {
|
|
|
|
err.subdiagnostic(match action {
|
|
|
|
Borrow => BorrowInGenerator { path_span },
|
|
|
|
MatchOn | Use => UseInGenerator { path_span },
|
|
|
|
Assignment => AssignInGenerator { path_span },
|
|
|
|
PartialAssignment => AssignPartInGenerator { path_span },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
err.subdiagnostic(match action {
|
|
|
|
Borrow => BorrowInClosure { path_span },
|
|
|
|
MatchOn | Use => UseInClosure { path_span },
|
|
|
|
Assignment => AssignInClosure { path_span },
|
|
|
|
PartialAssignment => AssignPartInClosure { path_span },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 02:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:15:06 +00: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,
|
2022-01-23 20:41:46 +00:00
|
|
|
err: &mut Diagnostic,
|
2019-02-08 06:28:15 +09:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:42:05 +08:00
|
|
|
/// Add a subdiagnostic to the use of the captured variable, if it exists.
|
|
|
|
pub(super) fn var_subdiag(
|
|
|
|
self,
|
|
|
|
err: &mut Diagnostic,
|
2022-11-09 20:56:28 +08:00
|
|
|
kind: Option<rustc_middle::mir::BorrowKind>,
|
|
|
|
f: impl Fn(Option<GeneratorKind>, Span) -> crate::session_diagnostics::CaptureVarCause,
|
2022-10-26 10:42:05 +08:00
|
|
|
) {
|
2022-11-09 20:56:28 +08:00
|
|
|
use crate::session_diagnostics::CaptureVarKind::*;
|
|
|
|
if let UseSpans::ClosureUse { generator_kind, capture_kind_span, path_span, .. } = self {
|
|
|
|
if capture_kind_span != path_span {
|
|
|
|
err.subdiagnostic(match kind {
|
|
|
|
Some(kd) => match kd {
|
|
|
|
rustc_middle::mir::BorrowKind::Shared
|
|
|
|
| rustc_middle::mir::BorrowKind::Shallow
|
|
|
|
| rustc_middle::mir::BorrowKind::Unique => {
|
|
|
|
Immute { kind_span: capture_kind_span }
|
|
|
|
}
|
|
|
|
|
|
|
|
rustc_middle::mir::BorrowKind::Mut { .. } => {
|
|
|
|
Mut { kind_span: capture_kind_span }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => Move { kind_span: capture_kind_span },
|
2022-10-26 10:42:05 +08:00
|
|
|
});
|
2022-11-09 20:56:28 +08:00
|
|
|
};
|
|
|
|
err.subdiagnostic(f(generator_kind, path_span));
|
2022-10-26 10:42:05 +08: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.
|
2022-07-10 03:18:56 +09:00
|
|
|
pub(super) fn describe(&self) -> &str {
|
2018-10-10 21:56:17 +02:00
|
|
|
match *self {
|
2019-10-10 22:20:57 +08:00
|
|
|
UseSpans::ClosureUse { generator_kind, .. } => {
|
|
|
|
if generator_kind.is_some() {
|
2022-07-10 03:18:56 +09:00
|
|
|
" in generator"
|
2018-10-10 21:56:17 +02:00
|
|
|
} else {
|
2022-07-10 03:18:56 +09:00
|
|
|
" in closure"
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-10-10 21:56:17 +02:00
|
|
|
}
|
2022-07-10 03:18:56 +09:00
|
|
|
_ => "",
|
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()
|
2022-03-05 07:28:41 +11:00
|
|
|
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
|
2021-11-16 13:12:38 -06:00
|
|
|
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(_)
|
|
|
|
| BorrowedContentSource::OverloadedIndex(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 => {
|
|
|
|
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
|
|
|
|
}
|
2021-11-16 13:12:38 -06:00
|
|
|
BorrowedContentSource::OverloadedDeref(ty) => ty
|
|
|
|
.ty_adt_def()
|
2022-03-05 07:28:41 +11:00
|
|
|
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
|
2021-11-16 13:12:38 -06:00
|
|
|
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::*;
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(stmt) = self.body[location.block].statements.get(location.statement_index) else {
|
|
|
|
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);
|
2022-12-02 19:07:57 +00:00
|
|
|
if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind
|
|
|
|
&& let AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) = **kind
|
|
|
|
{
|
|
|
|
debug!("move_spans: def_id={:?} places={:?}", def_id, places);
|
|
|
|
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
|
|
|
self.closure_span(def_id, moved_place, places)
|
|
|
|
{
|
|
|
|
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.
|
2022-12-02 19:07:57 +00:00
|
|
|
if let StatementKind::FakeRead(box (cause, 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);
|
2022-12-02 19:07:57 +00:00
|
|
|
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
|
|
|
|
{
|
2022-02-15 05:58:25 +01:00
|
|
|
let Some((method_did, method_substs)) =
|
2021-01-05 20:08:11 +01:00
|
|
|
rustc_const_eval::util::find_self_call(
|
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
target_temp,
|
|
|
|
location.block,
|
2022-02-15 05:58:25 +01:00
|
|
|
)
|
|
|
|
else {
|
2020-08-10 07:16:30 -04:00
|
|
|
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,
|
2022-07-04 17:23:24 +09:00
|
|
|
fn_span: self.infcx.tcx.def_span(method_did),
|
2020-08-10 07:16:30 -04:00
|
|
|
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-11-06 12:00:46 -05:00
|
|
|
let target = match self.body[location.block].statements.get(location.statement_index) {
|
2022-12-02 19:07:57 +00:00
|
|
|
Some(Statement { kind: StatementKind::Assign(box (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..] {
|
2022-12-02 19:07:57 +00:00
|
|
|
if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind {
|
2022-07-12 09:10:22 -05:00
|
|
|
let (&def_id, is_generator) = match kind {
|
2018-10-10 21:56:17 +02:00
|
|
|
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)) =
|
2022-07-12 09:10:22 -05:00
|
|
|
self.closure_span(def_id, Place::from(target).as_ref(), places)
|
2018-10-10 21:56:17 +02:00
|
|
|
{
|
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,
|
2022-07-12 09:10:22 -05:00
|
|
|
def_id: LocalDefId,
|
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
|
|
|
|
);
|
2022-07-12 09:10:22 -05:00
|
|
|
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(def_id);
|
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);
|
2022-07-11 23:39:53 +04:00
|
|
|
if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = expr {
|
2022-07-12 09:10:22 -05:00
|
|
|
for (captured_place, place) in
|
|
|
|
self.infcx.tcx.typeck(def_id).closure_min_captures_flattened(def_id).zip(places)
|
2021-03-17 02:51:27 -04:00
|
|
|
{
|
2019-05-04 03:47:16 +03:00
|
|
|
match place {
|
|
|
|
Operand::Copy(place) | Operand::Move(place)
|
2019-07-21 22:38:30 +02:00
|
|
|
if target_place == place.as_ref() =>
|
|
|
|
{
|
2019-05-04 03:47:16 +03:00
|
|
|
debug!("closure_span: found captured local {:?}", place);
|
2022-07-11 23:39:53 +04:00
|
|
|
let body = self.infcx.tcx.hir().body(body);
|
2019-10-10 22:20:57 +08:00
|
|
|
let generator_kind = body.generator_kind();
|
2020-08-25 23:58:58 -04:00
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
return Some((
|
2022-07-11 23:39:53 +04:00
|
|
|
fn_decl_span,
|
2021-03-17 02:51:27 -04:00
|
|
|
generator_kind,
|
|
|
|
captured_place.get_capture_kind_span(self.infcx.tcx),
|
|
|
|
captured_place.get_path_span(self.infcx.tcx),
|
|
|
|
));
|
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)
|
|
|
|
}
|
2022-03-02 04:30:16 +00:00
|
|
|
|
|
|
|
fn explain_captures(
|
|
|
|
&mut self,
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
span: Span,
|
|
|
|
move_span: Span,
|
|
|
|
move_spans: UseSpans<'tcx>,
|
|
|
|
moved_place: Place<'tcx>,
|
|
|
|
partially_str: &str,
|
|
|
|
loop_message: &str,
|
|
|
|
move_msg: &str,
|
|
|
|
is_loop_move: bool,
|
|
|
|
maybe_reinitialized_locations_is_empty: bool,
|
|
|
|
) {
|
|
|
|
if let UseSpans::FnSelfUse { var_span, fn_call_span, fn_span, kind } = move_spans {
|
|
|
|
let place_name = self
|
|
|
|
.describe_place(moved_place.as_ref())
|
|
|
|
.map(|n| format!("`{}`", n))
|
|
|
|
.unwrap_or_else(|| "value".to_owned());
|
|
|
|
match kind {
|
|
|
|
CallKind::FnCall { fn_trait_id, .. }
|
|
|
|
if Some(fn_trait_id) == self.infcx.tcx.lang_items().fn_once_trait() =>
|
|
|
|
{
|
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
|
|
|
&format!(
|
|
|
|
"{} {}moved due to this call{}",
|
|
|
|
place_name, partially_str, loop_message
|
|
|
|
),
|
|
|
|
);
|
|
|
|
err.span_note(
|
|
|
|
var_span,
|
|
|
|
"this value implements `FnOnce`, which causes it to be moved when called",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
CallKind::Operator { self_arg, .. } => {
|
|
|
|
let self_arg = self_arg.unwrap();
|
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
|
|
|
&format!(
|
|
|
|
"{} {}moved due to usage in operator{}",
|
|
|
|
place_name, partially_str, loop_message
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if self.fn_self_span_reported.insert(fn_span) {
|
|
|
|
err.span_note(
|
2022-12-12 15:36:08 +00:00
|
|
|
self_arg.span,
|
2022-03-02 04:30:16 +00:00
|
|
|
"calling this operator moves the left-hand side",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 12:07:09 +00:00
|
|
|
CallKind::Normal { self_arg, desugaring, method_did } => {
|
2022-03-02 04:30:16 +00:00
|
|
|
let self_arg = self_arg.unwrap();
|
2022-12-23 12:44:47 -08:00
|
|
|
let tcx = self.infcx.tcx;
|
2022-03-02 04:30:16 +00:00
|
|
|
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
|
2022-12-23 12:44:47 -08:00
|
|
|
let ty = moved_place.ty(self.body, tcx).ty;
|
|
|
|
let suggest = match tcx.get_diagnostic_item(sym::IntoIterator) {
|
2022-03-02 04:30:16 +00:00
|
|
|
Some(def_id) => {
|
|
|
|
let infcx = self.infcx.tcx.infer_ctxt().build();
|
|
|
|
type_known_to_meet_bound_modulo_regions(
|
|
|
|
&infcx,
|
|
|
|
self.param_env,
|
2022-12-23 12:44:47 -08:00
|
|
|
tcx.mk_imm_ref(tcx.lifetimes.re_erased, tcx.erase_regions(ty)),
|
2022-03-02 04:30:16 +00:00
|
|
|
def_id,
|
|
|
|
DUMMY_SP,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if suggest {
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
move_span.shrink_to_lo(),
|
|
|
|
&format!(
|
|
|
|
"consider iterating over a slice of the `{}`'s content to \
|
|
|
|
avoid moving into the `for` loop",
|
|
|
|
ty,
|
|
|
|
),
|
2022-06-13 15:48:40 +09:00
|
|
|
"&",
|
2022-03-02 04:30:16 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
|
|
|
&format!(
|
|
|
|
"{} {}moved due to this implicit call to `.into_iter()`{}",
|
|
|
|
place_name, partially_str, loop_message
|
|
|
|
),
|
|
|
|
);
|
2022-09-05 05:00:26 +00:00
|
|
|
// If the moved place was a `&mut` ref, then we can
|
|
|
|
// suggest to reborrow it where it was moved, so it
|
|
|
|
// will still be valid by the time we get to the usage.
|
|
|
|
if let ty::Ref(_, _, hir::Mutability::Mut) =
|
|
|
|
moved_place.ty(self.body, self.infcx.tcx).ty.kind()
|
2022-03-02 04:30:16 +00:00
|
|
|
{
|
|
|
|
// If we are in a loop this will be suggested later.
|
|
|
|
if !is_loop_move {
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
move_span.shrink_to_lo(),
|
|
|
|
&format!(
|
|
|
|
"consider creating a fresh reborrow of {} here",
|
|
|
|
self.describe_place(moved_place.as_ref())
|
|
|
|
.map(|n| format!("`{}`", n))
|
|
|
|
.unwrap_or_else(|| "the mutable reference".to_string()),
|
|
|
|
),
|
2022-06-13 15:48:40 +09:00
|
|
|
"&mut *",
|
2022-03-02 04:30:16 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
|
|
|
&format!(
|
|
|
|
"{} {}moved due to this method call{}",
|
|
|
|
place_name, partially_str, loop_message
|
|
|
|
),
|
|
|
|
);
|
2022-12-25 16:51:11 -08:00
|
|
|
let infcx = tcx.infer_ctxt().build();
|
|
|
|
let ty = infcx.freshen(moved_place.ty(self.body, tcx).ty);
|
2022-12-23 15:13:04 -08:00
|
|
|
if let ty::Adt(def, substs) = ty.kind()
|
2022-12-25 16:51:11 -08:00
|
|
|
&& Some(def.did()) == tcx.lang_items().pin_type()
|
2022-12-23 15:13:04 -08:00
|
|
|
&& let ty::Ref(_, _, hir::Mutability::Mut) = substs.type_at(0).kind()
|
2022-12-25 16:51:11 -08:00
|
|
|
&& let self_ty = infcx.freshen(
|
|
|
|
infcx.replace_bound_vars_with_fresh_vars(
|
|
|
|
fn_call_span,
|
|
|
|
LateBoundRegionConversionTime::FnCall,
|
|
|
|
tcx.fn_sig(method_did).input(0),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
&& infcx.can_eq(self.param_env, ty, self_ty).is_ok()
|
2022-12-23 09:59:39 -08:00
|
|
|
{
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
fn_call_span.shrink_to_lo(),
|
|
|
|
"consider reborrowing the `Pin` instead of moving it",
|
|
|
|
"as_mut().".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
2022-12-25 16:51:11 -08:00
|
|
|
if let Some(clone_trait) = tcx.lang_items().clone_trait()
|
|
|
|
&& let trait_ref = tcx.mk_trait_ref(clone_trait, [ty])
|
|
|
|
&& let o = Obligation::new(
|
|
|
|
tcx,
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
self.param_env,
|
|
|
|
ty::Binder::dummy(trait_ref),
|
|
|
|
)
|
|
|
|
&& infcx.predicate_must_hold_modulo_regions(&o)
|
|
|
|
{
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
fn_call_span.shrink_to_lo(),
|
|
|
|
"you can `clone` the value and consume it, but this might not be \
|
|
|
|
your desired behavior",
|
|
|
|
"clone().".to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2022-12-23 12:44:47 -08:00
|
|
|
}
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
// Avoid pointing to the same function in multiple different
|
|
|
|
// error messages.
|
|
|
|
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
|
2022-12-12 12:07:09 +00:00
|
|
|
let func = tcx.def_path_str(method_did);
|
2022-03-02 04:30:16 +00:00
|
|
|
err.span_note(
|
|
|
|
self_arg.span,
|
2022-12-12 12:07:09 +00:00
|
|
|
&format!("`{func}` takes ownership of the receiver `self`, which moves {place_name}")
|
2022-03-02 04:30:16 +00:00
|
|
|
);
|
|
|
|
}
|
2022-12-12 12:07:09 +00:00
|
|
|
let parent_did = tcx.parent(method_did);
|
|
|
|
let parent_self_ty = (tcx.def_kind(parent_did)
|
|
|
|
== rustc_hir::def::DefKind::Impl)
|
|
|
|
.then_some(parent_did)
|
|
|
|
.and_then(|did| match tcx.type_of(did).kind() {
|
|
|
|
ty::Adt(def, ..) => Some(def.did()),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
|
|
|
|
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
|
|
|
|
});
|
2022-08-16 03:02:04 +00:00
|
|
|
if is_option_or_result && maybe_reinitialized_locations_is_empty {
|
|
|
|
err.span_label(
|
|
|
|
var_span,
|
|
|
|
"help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents",
|
|
|
|
);
|
|
|
|
}
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
// Other desugarings takes &self, which cannot cause a move
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if move_span != span || !loop_message.is_empty() {
|
|
|
|
err.span_label(
|
|
|
|
move_span,
|
|
|
|
format!("value {}moved{} here{}", partially_str, move_msg, loop_message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// If the move error occurs due to a loop, don't show
|
|
|
|
// another message for the same span
|
|
|
|
if loop_message.is_empty() {
|
|
|
|
move_spans.var_span_label(
|
|
|
|
err,
|
|
|
|
format!("variable {}moved due to use{}", partially_str, move_spans.describe()),
|
|
|
|
"moved",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|