2019-11-27 11:39:25 -06:00
|
|
|
//! Borrow checker diagnostics.
|
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
use crate::session_diagnostics::{
|
|
|
|
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
|
|
|
|
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
|
|
|
|
};
|
2022-05-06 12:11:42 +08:00
|
|
|
use itertools::Itertools;
|
2022-03-02 04:30:16 +00:00
|
|
|
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;
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::IndexSlice;
|
2023-04-17 16:38:30 +00:00
|
|
|
use rustc_infer::infer::LateBoundRegionConversionTime;
|
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::{
|
2023-09-20 20:51:14 +02:00
|
|
|
AggregateKind, CallSource, ConstOperand, FakeReadCause, Local, LocalInfo, LocalKind, Location,
|
2023-06-18 05:24:38 +00:00
|
|
|
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;
|
2023-02-22 19:51:17 +04:00
|
|
|
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
|
2023-04-16 12:03:39 +02:00
|
|
|
use rustc_middle::util::{call_kind, CallDesugaringKind};
|
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};
|
2023-03-28 12:32:57 -07:00
|
|
|
use rustc_target::abi::{FieldIdx, 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};
|
2023-04-16 12:03:39 +02:00
|
|
|
pub(crate) use rustc_middle::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 {
|
2023-09-20 20:51:14 +02:00
|
|
|
func: Operand::Constant(box ConstOperand { const_, .. }),
|
2018-10-11 01:19:55 +02:00
|
|
|
args,
|
|
|
|
..
|
|
|
|
} = &terminator.kind
|
|
|
|
{
|
2023-09-20 20:51:14 +02:00
|
|
|
if let ty::FnDef(id, _) = *const_.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();
|
2023-02-25 22:51:57 +00:00
|
|
|
if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
|
2023-02-10 16:58:32 +08:00
|
|
|
diag.eager_subdiagnostic(
|
|
|
|
&self.infcx.tcx.sess.parse_sess.span_diagnostic,
|
|
|
|
OnClosureNote::InvokedTwice {
|
|
|
|
place_name: &ty::place_to_string_for_capture(
|
|
|
|
self.infcx.tcx,
|
|
|
|
hir_place,
|
|
|
|
),
|
|
|
|
span: *span,
|
|
|
|
},
|
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();
|
2023-02-25 22:51:57 +00:00
|
|
|
if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
|
2023-02-10 16:58:32 +08:00
|
|
|
diag.eager_subdiagnostic(
|
|
|
|
&self.infcx.tcx.sess.parse_sess.span_diagnostic,
|
|
|
|
OnClosureNote::MovedTwice {
|
|
|
|
place_name: &ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
|
|
|
|
span: *span,
|
|
|
|
},
|
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
|
|
|
}
|
2023-03-09 16:55:20 +00:00
|
|
|
if let LocalInfo::StaticRef { def_id, .. } =
|
|
|
|
*self.body.local_decls[local].local_info()
|
2022-05-06 12:11:42 +08:00
|
|
|
{
|
2023-03-09 16:55:20 +00:00
|
|
|
buf.push_str(self.infcx.tcx.item_name(def_id).as_str());
|
2022-05-06 12:11:42 +08:00
|
|
|
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(..) => (),
|
2023-08-16 08:43:30 +03:00
|
|
|
ProjectionElem::Subtype(..) => (),
|
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>,
|
2023-03-28 12:32:57 -07:00
|
|
|
field: FieldIdx,
|
2022-07-23 00:35:35 +03:00
|
|
|
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(..)
|
2023-08-16 08:43:30 +03:00
|
|
|
| ProjectionElem::Subtype(..)
|
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<'_>,
|
2023-03-28 12:32:57 -07:00
|
|
|
field: FieldIdx,
|
2019-03-28 18:00:17 -07:00
|
|
|
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;
|
|
|
|
}
|
2023-03-28 23:32:25 -07:00
|
|
|
Some(variant.fields[field].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();
|
2023-02-25 22:51:57 +00:00
|
|
|
let var_id =
|
|
|
|
self.infcx.tcx.closure_captures(def_id)[field.index()].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-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 {
|
2023-06-18 05:24:38 +00:00
|
|
|
kind:
|
|
|
|
TerminatorKind::Call {
|
|
|
|
func,
|
|
|
|
call_source: CallSource::OverloadedOperator,
|
|
|
|
..
|
|
|
|
},
|
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, .. })
|
2023-04-06 21:12:17 -04:00
|
|
|
| ty::RePlaceholder(ty::PlaceholderRegion {
|
|
|
|
bound: ty::BoundRegion { kind: br, .. },
|
|
|
|
..
|
|
|
|
}) => printer.region_highlight_mode.highlighting_bound_region(br, counter),
|
2022-01-28 11:25:15 +11:00
|
|
|
_ => {}
|
|
|
|
}
|
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, .. })
|
2023-04-06 21:12:17 -04:00
|
|
|
| ty::RePlaceholder(ty::PlaceholderRegion {
|
|
|
|
bound: ty::BoundRegion { kind: br, .. },
|
|
|
|
..
|
|
|
|
}) => printer.region_highlight_mode.highlighting_bound_region(br, counter),
|
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.
|
2023-02-10 16:58:32 +08:00
|
|
|
pub(super) fn args_subdiag(
|
|
|
|
self,
|
|
|
|
err: &mut Diagnostic,
|
|
|
|
f: impl FnOnce(Span) -> CaptureArgLabel,
|
|
|
|
) {
|
2018-08-01 20:38:02 +01:00
|
|
|
if let UseSpans::ClosureUse { args_span, .. } = self {
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(f(args_span));
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::InitializationRequiringAction::*;
|
2023-02-10 16:58:32 +08:00
|
|
|
use CaptureVarPathUseCause::*;
|
2022-11-04 17:09:14 +08:00
|
|
|
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-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,
|
2023-02-10 16:58:32 +08:00
|
|
|
handler: Option<&rustc_errors::Handler>,
|
2022-10-26 10:42:05 +08:00
|
|
|
err: &mut Diagnostic,
|
2022-11-09 20:56:28 +08:00
|
|
|
kind: Option<rustc_middle::mir::BorrowKind>,
|
2023-02-10 16:58:32 +08:00
|
|
|
f: impl FnOnce(Option<GeneratorKind>, Span) -> CaptureVarCause,
|
2022-10-26 10:42:05 +08:00
|
|
|
) {
|
2022-11-09 20:56:28 +08:00
|
|
|
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
|
2023-05-31 19:13:13 +08:00
|
|
|
| rustc_middle::mir::BorrowKind::Shallow => {
|
|
|
|
CaptureVarKind::Immut { kind_span: capture_kind_span }
|
|
|
|
}
|
2022-11-09 20:56:28 +08:00
|
|
|
|
|
|
|
rustc_middle::mir::BorrowKind::Mut { .. } => {
|
2023-02-10 16:58:32 +08:00
|
|
|
CaptureVarKind::Mut { kind_span: capture_kind_span }
|
2022-11-09 20:56:28 +08:00
|
|
|
}
|
|
|
|
},
|
2023-02-10 16:58:32 +08:00
|
|
|
None => CaptureVarKind::Move { kind_span: capture_kind_span },
|
2022-10-26 10:42:05 +08:00
|
|
|
});
|
2022-11-09 20:56:28 +08:00
|
|
|
};
|
2023-02-10 16:58:32 +08:00
|
|
|
let diag = f(generator_kind, path_span);
|
|
|
|
match handler {
|
|
|
|
Some(hd) => err.eager_subdiagnostic(hd, diag),
|
|
|
|
None => err.subdiagnostic(diag),
|
|
|
|
};
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())? {
|
2023-01-06 19:34:45 -05:00
|
|
|
name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
|
2021-11-16 13:12:38 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2023-01-06 19:34:45 -05:00
|
|
|
.unwrap_or_else(|| format!("dereference of `{ty}`")),
|
|
|
|
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{ty}`"),
|
2019-07-07 14:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())? {
|
2023-01-06 19:34:45 -05:00
|
|
|
name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
|
2021-11-16 13:12:38 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2023-01-06 19:34:45 -05:00
|
|
|
.unwrap_or_else(|| format!("dereference of `{ty}`")),
|
|
|
|
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{ty}`"),
|
2019-07-07 14:16:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::FnDef(def_id, args) => {
|
2019-07-07 14:00:59 +01:00
|
|
|
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()
|
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
Some(BorrowedContentSource::OverloadedDeref(args.type_at(0)))
|
2019-07-07 14:00:59 +01:00
|
|
|
} else if Some(trait_id) == lang_items.index_trait()
|
|
|
|
|| Some(trait_id) == lang_items.index_mut_trait()
|
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
Some(BorrowedContentSource::OverloadedIndex(args.type_at(0)))
|
2019-07-07 14:00:59 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
///helper struct for explain_captures()
|
|
|
|
struct CapturedMessageOpt {
|
|
|
|
is_partial_move: bool,
|
|
|
|
is_loop_message: bool,
|
|
|
|
is_move_msg: bool,
|
|
|
|
is_loop_move: bool,
|
|
|
|
maybe_reinitialized_locations_is_empty: bool,
|
|
|
|
}
|
|
|
|
|
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);
|
2023-01-23 22:15:33 +00:00
|
|
|
let def_id = def_id.expect_local();
|
2022-12-02 19:07:57 +00:00
|
|
|
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)) =
|
2023-04-01 20:11:38 -07:00
|
|
|
self.closure_span(closure_def_id, moved_place, IndexSlice::from_raw(places))
|
2021-03-25 23:03:12 -04:00
|
|
|
{
|
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 {
|
2023-06-18 05:24:38 +00:00
|
|
|
kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
|
2020-06-11 13:48:46 -04:00
|
|
|
}) = &self.body[location.block].terminator
|
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
let Some((method_did, method_args)) = rustc_middle::util::find_self_call(
|
2021-01-05 20:08:11 +01:00
|
|
|
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,
|
2023-07-11 22:35:29 +01:00
|
|
|
method_args,
|
2021-12-09 22:42:17 +08:00
|
|
|
*fn_span,
|
2023-06-18 05:24:38 +00:00
|
|
|
call_source.from_hir_call(),
|
2021-12-09 22:42:17 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-08 22:29:52 +01:00
|
|
|
// drop and replace might have moved the assignment to the next block
|
|
|
|
let maybe_additional_statement =
|
|
|
|
if let TerminatorKind::Drop { target: drop_target, .. } =
|
|
|
|
self.body[location.block].terminator().kind
|
|
|
|
{
|
|
|
|
self.body[drop_target].statements.first()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let statements =
|
|
|
|
self.body[location.block].statements[location.statement_index + 1..].iter();
|
|
|
|
|
|
|
|
for stmt in statements.chain(maybe_additional_statement) {
|
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,
|
|
|
|
};
|
2023-01-23 22:15:33 +00:00
|
|
|
let def_id = def_id.expect_local();
|
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>,
|
2023-04-01 20:11:38 -07:00
|
|
|
places: &IndexSlice<FieldIdx, 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
|
2023-02-25 22:51:57 +00:00
|
|
|
self.infcx.tcx.closure_captures(def_id).iter().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>,
|
2023-02-10 16:58:32 +08:00
|
|
|
msg_opt: CapturedMessageOpt,
|
2022-03-02 04:30:16 +00:00
|
|
|
) {
|
2023-02-10 16:58:32 +08:00
|
|
|
let CapturedMessageOpt {
|
|
|
|
is_partial_move: is_partial,
|
|
|
|
is_loop_message,
|
|
|
|
is_move_msg,
|
|
|
|
is_loop_move,
|
|
|
|
maybe_reinitialized_locations_is_empty,
|
|
|
|
} = msg_opt;
|
2022-03-02 04:30:16 +00:00
|
|
|
if let UseSpans::FnSelfUse { var_span, fn_call_span, fn_span, kind } = move_spans {
|
|
|
|
let place_name = self
|
|
|
|
.describe_place(moved_place.as_ref())
|
2023-01-06 19:34:45 -05:00
|
|
|
.map(|n| format!("`{n}`"))
|
2022-03-02 04:30:16 +00:00
|
|
|
.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() =>
|
|
|
|
{
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonLabel::Call {
|
2022-03-02 04:30:16 +00:00
|
|
|
fn_call_span,
|
2023-02-10 16:58:32 +08:00
|
|
|
place_name: &place_name,
|
|
|
|
is_partial,
|
|
|
|
is_loop_message,
|
|
|
|
});
|
|
|
|
err.subdiagnostic(CaptureReasonNote::FnOnceMoveInCall { var_span });
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
CallKind::Operator { self_arg, .. } => {
|
|
|
|
let self_arg = self_arg.unwrap();
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonLabel::OperatorUse {
|
2022-03-02 04:30:16 +00:00
|
|
|
fn_call_span,
|
2023-02-10 16:58:32 +08:00
|
|
|
place_name: &place_name,
|
|
|
|
is_partial,
|
|
|
|
is_loop_message,
|
|
|
|
});
|
2022-03-02 04:30:16 +00:00
|
|
|
if self.fn_self_span_reported.insert(fn_span) {
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonNote::LhsMoveByOperator {
|
|
|
|
span: self_arg.span,
|
|
|
|
});
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
CallKind::Normal { self_arg, desugaring, method_did, method_args } => {
|
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) {
|
2023-04-17 16:38:30 +00:00
|
|
|
Some(def_id) => type_known_to_meet_bound_modulo_regions(
|
|
|
|
&self.infcx,
|
|
|
|
self.param_env,
|
2023-07-05 20:13:26 +01:00
|
|
|
Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, ty),
|
2023-04-17 16:38:30 +00:00
|
|
|
def_id,
|
|
|
|
),
|
2022-03-02 04:30:16 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if suggest {
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonSuggest::IterateSlice {
|
|
|
|
ty,
|
|
|
|
span: move_span.shrink_to_lo(),
|
|
|
|
});
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonLabel::ImplicitCall {
|
2022-03-02 04:30:16 +00:00
|
|
|
fn_call_span,
|
2023-02-10 16:58:32 +08:00
|
|
|
place_name: &place_name,
|
|
|
|
is_partial,
|
|
|
|
is_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(),
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
format!(
|
2022-03-02 04:30:16 +00:00
|
|
|
"consider creating a fresh reborrow of {} here",
|
|
|
|
self.describe_place(moved_place.as_ref())
|
2023-01-06 19:34:45 -05:00
|
|
|
.map(|n| format!("`{n}`"))
|
2022-03-02 04:30:16 +00:00
|
|
|
.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 {
|
2023-04-25 19:48:59 +00:00
|
|
|
if let Some((CallDesugaringKind::Await, _)) = desugaring {
|
|
|
|
err.subdiagnostic(CaptureReasonLabel::Await {
|
|
|
|
fn_call_span,
|
|
|
|
place_name: &place_name,
|
|
|
|
is_partial,
|
|
|
|
is_loop_message,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
err.subdiagnostic(CaptureReasonLabel::MethodCall {
|
|
|
|
fn_call_span,
|
|
|
|
place_name: &place_name,
|
|
|
|
is_partial,
|
|
|
|
is_loop_message,
|
|
|
|
});
|
|
|
|
}
|
2023-01-28 20:43:16 +01:00
|
|
|
// Erase and shadow everything that could be passed to the new infcx.
|
2023-04-17 16:38:30 +00:00
|
|
|
let ty = moved_place.ty(self.body, tcx).ty;
|
2023-01-28 20:43:16 +01:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
if let ty::Adt(def, args) = ty.kind()
|
2022-12-25 16:51:11 -08:00
|
|
|
&& Some(def.did()) == tcx.lang_items().pin_type()
|
2023-07-11 22:35:29 +01:00
|
|
|
&& let ty::Ref(_, _, hir::Mutability::Mut) = args.type_at(0).kind()
|
2023-04-17 16:38:30 +00:00
|
|
|
&& let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
|
2022-12-26 13:35:35 -08:00
|
|
|
fn_call_span,
|
|
|
|
LateBoundRegionConversionTime::FnCall,
|
2023-07-11 22:35:29 +01:00
|
|
|
tcx.fn_sig(method_did).instantiate(tcx, method_args).input(0),
|
2022-12-25 16:51:11 -08:00
|
|
|
)
|
2023-04-17 16:38:30 +00:00
|
|
|
&& self.infcx.can_eq(self.param_env, ty, self_ty)
|
2022-12-23 09:59:39 -08:00
|
|
|
{
|
2023-02-10 16:58:32 +08:00
|
|
|
err.eager_subdiagnostic(
|
|
|
|
&self.infcx.tcx.sess.parse_sess.span_diagnostic,
|
|
|
|
CaptureReasonSuggest::FreshReborrow {
|
2023-06-22 21:32:39 +00:00
|
|
|
span: move_span.shrink_to_hi(),
|
2023-02-10 16:58:32 +08:00
|
|
|
});
|
2022-12-23 09:59:39 -08:00
|
|
|
}
|
2022-12-25 16:51:11 -08:00
|
|
|
if let Some(clone_trait) = tcx.lang_items().clone_trait()
|
2023-04-25 16:07:48 +00:00
|
|
|
&& let trait_ref = ty::TraitRef::new(tcx, clone_trait, [ty])
|
2022-12-25 16:51:11 -08:00
|
|
|
&& let o = Obligation::new(
|
|
|
|
tcx,
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
self.param_env,
|
|
|
|
ty::Binder::dummy(trait_ref),
|
|
|
|
)
|
2023-04-17 16:38:30 +00:00
|
|
|
&& self.infcx.predicate_must_hold_modulo_regions(&o)
|
2022-12-25 16:51:11 -08:00
|
|
|
{
|
|
|
|
err.span_suggestion_verbose(
|
2023-06-22 20:40:49 +00:00
|
|
|
move_span.shrink_to_hi(),
|
2022-12-25 16:51:11 -08:00
|
|
|
"you can `clone` the value and consume it, but this might not be \
|
|
|
|
your desired behavior",
|
2023-06-22 20:40:49 +00:00
|
|
|
".clone()".to_string(),
|
2022-12-25 16:51:11 -08:00
|
|
|
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) {
|
2023-06-30 00:38:56 +08:00
|
|
|
self.explain_iterator_advancement_in_for_loop_if_applicable(
|
|
|
|
err,
|
|
|
|
span,
|
|
|
|
&move_spans,
|
|
|
|
);
|
|
|
|
|
2022-12-12 12:07:09 +00:00
|
|
|
let func = tcx.def_path_str(method_did);
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonNote::FuncTakeSelf {
|
|
|
|
func,
|
|
|
|
place_name,
|
|
|
|
span: self_arg.span,
|
|
|
|
});
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
2022-12-12 12:07:09 +00:00
|
|
|
let parent_did = tcx.parent(method_did);
|
2023-02-12 18:26:47 +00:00
|
|
|
let parent_self_ty =
|
|
|
|
matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
|
|
|
|
.then_some(parent_did)
|
2023-07-11 22:35:29 +01:00
|
|
|
.and_then(|did| match tcx.type_of(did).instantiate_identity().kind() {
|
2023-02-12 18:26:47 +00:00
|
|
|
ty::Adt(def, ..) => Some(def.did()),
|
|
|
|
_ => None,
|
|
|
|
});
|
2023-05-24 14:19:22 +00:00
|
|
|
let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
|
2022-12-12 12:07:09 +00:00
|
|
|
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 {
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(CaptureReasonLabel::BorrowContent { var_span });
|
2022-08-16 03:02:04 +00:00
|
|
|
}
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
// Other desugarings takes &self, which cannot cause a move
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
2023-02-10 16:58:32 +08:00
|
|
|
if move_span != span || is_loop_message {
|
|
|
|
err.subdiagnostic(CaptureReasonLabel::MovedHere {
|
2022-03-02 04:30:16 +00:00
|
|
|
move_span,
|
2023-02-10 16:58:32 +08:00
|
|
|
is_partial,
|
|
|
|
is_move_msg,
|
|
|
|
is_loop_message,
|
|
|
|
});
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
// If the move error occurs due to a loop, don't show
|
|
|
|
// another message for the same span
|
2023-02-10 16:58:32 +08:00
|
|
|
if !is_loop_message {
|
|
|
|
move_spans.var_subdiag(None, err, None, |kind, var_span| match kind {
|
|
|
|
Some(_) => CaptureVarCause::PartialMoveUseInGenerator { var_span, is_partial },
|
|
|
|
None => CaptureVarCause::PartialMoveUseInClosure { var_span, is_partial },
|
|
|
|
})
|
2022-03-02 04:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 20:38:02 +01:00
|
|
|
}
|