2020-04-12 10:31:00 -07:00
|
|
|
use either::Either;
|
2019-05-04 08:40:07 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_errors::{Applicability, DiagnosticBuilder};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
|
|
|
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory,
|
2021-05-21 21:01:27 +02:00
|
|
|
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
|
2021-02-02 03:13:19 +11:00
|
|
|
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
|
2020-03-29 16:41:09 +02:00
|
|
|
};
|
2021-07-04 11:43:22 -04:00
|
|
|
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty};
|
2021-01-05 19:53:07 +01:00
|
|
|
use rustc_mir_dataflow::drop_flag_effects;
|
|
|
|
use rustc_mir_dataflow::move_paths::{MoveOutIndex, MovePathIndex};
|
2021-02-09 17:18:28 +00:00
|
|
|
use rustc_span::source_map::DesugaringKind;
|
|
|
|
use rustc_span::symbol::sym;
|
2021-06-28 11:22:47 -07:00
|
|
|
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
|
2021-07-06 05:38:15 -04:00
|
|
|
use rustc_trait_selection::infer::InferCtxtExt;
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::borrowck_errors;
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::{
|
2021-02-09 17:18:28 +00:00
|
|
|
borrow_set::BorrowData, diagnostics::Instance, prefixes::IsPrefixOf,
|
|
|
|
InitializationRequiringAction, MirBorrowckCtxt, PrefixSet, WriteKind,
|
2019-11-27 11:39:25 -06:00
|
|
|
};
|
|
|
|
|
2019-11-27 11:45:05 -06:00
|
|
|
use super::{
|
2020-06-11 13:48:46 -04:00
|
|
|
explain_borrow::BorrowExplanation, FnSelfUseKind, IncludingDowncast, RegionName,
|
|
|
|
RegionNameSource, UseSpans,
|
2019-11-27 11:45:05 -06:00
|
|
|
};
|
2019-11-27 11:39:25 -06:00
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct MoveSite {
|
|
|
|
/// Index of the "move out" that we found. The `MoveData` can
|
|
|
|
/// then tell us where the move occurred.
|
|
|
|
moi: MoveOutIndex,
|
|
|
|
|
|
|
|
/// `true` if we traversed a back edge while walking from the point
|
|
|
|
/// of error to the move site.
|
2019-12-22 17:42:04 -05:00
|
|
|
traversed_back_edge: bool,
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Which case a StorageDeadOrDrop is for.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum StorageDeadOrDrop<'tcx> {
|
|
|
|
LocalStorageDead,
|
|
|
|
BoxedStorageDead,
|
|
|
|
Destructor(Ty<'tcx>),
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_use_of_moved_or_uninitialized(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
desired_action: InitializationRequiringAction,
|
2020-03-04 18:25:03 -03:00
|
|
|
(moved_place, used_place, span): (PlaceRef<'tcx>, PlaceRef<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
mpi: MovePathIndex,
|
|
|
|
) {
|
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized: location={:?} desired_action={:?} \
|
|
|
|
moved_place={:?} used_place={:?} span={:?} mpi={:?}",
|
|
|
|
location, desired_action, moved_place, used_place, span, mpi
|
|
|
|
);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let use_spans =
|
|
|
|
self.move_spans(moved_place, location).or_else(|| self.borrow_spans(span, location));
|
2019-05-04 08:40:07 +01:00
|
|
|
let span = use_spans.args_or_use();
|
|
|
|
|
2021-05-19 18:40:47 +02:00
|
|
|
let (move_site_vec, maybe_reinitialized_locations) = self.get_moved_indexes(location, mpi);
|
2020-07-25 07:04:13 -04:00
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized: move_site_vec={:?} use_spans={:?}",
|
|
|
|
move_site_vec, use_spans
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
let move_out_indices: Vec<_> =
|
|
|
|
move_site_vec.iter().map(|move_site| move_site.moi).collect();
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
if move_out_indices.is_empty() {
|
2019-11-22 22:03:25 +00:00
|
|
|
let root_place = PlaceRef { projection: &[], ..used_place };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-16 13:48:20 +09:00
|
|
|
if !self.uninitialized_error_reported.insert(root_place) {
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
|
|
|
|
root_place
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let item_msg =
|
|
|
|
match self.describe_place_with_options(used_place, IncludingDowncast(true)) {
|
|
|
|
Some(name) => format!("`{}`", name),
|
|
|
|
None => "value".to_owned(),
|
|
|
|
};
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_act_on_uninitialized_variable(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
desired_action.as_noun(),
|
2019-12-22 17:42:04 -05:00
|
|
|
&self
|
|
|
|
.describe_place_with_options(moved_place, IncludingDowncast(true))
|
2019-05-04 08:40:07 +01:00
|
|
|
.unwrap_or_else(|| "_".to_owned()),
|
|
|
|
);
|
2019-09-01 18:09:59 +01:00
|
|
|
err.span_label(span, format!("use of possibly-uninitialized {}", item_msg));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
use_spans.var_span_label_path_only(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
|
|
|
format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
|
|
|
|
);
|
|
|
|
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
} else {
|
|
|
|
if let Some((reported_place, _)) = self.move_error_reported.get(&move_out_indices) {
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.prefixes(*reported_place, PrefixSet::All).any(|p| p == used_place) {
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized place: error suppressed \
|
|
|
|
mois={:?}",
|
|
|
|
move_out_indices
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:16:43 -04:00
|
|
|
let is_partial_move = move_site_vec.iter().any(|move_site| {
|
|
|
|
let move_out = self.move_data.moves[(*move_site).moi];
|
|
|
|
let moved_place = &self.move_data.move_paths[move_out.path].place;
|
|
|
|
// `*(_1)` where `_1` is a `Box` is actually a move out.
|
2020-11-04 13:48:50 +01:00
|
|
|
let is_box_move = moved_place.as_ref().projection == [ProjectionElem::Deref]
|
2020-08-08 13:16:43 -04:00
|
|
|
&& self.body.local_decls[moved_place.local].ty.is_box();
|
|
|
|
|
|
|
|
!is_box_move
|
|
|
|
&& used_place != moved_place.as_ref()
|
|
|
|
&& used_place.is_prefix_of(moved_place.as_ref())
|
|
|
|
});
|
|
|
|
|
|
|
|
let partial_str = if is_partial_move { "partial " } else { "" };
|
|
|
|
let partially_str = if is_partial_move { "partially " } else { "" };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_act_on_moved_value(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
desired_action.as_noun(),
|
2020-08-08 13:16:43 -04:00
|
|
|
partially_str,
|
2019-07-11 19:25:37 +02:00
|
|
|
self.describe_place_with_options(moved_place, IncludingDowncast(true)),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2021-05-19 18:40:47 +02:00
|
|
|
let reinit_spans = maybe_reinitialized_locations
|
|
|
|
.iter()
|
|
|
|
.take(3)
|
|
|
|
.map(|loc| {
|
|
|
|
self.move_spans(self.move_data.move_paths[mpi].place.as_ref(), *loc)
|
|
|
|
.args_or_use()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Span>>();
|
|
|
|
let reinits = maybe_reinitialized_locations.len();
|
|
|
|
if reinits == 1 {
|
|
|
|
err.span_label(reinit_spans[0], "this reinitialization might get skipped");
|
|
|
|
} else if reinits > 1 {
|
|
|
|
err.span_note(
|
|
|
|
MultiSpan::from_spans(reinit_spans),
|
|
|
|
&if reinits <= 3 {
|
|
|
|
format!("these {} reinitializations might get skipped", reinits)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"these 3 reinitializations and {} other{} might get skipped",
|
|
|
|
reinits - 3,
|
|
|
|
if reinits == 4 { "" } else { "s" }
|
|
|
|
)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.add_moved_or_invoked_closure_note(location, used_place, &mut err);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let mut is_loop_move = false;
|
2021-01-12 17:31:13 +09:00
|
|
|
let mut in_pattern = false;
|
2020-08-08 13:16:43 -04:00
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
for move_site in &move_site_vec {
|
|
|
|
let move_out = self.move_data.moves[(*move_site).moi];
|
|
|
|
let moved_place = &self.move_data.move_paths[move_out.path].place;
|
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
|
2019-05-04 08:40:07 +01:00
|
|
|
let move_span = move_spans.args_or_use();
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let move_msg = if move_spans.for_closure() { " into closure" } else { "" };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2020-12-22 22:15:40 -05:00
|
|
|
let loop_message = if location == move_out.source || move_site.traversed_back_edge {
|
|
|
|
", in previous iteration of loop"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
2020-06-21 21:27:34 -04:00
|
|
|
if location == move_out.source {
|
2019-05-04 08:40:07 +01:00
|
|
|
is_loop_move = true;
|
2020-12-22 22:15:40 -05: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())
|
|
|
|
.map(|n| format!("`{}`", n))
|
|
|
|
.unwrap_or_else(|| "value".to_owned());
|
|
|
|
match kind {
|
|
|
|
FnSelfUseKind::FnOnceCall => {
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
FnSelfUseKind::Operator { self_arg } => {
|
|
|
|
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(
|
|
|
|
self_arg.span,
|
|
|
|
"calling this operator moves the left-hand side",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 09:37:47 -07:00
|
|
|
FnSelfUseKind::Normal {
|
|
|
|
self_arg,
|
|
|
|
implicit_into_iter,
|
|
|
|
is_option_or_result,
|
|
|
|
} => {
|
2020-12-22 22:15:40 -05:00
|
|
|
if implicit_into_iter {
|
2020-06-11 13:48:46 -04:00
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
2020-08-08 13:16:43 -04:00
|
|
|
&format!(
|
2020-12-22 22:15:40 -05:00
|
|
|
"{} {}moved due to this implicit call to `.into_iter()`{}",
|
|
|
|
place_name, partially_str, loop_message
|
2020-08-08 13:16:43 -04:00
|
|
|
),
|
2020-06-11 13:48:46 -04:00
|
|
|
);
|
2020-12-22 22:15:40 -05:00
|
|
|
} else {
|
2020-06-11 13:48:46 -04:00
|
|
|
err.span_label(
|
|
|
|
fn_call_span,
|
2020-08-08 13:16:43 -04:00
|
|
|
&format!(
|
2020-12-22 22:15:40 -05:00
|
|
|
"{} {}moved due to this method call{}",
|
|
|
|
place_name, partially_str, loop_message
|
2020-08-08 13:16:43 -04:00
|
|
|
),
|
2020-06-11 13:48:46 -04:00
|
|
|
);
|
|
|
|
}
|
2021-05-19 18:40:47 +02:00
|
|
|
if is_option_or_result && maybe_reinitialized_locations.is_empty() {
|
2021-04-18 09:37:47 -07:00
|
|
|
err.span_suggestion_verbose(
|
|
|
|
fn_call_span.shrink_to_lo(),
|
|
|
|
"consider calling `.as_ref()` to borrow the type's contents",
|
|
|
|
"as_ref().".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-12-22 22:15:40 -05:00
|
|
|
// Avoid pointing to the same function in multiple different
|
2021-03-09 20:12:47 +05:30
|
|
|
// error messages.
|
|
|
|
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span)
|
|
|
|
{
|
2020-12-22 22:15:40 -05:00
|
|
|
err.span_note(
|
2021-03-09 20:12:47 +05:30
|
|
|
self_arg.span,
|
|
|
|
&format!("this function takes ownership of the receiver `self`, which moves {}", place_name)
|
|
|
|
);
|
2020-06-11 13:48:46 -04:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 22:15:40 -05:00
|
|
|
// Deref::deref takes &self, which cannot cause a move
|
|
|
|
FnSelfUseKind::DerefCoercion { .. } => unreachable!(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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() {
|
2020-06-11 13:48:46 -04:00
|
|
|
move_spans.var_span_label(
|
|
|
|
&mut err,
|
2020-08-08 13:16:43 -04:00
|
|
|
format!(
|
|
|
|
"variable {}moved due to use{}",
|
|
|
|
partially_str,
|
|
|
|
move_spans.describe()
|
|
|
|
),
|
2021-03-17 02:51:27 -04:00
|
|
|
"moved",
|
2020-06-11 13:48:46 -04:00
|
|
|
);
|
|
|
|
}
|
2019-05-24 18:23:43 -07:00
|
|
|
}
|
2020-12-22 22:15:40 -05:00
|
|
|
|
2021-05-19 18:40:47 +02:00
|
|
|
if let (UseSpans::PatUse(span), []) =
|
|
|
|
(move_spans, &maybe_reinitialized_locations[..])
|
|
|
|
{
|
|
|
|
if maybe_reinitialized_locations.is_empty() {
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
span.shrink_to_lo(),
|
|
|
|
&format!(
|
|
|
|
"borrow this field in the pattern to avoid moving {}",
|
|
|
|
self.describe_place(moved_place.as_ref())
|
|
|
|
.map(|n| format!("`{}`", n))
|
|
|
|
.unwrap_or_else(|| "the value".to_string())
|
|
|
|
),
|
|
|
|
"ref ".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
in_pattern = true;
|
|
|
|
}
|
2020-06-19 22:45:09 -07:00
|
|
|
}
|
|
|
|
|
2020-06-11 13:48:46 -04:00
|
|
|
if let Some(DesugaringKind::ForLoop(_)) = move_span.desugaring_kind() {
|
2019-09-20 16:06:32 -07:00
|
|
|
let sess = self.infcx.tcx.sess;
|
2021-04-06 21:39:44 +02:00
|
|
|
let ty = used_place.ty(self.body, self.infcx.tcx).ty;
|
|
|
|
// If we have a `&mut` ref, we need to reborrow.
|
|
|
|
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
|
|
|
|
// 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()),
|
|
|
|
),
|
2021-07-21 22:43:19 +02:00
|
|
|
"&mut *".to_string(),
|
2021-04-06 21:39:44 +02:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if let Ok(snippet) = sess.source_map().span_to_snippet(move_span) {
|
2019-05-24 18:23:43 -07:00
|
|
|
err.span_suggestion(
|
|
|
|
move_span,
|
|
|
|
"consider borrowing to avoid moving into the for loop",
|
|
|
|
format!("&{}", snippet),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
use_spans.var_span_label_path_only(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
|
|
|
format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
|
|
|
|
);
|
|
|
|
|
|
|
|
if !is_loop_move {
|
|
|
|
err.span_label(
|
|
|
|
span,
|
|
|
|
format!(
|
2020-08-08 13:16:43 -04:00
|
|
|
"value {} here after {}move",
|
2019-05-04 08:40:07 +01:00
|
|
|
desired_action.as_verb_in_past_tense(),
|
2020-08-08 13:16:43 -04:00
|
|
|
partial_str
|
2019-05-04 08:40:07 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-16 11:34:22 +01:00
|
|
|
let ty = used_place.ty(self.body, self.infcx.tcx).ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
let needs_note = match ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
ty::Closure(id, _) => {
|
2020-07-17 08:47:04 +00:00
|
|
|
let tables = self.infcx.tcx.typeck(id.expect_local());
|
2020-08-12 12:22:56 +02:00
|
|
|
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(id.expect_local());
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
tables.closure_kind_origins().get(hir_id).is_none()
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
|
2020-06-19 22:45:09 -07:00
|
|
|
let mpi = self.move_data.moves[move_out_indices[0]].path;
|
|
|
|
let place = &self.move_data.move_paths[mpi].place;
|
|
|
|
let ty = place.ty(self.body, self.infcx.tcx).ty;
|
|
|
|
|
2021-01-12 17:31:13 +09:00
|
|
|
// If we're in pattern, we do nothing in favor of the previous suggestion (#80913).
|
|
|
|
if is_loop_move & !in_pattern {
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
|
2020-06-19 22:45:09 -07:00
|
|
|
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
span.shrink_to_lo(),
|
|
|
|
&format!(
|
|
|
|
"consider creating a fresh reborrow of {} here",
|
|
|
|
self.describe_place(moved_place)
|
|
|
|
.map(|n| format!("`{}`", n))
|
|
|
|
.unwrap_or_else(|| "the mutable reference".to_string()),
|
|
|
|
),
|
|
|
|
"&mut *".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2020-06-19 22:45:09 -07:00
|
|
|
if needs_note {
|
2019-07-11 19:25:37 +02:00
|
|
|
let opt_name =
|
2019-07-21 22:38:30 +02:00
|
|
|
self.describe_place_with_options(place.as_ref(), IncludingDowncast(true));
|
2019-05-04 08:40:07 +01:00
|
|
|
let note_msg = match opt_name {
|
|
|
|
Some(ref name) => format!("`{}`", name),
|
|
|
|
None => "value".to_owned(),
|
|
|
|
};
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Param(param_ty) = ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
let tcx = self.infcx.tcx;
|
2020-10-04 17:23:02 -07:00
|
|
|
let generics = tcx.generics_of(self.mir_def_id());
|
2019-11-19 16:43:24 -08:00
|
|
|
let param = generics.type_param(¶m_ty, tcx);
|
2020-10-04 17:23:02 -07:00
|
|
|
if let Some(generics) = tcx
|
|
|
|
.hir()
|
|
|
|
.get_generics(tcx.closure_base_def_id(self.mir_def_id().to_def_id()))
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-12-03 15:02:27 -08:00
|
|
|
suggest_constraining_type_param(
|
2020-02-02 02:03:54 -08:00
|
|
|
tcx,
|
2019-12-03 15:02:27 -08:00
|
|
|
generics,
|
|
|
|
&mut err,
|
|
|
|
¶m.name.as_str(),
|
|
|
|
"Copy",
|
2020-02-02 02:03:54 -08:00
|
|
|
None,
|
2019-12-03 15:02:27 -08:00
|
|
|
);
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
2019-10-20 16:09:36 -04:00
|
|
|
let span = if let Some(local) = place.as_local() {
|
2019-11-06 12:00:46 -05:00
|
|
|
let decl = &self.body.local_decls[local];
|
2019-05-04 22:28:22 +01:00
|
|
|
Some(decl.source_info.span)
|
2019-05-04 08:40:07 +01:00
|
|
|
} else {
|
2019-05-04 22:28:22 +01:00
|
|
|
None
|
|
|
|
};
|
2020-08-08 13:16:43 -04:00
|
|
|
self.note_type_does_not_implement_copy(&mut err, ¬e_msg, ty, span, partial_str);
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-25 07:04:13 -04:00
|
|
|
if let UseSpans::FnSelfUse {
|
|
|
|
kind: FnSelfUseKind::DerefCoercion { deref_target, deref_target_ty },
|
|
|
|
..
|
|
|
|
} = use_spans
|
|
|
|
{
|
|
|
|
err.note(&format!(
|
|
|
|
"{} occurs due to deref coercion to `{}`",
|
|
|
|
desired_action.as_noun(),
|
|
|
|
deref_target_ty
|
|
|
|
));
|
|
|
|
|
|
|
|
err.span_note(deref_target, "deref defined here");
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if let Some((_, mut old_err)) =
|
|
|
|
self.move_error_reported.insert(move_out_indices, (used_place, err))
|
2019-05-04 08:40:07 +01:00
|
|
|
{
|
|
|
|
// Cancel the old error so it doesn't ICE.
|
|
|
|
old_err.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_move_out_while_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
) {
|
|
|
|
debug!(
|
|
|
|
"report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
|
|
|
|
location, place, span, borrow
|
|
|
|
);
|
2020-03-26 01:55:16 +01:00
|
|
|
let value_msg = self.describe_any_place(place.as_ref());
|
|
|
|
let borrow_msg = self.describe_any_place(borrow.borrowed_place.as_ref());
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let borrow_spans = self.retrieve_borrow_spans(borrow);
|
|
|
|
let borrow_span = borrow_spans.args_or_use();
|
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
let move_spans = self.move_spans(place.as_ref(), location);
|
2019-05-04 08:40:07 +01:00
|
|
|
let span = move_spans.args_or_use();
|
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
let mut err =
|
2020-03-26 01:55:16 +01:00
|
|
|
self.cannot_move_when_borrowed(span, &self.describe_any_place(place.as_ref()));
|
2019-05-04 08:40:07 +01:00
|
|
|
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
|
|
|
|
err.span_label(span, format!("move out of {} occurs here", value_msg));
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
borrow_spans.var_span_label_path_only(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
2019-12-22 17:42:04 -05:00
|
|
|
format!("borrow occurs due to use{}", borrow_spans.describe()),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
move_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!("move occurs due to use{}", move_spans.describe()),
|
|
|
|
"moved",
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.explain_why_borrow_contains_point(location, borrow, None)
|
|
|
|
.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
&self.local_names,
|
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
Some(borrow_span),
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_use_while_mutably_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, _span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let borrow_spans = self.retrieve_borrow_spans(borrow);
|
|
|
|
let borrow_span = borrow_spans.args_or_use();
|
|
|
|
|
|
|
|
// Conflicting borrows are reported separately, so only check for move
|
|
|
|
// captures.
|
2019-07-21 22:38:30 +02:00
|
|
|
let use_spans = self.move_spans(place.as_ref(), location);
|
2019-05-04 08:40:07 +01:00
|
|
|
let span = use_spans.var_or_use();
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
// If the attempted use is in a closure then we do not care about the path span of the place we are currently trying to use
|
|
|
|
// we call `var_span_label` on `borrow_spans` to annotate if the existing borrow was in a closure
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_use_when_mutably_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
2020-03-26 01:55:16 +01:00
|
|
|
&self.describe_any_place(place.as_ref()),
|
2019-05-04 08:40:07 +01:00
|
|
|
borrow_span,
|
2020-03-26 01:55:16 +01:00
|
|
|
&self.describe_any_place(borrow.borrowed_place.as_ref()),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
borrow_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
{
|
|
|
|
let place = &borrow.borrowed_place;
|
|
|
|
let desc_place = self.describe_any_place(place.as_ref());
|
|
|
|
format!("borrow occurs due to use of {}{}", desc_place, borrow_spans.describe())
|
|
|
|
},
|
|
|
|
"mutable",
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
self.explain_why_borrow_contains_point(location, borrow, None)
|
2018-05-16 18:58:54 +03:00
|
|
|
.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2018-05-16 18:58:54 +03:00
|
|
|
&self.local_names,
|
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2018-05-16 18:58:54 +03:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_conflicting_borrow(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
gen_borrow_kind: BorrowKind,
|
|
|
|
issued_borrow: &BorrowData<'tcx>,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let issued_spans = self.retrieve_borrow_spans(issued_borrow);
|
|
|
|
let issued_span = issued_spans.args_or_use();
|
|
|
|
|
|
|
|
let borrow_spans = self.borrow_spans(span, location);
|
|
|
|
let span = borrow_spans.args_or_use();
|
|
|
|
|
|
|
|
let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
|
|
|
|
"generator"
|
|
|
|
} else {
|
|
|
|
"closure"
|
|
|
|
};
|
|
|
|
|
|
|
|
let (desc_place, msg_place, msg_borrow, union_type_name) =
|
2020-03-30 17:49:33 -03:00
|
|
|
self.describe_place_for_conflicting_borrow(place, issued_borrow.borrowed_place);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let explanation = self.explain_why_borrow_contains_point(location, issued_borrow, None);
|
2019-12-22 17:42:04 -05:00
|
|
|
let second_borrow_desc = if explanation.is_explained() { "second " } else { "" };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
// FIXME: supply non-"" `opt_via` when appropriate
|
|
|
|
let first_borrow_desc;
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut err = match (gen_borrow_kind, issued_borrow.kind) {
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Shared, BorrowKind::Mut { .. }) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "mutable ";
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_reborrow_already_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
&desc_place,
|
|
|
|
&msg_place,
|
2019-10-19 21:00:21 +01:00
|
|
|
"immutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
issued_span,
|
|
|
|
"it",
|
2019-10-19 21:00:21 +01:00
|
|
|
"mutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
&msg_borrow,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Mut { .. }, BorrowKind::Shared) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "immutable ";
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_reborrow_already_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
&desc_place,
|
|
|
|
&msg_place,
|
2019-10-19 21:00:21 +01:00
|
|
|
"mutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
issued_span,
|
|
|
|
"it",
|
2019-10-19 21:00:21 +01:00
|
|
|
"immutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
&msg_borrow,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Mut { .. }, BorrowKind::Mut { .. }) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "first ";
|
2020-02-03 17:58:28 -08:00
|
|
|
let mut err = self.cannot_mutably_borrow_multiply(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
&desc_place,
|
|
|
|
&msg_place,
|
|
|
|
issued_span,
|
|
|
|
&msg_borrow,
|
|
|
|
None,
|
2020-02-03 17:58:28 -08:00
|
|
|
);
|
|
|
|
self.suggest_split_at_mut_if_applicable(
|
|
|
|
&mut err,
|
2020-03-31 12:19:29 -03:00
|
|
|
place,
|
|
|
|
issued_borrow.borrowed_place,
|
2020-02-03 17:58:28 -08:00
|
|
|
);
|
|
|
|
err
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Unique, BorrowKind::Unique) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "first ";
|
2019-12-22 17:42:04 -05:00
|
|
|
self.cannot_uniquely_borrow_by_two_closures(span, &desc_place, issued_span, None)
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:38:52 -07:00
|
|
|
(BorrowKind::Mut { .. } | BorrowKind::Unique, BorrowKind::Shallow) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
if let Some(immutable_section_description) =
|
2020-03-31 12:19:29 -03:00
|
|
|
self.classify_immutable_section(issued_borrow.assigned_place)
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-10-19 21:00:21 +01:00
|
|
|
let mut err = self.cannot_mutate_in_immutable_section(
|
|
|
|
span,
|
|
|
|
issued_span,
|
|
|
|
&desc_place,
|
|
|
|
immutable_section_description,
|
|
|
|
"mutably borrow",
|
|
|
|
);
|
|
|
|
borrow_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"borrow occurs due to use of {}{}",
|
2019-10-19 21:00:21 +01:00
|
|
|
desc_place,
|
|
|
|
borrow_spans.describe(),
|
|
|
|
),
|
2021-03-17 02:51:27 -04:00
|
|
|
"immutable",
|
2019-10-19 21:00:21 +01:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
return err;
|
|
|
|
} else {
|
|
|
|
first_borrow_desc = "immutable ";
|
|
|
|
self.cannot_reborrow_already_borrowed(
|
|
|
|
span,
|
|
|
|
&desc_place,
|
|
|
|
&msg_place,
|
|
|
|
"mutable",
|
|
|
|
issued_span,
|
|
|
|
"it",
|
|
|
|
"immutable",
|
|
|
|
&msg_borrow,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Unique, _) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "first ";
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_uniquely_borrow_by_one_closure(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
container_name,
|
|
|
|
&desc_place,
|
|
|
|
"",
|
|
|
|
issued_span,
|
|
|
|
"it",
|
|
|
|
"",
|
|
|
|
None,
|
|
|
|
)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Shared, BorrowKind::Unique) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "first ";
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_reborrow_already_uniquely_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
container_name,
|
|
|
|
&desc_place,
|
|
|
|
"",
|
2019-10-19 21:00:21 +01:00
|
|
|
"immutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
issued_span,
|
|
|
|
"",
|
|
|
|
None,
|
|
|
|
second_borrow_desc,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
(BorrowKind::Mut { .. }, BorrowKind::Unique) => {
|
2019-05-04 08:40:07 +01:00
|
|
|
first_borrow_desc = "first ";
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_reborrow_already_uniquely_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
container_name,
|
|
|
|
&desc_place,
|
|
|
|
"",
|
2019-10-19 21:00:21 +01:00
|
|
|
"mutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
issued_span,
|
|
|
|
"",
|
|
|
|
None,
|
|
|
|
second_borrow_desc,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:38:52 -07:00
|
|
|
(BorrowKind::Shared, BorrowKind::Shared | BorrowKind::Shallow)
|
|
|
|
| (
|
|
|
|
BorrowKind::Shallow,
|
|
|
|
BorrowKind::Mut { .. }
|
|
|
|
| BorrowKind::Unique
|
|
|
|
| BorrowKind::Shared
|
|
|
|
| BorrowKind::Shallow,
|
|
|
|
) => unreachable!(),
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if issued_spans == borrow_spans {
|
|
|
|
borrow_spans.var_span_label(
|
|
|
|
&mut err,
|
2021-03-17 02:51:27 -04:00
|
|
|
format!("borrows occur due to use of {}{}", desc_place, borrow_spans.describe(),),
|
|
|
|
gen_borrow_kind.describe_mutability(),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let borrow_place = &issued_borrow.borrowed_place;
|
2020-03-26 01:55:16 +01:00
|
|
|
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
|
2019-05-04 08:40:07 +01:00
|
|
|
issued_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"first borrow occurs due to use of {}{}",
|
2019-05-04 08:40:07 +01:00
|
|
|
borrow_place_desc,
|
|
|
|
issued_spans.describe(),
|
|
|
|
),
|
2021-03-17 02:51:27 -04:00
|
|
|
issued_borrow.kind.describe_mutability(),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
borrow_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"second borrow occurs due to use of {}{}",
|
2019-05-04 08:40:07 +01:00
|
|
|
desc_place,
|
|
|
|
borrow_spans.describe(),
|
|
|
|
),
|
2021-03-17 02:51:27 -04:00
|
|
|
gen_borrow_kind.describe_mutability(),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if union_type_name != "" {
|
|
|
|
err.note(&format!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"{} is a field of the union `{}`, so it overlaps the field {}",
|
2019-05-04 08:40:07 +01:00
|
|
|
msg_place, union_type_name, msg_borrow,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
explanation.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2018-05-16 18:58:54 +03:00
|
|
|
&self.local_names,
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
|
|
|
first_borrow_desc,
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
Some((issued_span, span)),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2020-02-03 17:58:28 -08:00
|
|
|
fn suggest_split_at_mut_if_applicable(
|
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
2020-03-31 12:19:29 -03:00
|
|
|
place: Place<'tcx>,
|
|
|
|
borrowed_place: Place<'tcx>,
|
2020-02-03 17:58:28 -08:00
|
|
|
) {
|
2020-03-22 13:36:56 +01:00
|
|
|
if let ([ProjectionElem::Index(_)], [ProjectionElem::Index(_)]) =
|
|
|
|
(&place.projection[..], &borrowed_place.projection[..])
|
|
|
|
{
|
|
|
|
err.help(
|
|
|
|
"consider using `.split_at_mut(position)` or similar method to obtain \
|
2020-02-03 17:58:28 -08:00
|
|
|
two mutable non-overlapping sub-slices",
|
2020-03-22 13:36:56 +01:00
|
|
|
);
|
2020-02-03 17:58:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
/// Returns the description of the root place for a conflicting borrow and the full
|
|
|
|
/// descriptions of the places that caused the conflict.
|
|
|
|
///
|
|
|
|
/// In the simplest case, where there are no unions involved, if a mutable borrow of `x` is
|
|
|
|
/// attempted while a shared borrow is live, then this function will return:
|
|
|
|
///
|
|
|
|
/// ("x", "", "")
|
|
|
|
///
|
|
|
|
/// In the simple union case, if a mutable borrow of a union field `x.z` is attempted while
|
|
|
|
/// a shared borrow of another field `x.y`, then this function will return:
|
|
|
|
///
|
|
|
|
/// ("x", "x.z", "x.y")
|
|
|
|
///
|
|
|
|
/// In the more complex union case, where the union is a field of a struct, then if a mutable
|
|
|
|
/// borrow of a union field in a struct `x.u.z` is attempted while a shared borrow of
|
|
|
|
/// another field `x.u.y`, then this function will return:
|
|
|
|
///
|
|
|
|
/// ("x.u", "x.u.z", "x.u.y")
|
|
|
|
///
|
|
|
|
/// This is used when creating error messages like below:
|
|
|
|
///
|
2020-05-01 22:28:15 +02:00
|
|
|
/// ```text
|
|
|
|
/// cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
|
|
|
|
/// mutable (via `a.u.s.b`) [E0502]
|
|
|
|
/// ```
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn describe_place_for_conflicting_borrow(
|
2019-05-04 08:40:07 +01:00
|
|
|
&self,
|
2020-03-30 17:49:33 -03:00
|
|
|
first_borrowed_place: Place<'tcx>,
|
|
|
|
second_borrowed_place: Place<'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
) -> (String, String, String, String) {
|
|
|
|
// Define a small closure that we can use to check if the type of a place
|
|
|
|
// is a union.
|
2021-01-02 19:31:37 +01:00
|
|
|
let union_ty = |place_base| {
|
2021-01-16 11:34:22 +01:00
|
|
|
// Need to use fn call syntax `PlaceRef::ty` to determine the type of `place_base`;
|
|
|
|
// using a type annotation in the closure argument instead leads to a lifetime error.
|
2021-01-02 19:31:37 +01:00
|
|
|
let ty = PlaceRef::ty(&place_base, self.body, self.infcx.tcx).ty;
|
2019-05-28 15:18:37 +02:00
|
|
|
ty.ty_adt_def().filter(|adt| adt.is_union()).map(|_| ty)
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Start with an empty tuple, so we can use the functions on `Option` to reduce some
|
|
|
|
// code duplication (particularly around returning an empty description in the failure
|
|
|
|
// case).
|
|
|
|
Some(())
|
|
|
|
.filter(|_| {
|
|
|
|
// If we have a conflicting borrow of the same place, then we don't want to add
|
|
|
|
// an extraneous "via x.y" to our diagnostics, so filter out this case.
|
|
|
|
first_borrowed_place != second_borrowed_place
|
|
|
|
})
|
|
|
|
.and_then(|_| {
|
|
|
|
// We're going to want to traverse the first borrowed place to see if we can find
|
|
|
|
// field access to a union. If we find that, then we will keep the place of the
|
|
|
|
// union being accessed and the field that was being accessed so we can check the
|
2021-08-22 14:46:15 +02:00
|
|
|
// second borrowed place for the same union and an access to a different field.
|
2021-01-02 19:31:37 +01:00
|
|
|
for (place_base, elem) in first_borrowed_place.iter_projections().rev() {
|
2019-05-04 08:40:07 +01:00
|
|
|
match elem {
|
2021-01-02 19:31:37 +01:00
|
|
|
ProjectionElem::Field(field, _) if union_ty(place_base).is_some() => {
|
|
|
|
return Some((place_base, field));
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.and_then(|(target_base, target_field)| {
|
|
|
|
// With the place of a union and a field access into it, we traverse the second
|
2021-08-22 14:46:15 +02:00
|
|
|
// borrowed place and look for an access to a different field of the same union.
|
2021-01-02 19:31:37 +01:00
|
|
|
for (place_base, elem) in second_borrowed_place.iter_projections().rev() {
|
2019-05-28 15:18:37 +02:00
|
|
|
if let ProjectionElem::Field(field, _) = elem {
|
2021-01-02 19:31:37 +01:00
|
|
|
if let Some(union_ty) = union_ty(place_base) {
|
|
|
|
if field != target_field && place_base == target_base {
|
2019-05-28 15:18:37 +02:00
|
|
|
return Some((
|
2021-01-02 19:31:37 +01:00
|
|
|
self.describe_any_place(place_base),
|
2020-03-26 01:55:16 +01:00
|
|
|
self.describe_any_place(first_borrowed_place.as_ref()),
|
|
|
|
self.describe_any_place(second_borrowed_place.as_ref()),
|
2019-05-28 15:18:37 +02:00
|
|
|
union_ty.to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
// If we didn't find a field access into a union, or both places match, then
|
|
|
|
// only return the description of the first place.
|
2019-05-28 16:47:59 +02:00
|
|
|
(
|
2020-03-26 01:55:16 +01:00
|
|
|
self.describe_any_place(first_borrowed_place.as_ref()),
|
2019-05-28 16:47:59 +02:00
|
|
|
"".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
)
|
2019-05-04 08:40:07 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
|
|
|
|
///
|
|
|
|
/// This means that some data referenced by `borrow` needs to live
|
|
|
|
/// past the point where the StorageDeadOrDrop of `place` occurs.
|
|
|
|
/// This is usually interpreted as meaning that `place` has too
|
|
|
|
/// short a lifetime. (But sometimes it is more useful to report
|
|
|
|
/// it as a more direct conflict between the execution of a
|
|
|
|
/// `Drop::drop` with an aliasing borrow.)
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_borrowed_value_does_not_live_long_enough(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
2020-03-30 17:49:33 -03:00
|
|
|
place_span: (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
kind: Option<WriteKind>,
|
|
|
|
) {
|
|
|
|
debug!(
|
|
|
|
"report_borrowed_value_does_not_live_long_enough(\
|
|
|
|
{:?}, {:?}, {:?}, {:?}\
|
|
|
|
)",
|
|
|
|
location, borrow, place_span, kind
|
|
|
|
);
|
|
|
|
|
|
|
|
let drop_span = place_span.1;
|
2019-12-22 17:42:04 -05:00
|
|
|
let root_place =
|
|
|
|
self.prefixes(borrow.borrowed_place.as_ref(), PrefixSet::All).last().unwrap();
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let borrow_spans = self.retrieve_borrow_spans(borrow);
|
2021-03-17 02:51:27 -04:00
|
|
|
let borrow_span = borrow_spans.var_or_use_path_span();
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
assert!(root_place.projection.is_empty());
|
2020-01-14 02:10:05 -03:00
|
|
|
let proper_span = self.body.local_decls[root_place.local].source_info.span;
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:11:04 -04:00
|
|
|
let root_place_projection = self.infcx.tcx.intern_place_elems(root_place.projection);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.access_place_error_reported.contains(&(
|
2020-01-28 12:17:02 -03:00
|
|
|
Place { local: root_place.local, projection: root_place_projection },
|
2019-12-22 17:42:04 -05:00
|
|
|
borrow_span,
|
|
|
|
)) {
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
|
|
|
"suppressing access_place error when borrow doesn't live long enough for {:?}",
|
|
|
|
borrow_span
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.access_place_error_reported.insert((
|
2020-01-28 12:17:02 -03:00
|
|
|
Place { local: root_place.local, projection: root_place_projection },
|
2019-12-22 17:42:04 -05:00
|
|
|
borrow_span,
|
|
|
|
));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
let borrowed_local = borrow.borrowed_place.local;
|
|
|
|
if self.body.local_decls[borrowed_local].is_ref_to_thread_local() {
|
|
|
|
let err =
|
|
|
|
self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span);
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-18 23:04:06 +00:00
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
if let StorageDeadOrDrop::Destructor(dropped_ty) =
|
2019-07-21 22:38:30 +02:00
|
|
|
self.classify_drop_access_kind(borrow.borrowed_place.as_ref())
|
2019-05-04 08:40:07 +01:00
|
|
|
{
|
|
|
|
// If a borrow of path `B` conflicts with drop of `D` (and
|
|
|
|
// we're not in the uninteresting case where `B` is a
|
|
|
|
// prefix of `D`), then report this as a more interesting
|
|
|
|
// destructor conflict.
|
2019-07-21 22:38:30 +02:00
|
|
|
if !borrow.borrowed_place.as_ref().is_prefix_of(place_span.0.as_ref()) {
|
2019-05-04 08:40:07 +01:00
|
|
|
self.report_borrow_conflicts_with_destructor(
|
|
|
|
location, borrow, place_span, kind, dropped_ty,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
let place_desc = self.describe_place(borrow.borrowed_place.as_ref());
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
|
|
|
|
let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);
|
|
|
|
|
2019-10-06 21:58:32 +08:00
|
|
|
debug!(
|
|
|
|
"report_borrowed_value_does_not_live_long_enough(place_desc: {:?}, explanation: {:?})",
|
2019-12-22 17:42:04 -05:00
|
|
|
place_desc, explanation
|
2019-10-06 21:58:32 +08:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
let err = match (place_desc, explanation) {
|
|
|
|
// If the outlives constraint comes from inside the closure,
|
|
|
|
// for example:
|
|
|
|
//
|
|
|
|
// let x = 0;
|
|
|
|
// let y = &x;
|
|
|
|
// Box::new(|| y) as Box<Fn() -> &'static i32>
|
|
|
|
//
|
|
|
|
// then just use the normal error. The closure isn't escaping
|
|
|
|
// and `move` will not help here.
|
|
|
|
(
|
|
|
|
Some(ref name),
|
|
|
|
BorrowExplanation::MustBeValidFor {
|
2020-04-07 23:57:26 +02:00
|
|
|
category:
|
|
|
|
category
|
|
|
|
@
|
2020-05-23 21:40:55 -04:00
|
|
|
(ConstraintCategory::Return(_)
|
2020-04-07 23:57:26 +02:00
|
|
|
| ConstraintCategory::CallArgument
|
|
|
|
| ConstraintCategory::OpaqueType),
|
2019-05-04 08:40:07 +01:00
|
|
|
from_closure: false,
|
|
|
|
ref region_name,
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
},
|
2020-04-07 23:57:26 +02:00
|
|
|
) if borrow_spans.for_generator() | borrow_spans.for_closure() => self
|
|
|
|
.report_escaping_closure_capture(
|
|
|
|
borrow_spans,
|
|
|
|
borrow_span,
|
|
|
|
region_name,
|
|
|
|
category,
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
2020-04-07 23:57:26 +02:00
|
|
|
&format!("`{}`", name),
|
|
|
|
),
|
2019-05-04 08:40:07 +01:00
|
|
|
(
|
|
|
|
ref name,
|
|
|
|
BorrowExplanation::MustBeValidFor {
|
|
|
|
category: ConstraintCategory::Assignment,
|
|
|
|
from_closure: false,
|
2019-12-22 17:42:04 -05:00
|
|
|
region_name:
|
|
|
|
RegionName {
|
|
|
|
source:
|
|
|
|
RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
|
|
|
|
..
|
|
|
|
},
|
2019-05-04 08:40:07 +01:00
|
|
|
span,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
|
|
|
|
(Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
|
|
|
|
location,
|
|
|
|
&name,
|
|
|
|
&borrow,
|
|
|
|
drop_span,
|
|
|
|
borrow_spans,
|
|
|
|
explanation,
|
|
|
|
),
|
|
|
|
(None, explanation) => self.report_temporary_value_does_not_live_long_enough(
|
|
|
|
location,
|
|
|
|
&borrow,
|
|
|
|
drop_span,
|
|
|
|
borrow_spans,
|
|
|
|
proper_span,
|
|
|
|
explanation,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_local_value_does_not_live_long_enough(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
name: &str,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
drop_span: Span,
|
2020-07-25 07:04:13 -04:00
|
|
|
borrow_spans: UseSpans<'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
explanation: BorrowExplanation,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
debug!(
|
|
|
|
"report_local_value_does_not_live_long_enough(\
|
2019-05-04 10:10:37 +01:00
|
|
|
{:?}, {:?}, {:?}, {:?}, {:?}\
|
2019-05-04 08:40:07 +01:00
|
|
|
)",
|
2019-05-04 10:10:37 +01:00
|
|
|
location, name, borrow, drop_span, borrow_spans
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
let borrow_span = borrow_spans.var_or_use_path_span();
|
2019-05-04 08:40:07 +01:00
|
|
|
if let BorrowExplanation::MustBeValidFor {
|
|
|
|
category,
|
|
|
|
span,
|
|
|
|
ref opt_place_desc,
|
|
|
|
from_closure: false,
|
|
|
|
..
|
2019-12-22 17:42:04 -05:00
|
|
|
} = explanation
|
|
|
|
{
|
2019-05-04 08:40:07 +01:00
|
|
|
if let Some(diag) = self.try_report_cannot_return_reference_to_local(
|
|
|
|
borrow,
|
|
|
|
borrow_span,
|
|
|
|
span,
|
|
|
|
category,
|
|
|
|
opt_place_desc.as_ref(),
|
|
|
|
) {
|
|
|
|
return diag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut err = self.path_does_not_live_long_enough(borrow_span, &format!("`{}`", name));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
|
|
|
|
let region_name = annotation.emit(self, &mut err);
|
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
borrow_span,
|
|
|
|
format!("`{}` would have to be valid for `{}`...", name, region_name),
|
|
|
|
);
|
|
|
|
|
2020-10-04 17:23:02 -07:00
|
|
|
let fn_hir_id = self.mir_hir_id();
|
2020-05-11 10:29:05 +02:00
|
|
|
err.span_label(
|
|
|
|
drop_span,
|
|
|
|
format!(
|
|
|
|
"...but `{}` will be dropped here, when the {} returns",
|
|
|
|
name,
|
|
|
|
self.infcx
|
|
|
|
.tcx
|
|
|
|
.hir()
|
|
|
|
.opt_name(fn_hir_id)
|
|
|
|
.map(|name| format!("function `{}`", name))
|
|
|
|
.unwrap_or_else(|| {
|
2020-08-03 00:49:11 +02:00
|
|
|
match &self
|
|
|
|
.infcx
|
|
|
|
.tcx
|
2020-10-04 17:23:02 -07:00
|
|
|
.typeck(self.mir_def_id())
|
2020-08-03 00:49:11 +02:00
|
|
|
.node_type(fn_hir_id)
|
|
|
|
.kind()
|
2020-05-11 10:29:05 +02:00
|
|
|
{
|
|
|
|
ty::Closure(..) => "enclosing closure",
|
|
|
|
ty::Generator(..) => "enclosing generator",
|
|
|
|
kind => bug!("expected closure or generator, found {:?}", kind),
|
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
})
|
|
|
|
),
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2020-05-11 10:29:05 +02:00
|
|
|
err.note(
|
|
|
|
"functions cannot return a borrow to data owned within the function's scope, \
|
|
|
|
functions can only return borrows to data passed as arguments",
|
|
|
|
);
|
|
|
|
err.note(
|
|
|
|
"to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
|
|
|
|
references-and-borrowing.html#dangling-references>",
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
if let BorrowExplanation::MustBeValidFor { .. } = explanation {
|
|
|
|
} else {
|
|
|
|
explanation.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2018-05-16 18:58:54 +03:00
|
|
|
&self.local_names,
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err.span_label(borrow_span, "borrowed value does not live long enough");
|
2019-12-22 17:42:04 -05:00
|
|
|
err.span_label(drop_span, format!("`{}` dropped here while still borrowed", name));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let within = if borrow_spans.for_generator() { " by generator" } else { "" };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
borrow_spans.args_span_label(&mut err, format!("value captured here{}", within));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
explanation.add_explanation_to_diagnostic(
|
2019-12-22 17:42:04 -05:00
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
&self.local_names,
|
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_borrow_conflicts_with_destructor(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, drop_span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
kind: Option<WriteKind>,
|
|
|
|
dropped_ty: Ty<'tcx>,
|
|
|
|
) {
|
|
|
|
debug!(
|
|
|
|
"report_borrow_conflicts_with_destructor(\
|
|
|
|
{:?}, {:?}, ({:?}, {:?}), {:?}\
|
|
|
|
)",
|
|
|
|
location, borrow, place, drop_span, kind,
|
|
|
|
);
|
|
|
|
|
|
|
|
let borrow_spans = self.retrieve_borrow_spans(borrow);
|
|
|
|
let borrow_span = borrow_spans.var_or_use();
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_borrow_across_destructor(borrow_span);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
let what_was_dropped = match self.describe_place(place.as_ref()) {
|
2019-10-22 11:04:25 +11:00
|
|
|
Some(name) => format!("`{}`", name),
|
2019-05-04 08:40:07 +01:00
|
|
|
None => String::from("temporary value"),
|
|
|
|
};
|
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
let label = match self.describe_place(borrow.borrowed_place.as_ref()) {
|
2019-05-04 08:40:07 +01:00
|
|
|
Some(borrowed) => format!(
|
|
|
|
"here, drop of {D} needs exclusive access to `{B}`, \
|
|
|
|
because the type `{T}` implements the `Drop` trait",
|
|
|
|
D = what_was_dropped,
|
|
|
|
T = dropped_ty,
|
|
|
|
B = borrowed
|
|
|
|
),
|
|
|
|
None => format!(
|
|
|
|
"here is drop of {D}; whose type `{T}` implements the `Drop` trait",
|
|
|
|
D = what_was_dropped,
|
|
|
|
T = dropped_ty
|
|
|
|
),
|
|
|
|
};
|
|
|
|
err.span_label(drop_span, label);
|
|
|
|
|
|
|
|
// Only give this note and suggestion if they could be relevant.
|
|
|
|
let explanation =
|
|
|
|
self.explain_why_borrow_contains_point(location, borrow, kind.map(|k| (k, place)));
|
|
|
|
match explanation {
|
|
|
|
BorrowExplanation::UsedLater { .. }
|
|
|
|
| BorrowExplanation::UsedLaterWhenDropped { .. } => {
|
|
|
|
err.note("consider using a `let` binding to create a longer lived value");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:58:54 +03:00
|
|
|
explanation.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2018-05-16 18:58:54 +03:00
|
|
|
&self.local_names,
|
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2018-05-16 18:58:54 +03:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_thread_local_value_does_not_live_long_enough(
|
|
|
|
&mut self,
|
|
|
|
drop_span: Span,
|
|
|
|
borrow_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
debug!(
|
|
|
|
"report_thread_local_value_does_not_live_long_enough(\
|
|
|
|
{:?}, {:?}\
|
|
|
|
)",
|
|
|
|
drop_span, borrow_span
|
|
|
|
);
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
borrow_span,
|
|
|
|
"thread-local variables cannot be borrowed beyond the end of the function",
|
|
|
|
);
|
|
|
|
err.span_label(drop_span, "end of enclosing function is here");
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_temporary_value_does_not_live_long_enough(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
drop_span: Span,
|
2020-07-25 07:04:13 -04:00
|
|
|
borrow_spans: UseSpans<'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
proper_span: Span,
|
|
|
|
explanation: BorrowExplanation,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
debug!(
|
|
|
|
"report_temporary_value_does_not_live_long_enough(\
|
2019-05-04 10:10:37 +01:00
|
|
|
{:?}, {:?}, {:?}, {:?}\
|
2019-05-04 08:40:07 +01:00
|
|
|
)",
|
2019-05-04 10:10:37 +01:00
|
|
|
location, borrow, drop_span, proper_span
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } =
|
|
|
|
explanation
|
|
|
|
{
|
2019-05-04 08:40:07 +01:00
|
|
|
if let Some(diag) = self.try_report_cannot_return_reference_to_local(
|
|
|
|
borrow,
|
|
|
|
proper_span,
|
|
|
|
span,
|
|
|
|
category,
|
|
|
|
None,
|
|
|
|
) {
|
|
|
|
return diag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.temporary_value_borrowed_for_too_long(proper_span);
|
2019-12-22 17:42:04 -05:00
|
|
|
err.span_label(proper_span, "creates a temporary which is freed while still in use");
|
|
|
|
err.span_label(drop_span, "temporary value is freed at the end of this statement");
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
match explanation {
|
|
|
|
BorrowExplanation::UsedLater(..)
|
|
|
|
| BorrowExplanation::UsedLaterInLoop(..)
|
|
|
|
| BorrowExplanation::UsedLaterWhenDropped { .. } => {
|
|
|
|
// Only give this note and suggestion if it could be relevant.
|
|
|
|
err.note("consider using a `let` binding to create a longer lived value");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2018-05-16 18:58:54 +03:00
|
|
|
explanation.add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2018-05-16 18:58:54 +03:00
|
|
|
&self.local_names,
|
|
|
|
&mut err,
|
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2018-05-16 18:58:54 +03:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let within = if borrow_spans.for_generator() { " by generator" } else { "" };
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
borrow_spans.args_span_label(&mut err, format!("value captured here{}", within));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_report_cannot_return_reference_to_local(
|
|
|
|
&self,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
borrow_span: Span,
|
|
|
|
return_span: Span,
|
|
|
|
category: ConstraintCategory,
|
|
|
|
opt_place_desc: Option<&String>,
|
|
|
|
) -> Option<DiagnosticBuilder<'cx>> {
|
|
|
|
let return_kind = match category {
|
2020-05-23 21:40:55 -04:00
|
|
|
ConstraintCategory::Return(_) => "return",
|
2019-05-04 08:40:07 +01:00
|
|
|
ConstraintCategory::Yield => "yield",
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME use a better heuristic than Spans
|
2019-12-22 17:42:04 -05:00
|
|
|
let reference_desc = if return_span == self.body.source_info(borrow.reserve_location).span {
|
|
|
|
"reference to"
|
|
|
|
} else {
|
|
|
|
"value referencing"
|
|
|
|
};
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
|
2019-10-20 16:09:36 -04:00
|
|
|
let local_kind = if let Some(local) = borrow.borrowed_place.as_local() {
|
2019-11-06 12:00:46 -05:00
|
|
|
match self.body.local_kind(local) {
|
2019-12-22 17:42:04 -05:00
|
|
|
LocalKind::ReturnPointer | LocalKind::Temp => {
|
|
|
|
bug!("temporary or return pointer with a name")
|
|
|
|
}
|
2019-10-20 16:09:36 -04:00
|
|
|
LocalKind::Var => "local variable ",
|
2021-05-21 21:01:27 +02:00
|
|
|
LocalKind::Arg
|
|
|
|
if !self.upvars.is_empty() && local == ty::CAPTURE_STRUCT_LOCAL =>
|
|
|
|
{
|
2019-10-20 16:09:36 -04:00
|
|
|
"variable captured by `move` "
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
LocalKind::Arg => "function parameter ",
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
2019-10-20 16:09:36 -04:00
|
|
|
} else {
|
|
|
|
"local data "
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
|
|
|
(
|
|
|
|
format!("{}`{}`", local_kind, place_desc),
|
|
|
|
format!("`{}` is borrowed here", place_desc),
|
|
|
|
)
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
let root_place =
|
|
|
|
self.prefixes(borrow.borrowed_place.as_ref(), PrefixSet::All).last().unwrap();
|
2019-12-11 16:50:03 -03:00
|
|
|
let local = root_place.local;
|
2020-01-14 02:10:05 -03:00
|
|
|
match self.body.local_kind(local) {
|
2019-12-22 17:42:04 -05:00
|
|
|
LocalKind::ReturnPointer | LocalKind::Temp => {
|
|
|
|
("temporary value".to_string(), "temporary value created here".to_string())
|
|
|
|
}
|
2019-07-27 13:23:34 -07:00
|
|
|
LocalKind::Arg => (
|
|
|
|
"function parameter".to_string(),
|
|
|
|
"function parameter borrowed here".to_string(),
|
|
|
|
),
|
2019-12-22 17:42:04 -05:00
|
|
|
LocalKind::Var => {
|
|
|
|
("local binding".to_string(), "local binding introduced here".to_string())
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_return_reference_to_local(
|
2019-05-04 08:40:07 +01:00
|
|
|
return_span,
|
|
|
|
return_kind,
|
|
|
|
reference_desc,
|
|
|
|
&place_desc,
|
|
|
|
);
|
|
|
|
|
|
|
|
if return_span != borrow_span {
|
|
|
|
err.span_label(borrow_span, note);
|
2021-02-09 17:18:28 +00:00
|
|
|
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let ty_params = ty::List::empty();
|
|
|
|
|
|
|
|
let return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
|
|
|
|
let return_ty = tcx.erase_regions(return_ty);
|
|
|
|
|
|
|
|
// to avoid panics
|
2021-07-04 11:43:22 -04:00
|
|
|
if let Some(iter_trait) = tcx.get_diagnostic_item(sym::Iterator) {
|
2021-07-06 05:38:15 -04:00
|
|
|
if self
|
|
|
|
.infcx
|
|
|
|
.type_implements_trait(iter_trait, return_ty, ty_params, self.param_env)
|
2021-07-04 11:43:22 -04:00
|
|
|
.must_apply_modulo_regions()
|
|
|
|
{
|
|
|
|
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) {
|
|
|
|
err.span_suggestion_hidden(
|
|
|
|
return_span,
|
|
|
|
"use `.collect()` to allocate the iterator",
|
|
|
|
format!("{}{}", snippet, ".collect::<Vec<_>>()"),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2021-02-09 17:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_escaping_closure_capture(
|
|
|
|
&mut self,
|
2020-07-25 07:04:13 -04:00
|
|
|
use_span: UseSpans<'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
var_span: Span,
|
|
|
|
fr_name: &RegionName,
|
|
|
|
category: ConstraintCategory,
|
|
|
|
constraint_span: Span,
|
|
|
|
captured_var: &str,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let tcx = self.infcx.tcx;
|
2019-10-10 22:20:57 +08:00
|
|
|
let args_span = use_span.args_or_use();
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2021-06-28 11:22:47 -07:00
|
|
|
let (sugg_span, suggestion) = match tcx.sess.source_map().span_to_snippet(args_span) {
|
|
|
|
Ok(string) => {
|
2019-08-19 13:25:06 +01:00
|
|
|
if string.starts_with("async ") {
|
2021-06-28 11:22:47 -07:00
|
|
|
let pos = args_span.lo() + BytePos(6);
|
|
|
|
(args_span.with_lo(pos).with_hi(pos), "move ".to_string())
|
2019-08-19 13:25:06 +01:00
|
|
|
} else if string.starts_with("async|") {
|
2021-06-28 11:22:47 -07:00
|
|
|
let pos = args_span.lo() + BytePos(5);
|
|
|
|
(args_span.with_lo(pos).with_hi(pos), " move".to_string())
|
2019-08-19 13:25:06 +01:00
|
|
|
} else {
|
2021-06-28 11:22:47 -07:00
|
|
|
(args_span.shrink_to_lo(), "move ".to_string())
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2021-06-28 11:22:47 -07:00
|
|
|
Err(_) => (args_span, "move |<args>| <body>".to_string()),
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
2019-10-10 22:20:57 +08:00
|
|
|
let kind = match use_span.generator_kind() {
|
|
|
|
Some(generator_kind) => match generator_kind {
|
|
|
|
GeneratorKind::Async(async_kind) => match async_kind {
|
|
|
|
AsyncGeneratorKind::Block => "async block",
|
|
|
|
AsyncGeneratorKind::Closure => "async closure",
|
2020-03-06 12:13:55 +01:00
|
|
|
_ => bug!("async block/closure expected, but async function found."),
|
2019-10-10 22:20:57 +08:00
|
|
|
},
|
|
|
|
GeneratorKind::Gen => "generator",
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2019-10-10 22:20:57 +08:00
|
|
|
None => "closure",
|
|
|
|
};
|
2020-04-07 23:57:26 +02:00
|
|
|
|
|
|
|
let mut err =
|
|
|
|
self.cannot_capture_in_long_lived_closure(args_span, kind, captured_var, var_span);
|
2021-06-28 11:22:47 -07:00
|
|
|
err.span_suggestion_verbose(
|
|
|
|
sugg_span,
|
2019-10-10 22:20:57 +08:00
|
|
|
&format!(
|
|
|
|
"to force the {} to take ownership of {} (and any \
|
|
|
|
other referenced variables), use the `move` keyword",
|
2019-12-22 17:42:04 -05:00
|
|
|
kind, captured_var
|
2019-10-10 22:20:57 +08:00
|
|
|
),
|
2019-05-04 08:40:07 +01:00
|
|
|
suggestion,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
|
2021-01-09 19:46:19 +11:00
|
|
|
match category {
|
2020-05-23 21:40:55 -04:00
|
|
|
ConstraintCategory::Return(_) | ConstraintCategory::OpaqueType => {
|
2021-01-09 19:46:19 +11:00
|
|
|
let msg = format!("{} is returned here", kind);
|
|
|
|
err.span_note(constraint_span, &msg);
|
2020-04-07 23:57:26 +02:00
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
ConstraintCategory::CallArgument => {
|
|
|
|
fr_name.highlight_region_name(&mut err);
|
2021-01-16 03:32:54 +11:00
|
|
|
if matches!(use_span.generator_kind(), Some(GeneratorKind::Async(_))) {
|
2021-01-09 20:02:47 +11:00
|
|
|
err.note(
|
2021-01-13 23:37:49 +11:00
|
|
|
"async blocks are not executed immediately and must either take a \
|
2021-01-09 20:02:47 +11:00
|
|
|
reference or ownership of outside variables they use",
|
|
|
|
);
|
2021-01-09 19:46:19 +11:00
|
|
|
} else {
|
|
|
|
let msg = format!("function requires argument type to outlive `{}`", fr_name);
|
|
|
|
err.span_note(constraint_span, &msg);
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!(
|
|
|
|
"report_escaping_closure_capture called with unexpected constraint \
|
2019-12-30 19:46:30 -06:00
|
|
|
category: `{:?}`",
|
2019-12-22 17:42:04 -05:00
|
|
|
category
|
|
|
|
),
|
2021-01-03 03:53:50 +11:00
|
|
|
}
|
2021-01-09 19:46:19 +11:00
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_escaping_data(
|
|
|
|
&mut self,
|
|
|
|
borrow_span: Span,
|
|
|
|
name: &Option<String>,
|
|
|
|
upvar_span: Span,
|
|
|
|
upvar_name: &str,
|
|
|
|
escape_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'cx> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
|
2020-10-04 17:23:02 -07:00
|
|
|
let (_, escapes_from) = tcx.article_and_description(self.mir_def_id().to_def_id());
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut err =
|
|
|
|
borrowck_errors::borrowed_data_escapes_closure(tcx, escape_span, escapes_from);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
err.span_label(
|
|
|
|
upvar_span,
|
2020-01-22 01:41:44 +00:00
|
|
|
format!("`{}` declared here, outside of the {} body", upvar_name, escapes_from),
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
err.span_label(borrow_span, format!("borrow is only valid in the {} body", escapes_from));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
err.span_label(
|
|
|
|
escape_span,
|
|
|
|
format!("reference to `{}` escapes the {} body here", name, escapes_from),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_label(
|
|
|
|
escape_span,
|
|
|
|
format!("reference escapes the {} body here", escapes_from),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:40:47 +02:00
|
|
|
fn get_moved_indexes(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
mpi: MovePathIndex,
|
|
|
|
) -> (Vec<MoveSite>, Vec<Location>) {
|
2020-04-12 10:31:00 -07:00
|
|
|
fn predecessor_locations(
|
|
|
|
body: &'a mir::Body<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> impl Iterator<Item = Location> + 'a {
|
|
|
|
if location.statement_index == 0 {
|
2020-04-21 10:14:21 -07:00
|
|
|
let predecessors = body.predecessors()[location.block].to_vec();
|
2020-04-12 10:31:00 -07:00
|
|
|
Either::Left(predecessors.into_iter().map(move |bb| body.terminator_loc(bb)))
|
|
|
|
} else {
|
|
|
|
Either::Right(std::iter::once(Location {
|
|
|
|
statement_index: location.statement_index - 1,
|
|
|
|
..location
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
let mut stack = Vec::new();
|
2020-04-12 10:31:00 -07:00
|
|
|
stack.extend(predecessor_locations(self.body, location).map(|predecessor| {
|
2019-05-04 08:40:07 +01:00
|
|
|
let is_back_edge = location.dominates(predecessor, &self.dominators);
|
|
|
|
(predecessor, is_back_edge)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let mut visited = FxHashSet::default();
|
2021-05-19 18:40:47 +02:00
|
|
|
let mut move_locations = FxHashSet::default();
|
|
|
|
let mut reinits = vec![];
|
2019-05-04 08:40:07 +01:00
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
'dfs: while let Some((location, is_back_edge)) = stack.pop() {
|
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized: (current_location={:?}, back_edge={})",
|
|
|
|
location, is_back_edge
|
|
|
|
);
|
|
|
|
|
|
|
|
if !visited.insert(location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for moves
|
2019-12-22 17:42:04 -05:00
|
|
|
let stmt_kind =
|
|
|
|
self.body[location.block].statements.get(location.statement_index).map(|s| &s.kind);
|
2019-05-04 08:40:07 +01:00
|
|
|
if let Some(StatementKind::StorageDead(..)) = stmt_kind {
|
|
|
|
// this analysis only tries to find moves explicitly
|
|
|
|
// written by the user, so we ignore the move-outs
|
|
|
|
// created by `StorageDead` and at the beginning
|
|
|
|
// of a function.
|
|
|
|
} else {
|
|
|
|
// If we are found a use of a.b.c which was in error, then we want to look for
|
|
|
|
// moves not only of a.b.c but also a.b and a.
|
|
|
|
//
|
|
|
|
// Note that the moves data already includes "parent" paths, so we don't have to
|
|
|
|
// worry about the other case: that is, if there is a move of a.b.c, it is already
|
|
|
|
// marked as a move of a.b and a as well, so we will generate the correct errors
|
|
|
|
// there.
|
|
|
|
let mut mpis = vec![mpi];
|
|
|
|
let move_paths = &self.move_data.move_paths;
|
2020-01-24 12:38:51 -08:00
|
|
|
mpis.extend(move_paths[mpi].parents(move_paths).map(|(mpi, _)| mpi));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
for moi in &self.move_data.loc_map[location] {
|
|
|
|
debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
|
2020-08-08 13:16:43 -04:00
|
|
|
let path = self.move_data.moves[*moi].path;
|
|
|
|
if mpis.contains(&path) {
|
|
|
|
debug!(
|
|
|
|
"report_use_of_moved_or_uninitialized: found {:?}",
|
|
|
|
move_paths[path].place
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
result.push(MoveSite { moi: *moi, traversed_back_edge: is_back_edge });
|
2021-05-19 18:40:47 +02:00
|
|
|
move_locations.insert(location);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
// Strictly speaking, we could continue our DFS here. There may be
|
|
|
|
// other moves that can reach the point of error. But it is kind of
|
|
|
|
// confusing to highlight them.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// let a = vec![];
|
|
|
|
// let b = a;
|
|
|
|
// let c = a;
|
|
|
|
// drop(a); // <-- current point of error
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Because we stop the DFS here, we only highlight `let c = a`,
|
|
|
|
// and not `let b = a`. We will of course also report an error at
|
|
|
|
// `let c = a` which highlights `let b = a` as the move.
|
|
|
|
continue 'dfs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for inits
|
|
|
|
let mut any_match = false;
|
|
|
|
drop_flag_effects::for_location_inits(
|
|
|
|
self.infcx.tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
&self.body,
|
2019-05-04 08:40:07 +01:00
|
|
|
self.move_data,
|
|
|
|
location,
|
|
|
|
|m| {
|
|
|
|
if m == mpi {
|
|
|
|
any_match = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if any_match {
|
2021-05-19 18:40:47 +02:00
|
|
|
reinits.push(location);
|
2019-05-04 08:40:07 +01:00
|
|
|
continue 'dfs;
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:31:00 -07:00
|
|
|
stack.extend(predecessor_locations(self.body, location).map(|predecessor| {
|
2019-05-04 08:40:07 +01:00
|
|
|
let back_edge = location.dominates(predecessor, &self.dominators);
|
|
|
|
(predecessor, is_back_edge || back_edge)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:40:47 +02:00
|
|
|
// Check if we can reach these reinits from a move location.
|
|
|
|
let reinits_reachable = reinits
|
|
|
|
.into_iter()
|
|
|
|
.filter(|reinit| {
|
|
|
|
let mut visited = FxHashSet::default();
|
|
|
|
let mut stack = vec![*reinit];
|
|
|
|
while let Some(location) = stack.pop() {
|
|
|
|
if !visited.insert(location) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if move_locations.contains(&location) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
stack.extend(predecessor_locations(self.body, location));
|
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
|
|
|
.collect::<Vec<Location>>();
|
|
|
|
(result, reinits_reachable)
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_illegal_mutation_of_borrowed(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
location: Location,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
loan: &BorrowData<'tcx>,
|
|
|
|
) {
|
|
|
|
let loan_spans = self.retrieve_borrow_spans(loan);
|
|
|
|
let loan_span = loan_spans.args_or_use();
|
|
|
|
|
2020-03-26 01:55:16 +01:00
|
|
|
let descr_place = self.describe_any_place(place.as_ref());
|
2019-05-04 08:40:07 +01:00
|
|
|
if loan.kind == BorrowKind::Shallow {
|
2020-03-31 12:19:29 -03:00
|
|
|
if let Some(section) = self.classify_immutable_section(loan.assigned_place) {
|
2019-10-19 21:00:21 +01:00
|
|
|
let mut err = self.cannot_mutate_in_immutable_section(
|
|
|
|
span,
|
|
|
|
loan_span,
|
2020-03-25 11:17:06 +01:00
|
|
|
&descr_place,
|
2019-10-19 21:00:21 +01:00
|
|
|
section,
|
|
|
|
"assign",
|
|
|
|
);
|
|
|
|
loan_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!("borrow occurs due to use{}", loan_spans.describe()),
|
2021-03-17 02:51:27 -04:00
|
|
|
loan.kind.describe_mutability(),
|
2019-10-19 21:00:21 +01:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
err.buffer(&mut self.errors_buffer);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
let mut err = self.cannot_assign_to_borrowed(span, loan_span, &descr_place);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2021-03-17 02:51:27 -04:00
|
|
|
loan_spans.var_span_label(
|
|
|
|
&mut err,
|
|
|
|
format!("borrow occurs due to use{}", loan_spans.describe()),
|
|
|
|
loan.kind.describe_mutability(),
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
|
|
|
|
self.explain_why_borrow_contains_point(location, loan, None).add_explanation_to_diagnostic(
|
|
|
|
self.infcx.tcx,
|
|
|
|
&self.body,
|
|
|
|
&self.local_names,
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut err,
|
2019-12-22 17:42:04 -05:00
|
|
|
"",
|
|
|
|
None,
|
2021-06-16 09:44:47 +09:00
|
|
|
None,
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2021-02-22 19:08:44 +11:00
|
|
|
self.explain_deref_coercion(loan, &mut err);
|
|
|
|
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn explain_deref_coercion(&mut self, loan: &BorrowData<'tcx>, err: &mut DiagnosticBuilder<'_>) {
|
2021-02-02 03:13:19 +11:00
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
if let (
|
|
|
|
Some(Terminator { kind: TerminatorKind::Call { from_hir_call: false, .. }, .. }),
|
|
|
|
Some((method_did, method_substs)),
|
|
|
|
) = (
|
|
|
|
&self.body[loan.reserve_location.block].terminator,
|
2021-01-05 20:08:11 +01:00
|
|
|
rustc_const_eval::util::find_self_call(
|
2021-02-02 03:13:19 +11:00
|
|
|
tcx,
|
|
|
|
self.body,
|
|
|
|
loan.assigned_place.local,
|
|
|
|
loan.reserve_location.block,
|
|
|
|
),
|
|
|
|
) {
|
|
|
|
if tcx.is_diagnostic_item(sym::deref_method, method_did) {
|
|
|
|
let deref_target =
|
|
|
|
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
|
|
|
|
Instance::resolve(tcx, self.param_env, deref_target, method_substs)
|
|
|
|
.transpose()
|
|
|
|
});
|
|
|
|
if let Some(Ok(instance)) = deref_target {
|
|
|
|
let deref_target_ty = instance.ty(tcx, self.param_env);
|
|
|
|
err.note(&format!(
|
|
|
|
"borrow occurs due to deref coercion to `{}`",
|
|
|
|
deref_target_ty
|
|
|
|
));
|
2021-02-19 13:30:49 +11:00
|
|
|
err.span_note(tcx.def_span(instance.def_id()), "deref defined here");
|
2021-02-02 03:13:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reports an illegal reassignment; for example, an assignment to
|
|
|
|
/// (part of) a non-`mut` local that occurs potentially after that
|
|
|
|
/// local has already been initialized. `place` is the path being
|
|
|
|
/// assigned; `err_place` is a place providing a reason why
|
|
|
|
/// `place` is not mutable (e.g., the non-`mut` local `x` in an
|
|
|
|
/// assignment to `x.f`).
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn report_illegal_reassignment(
|
2019-05-04 08:40:07 +01:00
|
|
|
&mut self,
|
|
|
|
_location: Location,
|
2020-03-30 17:49:33 -03:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2019-05-04 08:40:07 +01:00
|
|
|
assigned_span: Span,
|
2020-03-30 17:49:33 -03:00
|
|
|
err_place: Place<'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
) {
|
2018-05-16 18:58:54 +03:00
|
|
|
let (from_arg, local_decl, local_name) = match err_place.as_local() {
|
|
|
|
Some(local) => (
|
2019-11-06 12:00:46 -05:00
|
|
|
self.body.local_kind(local) == LocalKind::Arg,
|
|
|
|
Some(&self.body.local_decls[local]),
|
2018-05-16 18:58:54 +03:00
|
|
|
self.local_names[local],
|
|
|
|
),
|
|
|
|
None => (false, None, None),
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// If root local is initialized immediately (everything apart from let
|
|
|
|
// PATTERN;) then make the error refer to that local, rather than the
|
|
|
|
// place being assigned later.
|
|
|
|
let (place_description, assigned_span) = match local_decl {
|
2020-04-16 17:38:52 -07:00
|
|
|
Some(LocalDecl {
|
2019-12-22 17:42:04 -05:00
|
|
|
local_info:
|
2020-05-06 12:29:00 +10:00
|
|
|
Some(box LocalInfo::User(
|
2020-04-16 17:38:52 -07:00
|
|
|
ClearCrossCrate::Clear
|
|
|
|
| ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
|
|
|
opt_match_place: None,
|
|
|
|
..
|
|
|
|
})),
|
2020-05-06 12:29:00 +10:00
|
|
|
))
|
|
|
|
| Some(box LocalInfo::StaticRef { .. })
|
|
|
|
| None,
|
2019-05-04 08:40:07 +01:00
|
|
|
..
|
|
|
|
})
|
2020-03-26 01:55:16 +01:00
|
|
|
| None => (self.describe_any_place(place.as_ref()), assigned_span),
|
|
|
|
Some(decl) => (self.describe_any_place(err_place.as_ref()), decl.source_info.span),
|
2019-05-04 08:40:07 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
let mut err = self.cannot_reassign_immutable(span, &place_description, from_arg);
|
2019-05-04 08:40:07 +01:00
|
|
|
let msg = if from_arg {
|
|
|
|
"cannot assign to immutable argument"
|
|
|
|
} else {
|
|
|
|
"cannot assign twice to immutable variable"
|
|
|
|
};
|
|
|
|
if span != assigned_span {
|
|
|
|
if !from_arg {
|
2020-03-25 11:17:06 +01:00
|
|
|
err.span_label(assigned_span, format!("first assignment to {}", place_description));
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(decl) = local_decl {
|
2018-05-16 18:58:54 +03:00
|
|
|
if let Some(name) = local_name {
|
2019-05-04 08:40:07 +01:00
|
|
|
if decl.can_be_made_mutable() {
|
|
|
|
err.span_suggestion(
|
|
|
|
decl.source_info.span,
|
2021-04-12 23:29:09 +01:00
|
|
|
"consider making this binding mutable",
|
2019-05-04 08:40:07 +01:00
|
|
|
format!("mut {}", name),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.span_label(span, msg);
|
|
|
|
err.buffer(&mut self.errors_buffer);
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:25:03 -03:00
|
|
|
fn classify_drop_access_kind(&self, place: PlaceRef<'tcx>) -> StorageDeadOrDrop<'tcx> {
|
2019-05-04 08:40:07 +01:00
|
|
|
let tcx = self.infcx.tcx;
|
2021-01-09 23:33:38 -06:00
|
|
|
match place.last_projection() {
|
|
|
|
None => StorageDeadOrDrop::LocalStorageDead,
|
|
|
|
Some((place_base, elem)) => {
|
2019-07-30 00:07:28 +02:00
|
|
|
// FIXME(spastorino) make this iterate
|
2021-01-09 23:33:38 -06:00
|
|
|
let base_access = self.classify_drop_access_kind(place_base);
|
2019-05-04 08:40:07 +01:00
|
|
|
match elem {
|
|
|
|
ProjectionElem::Deref => match base_access {
|
|
|
|
StorageDeadOrDrop::LocalStorageDead
|
|
|
|
| StorageDeadOrDrop::BoxedStorageDead => {
|
|
|
|
assert!(
|
2021-01-09 23:33:38 -06:00
|
|
|
place_base.ty(self.body, tcx).ty.is_box(),
|
2019-05-04 08:40:07 +01:00
|
|
|
"Drop of value behind a reference or raw pointer"
|
|
|
|
);
|
|
|
|
StorageDeadOrDrop::BoxedStorageDead
|
|
|
|
}
|
|
|
|
StorageDeadOrDrop::Destructor(_) => base_access,
|
|
|
|
},
|
|
|
|
ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
|
2021-01-09 23:33:38 -06:00
|
|
|
let base_ty = place_base.ty(self.body, tcx).ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
match base_ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
ty::Adt(def, _) if def.has_dtor(tcx) => {
|
|
|
|
// Report the outermost adt with a destructor
|
|
|
|
match base_access {
|
|
|
|
StorageDeadOrDrop::Destructor(_) => base_access,
|
|
|
|
StorageDeadOrDrop::LocalStorageDead
|
|
|
|
| StorageDeadOrDrop::BoxedStorageDead => {
|
|
|
|
StorageDeadOrDrop::Destructor(base_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => base_access,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. }
|
|
|
|
| ProjectionElem::Index(_) => base_access,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:00:21 +01:00
|
|
|
/// Describe the reason for the fake borrow that was assigned to `place`.
|
2020-03-31 12:19:29 -03:00
|
|
|
fn classify_immutable_section(&self, place: Place<'tcx>) -> Option<&'static str> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
2020-03-31 12:19:29 -03:00
|
|
|
struct FakeReadCauseFinder<'tcx> {
|
|
|
|
place: Place<'tcx>,
|
2019-10-19 21:00:21 +01:00
|
|
|
cause: Option<FakeReadCause>,
|
|
|
|
}
|
2020-03-31 12:19:29 -03:00
|
|
|
impl<'tcx> Visitor<'tcx> for FakeReadCauseFinder<'tcx> {
|
2019-10-19 21:00:21 +01:00
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) {
|
|
|
|
match statement {
|
2021-03-29 22:48:44 -04:00
|
|
|
Statement { kind: StatementKind::FakeRead(box (cause, place)), .. }
|
2020-03-31 12:19:29 -03:00
|
|
|
if *place == self.place =>
|
2019-12-22 17:42:04 -05:00
|
|
|
{
|
2019-10-19 21:00:21 +01:00
|
|
|
self.cause = Some(*cause);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut visitor = FakeReadCauseFinder { place, cause: None };
|
2020-03-28 14:54:41 -07:00
|
|
|
visitor.visit_body(&self.body);
|
2019-10-19 21:00:21 +01:00
|
|
|
match visitor.cause {
|
|
|
|
Some(FakeReadCause::ForMatchGuard) => Some("match guard"),
|
|
|
|
Some(FakeReadCause::ForIndex) => Some("indexing expression"),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 08:40:07 +01:00
|
|
|
/// Annotate argument and return type of function and closure with (synthesized) lifetime for
|
|
|
|
/// borrow of local value that does not live long enough.
|
|
|
|
fn annotate_argument_and_return_for_borrow(
|
|
|
|
&self,
|
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
|
|
|
|
// Define a fallback for when we can't match a closure.
|
|
|
|
let fallback = || {
|
2020-10-04 17:23:02 -07:00
|
|
|
let is_closure = self.infcx.tcx.is_closure(self.mir_def_id().to_def_id());
|
2019-05-04 08:40:07 +01:00
|
|
|
if is_closure {
|
|
|
|
None
|
|
|
|
} else {
|
2020-10-04 17:23:02 -07:00
|
|
|
let ty = self.infcx.tcx.type_of(self.mir_def_id());
|
2020-08-03 00:49:11 +02:00
|
|
|
match ty.kind() {
|
2020-05-11 10:29:05 +02:00
|
|
|
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
|
2020-10-04 17:23:02 -07:00
|
|
|
self.mir_def_id().to_def_id(),
|
|
|
|
self.infcx.tcx.fn_sig(self.mir_def_id()),
|
2020-05-11 10:29:05 +02:00
|
|
|
),
|
2019-05-04 08:40:07 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// In order to determine whether we need to annotate, we need to check whether the reserve
|
|
|
|
// place was an assignment into a temporary.
|
|
|
|
//
|
|
|
|
// If it was, we check whether or not that temporary is eventually assigned into the return
|
|
|
|
// place. If it was, we can add annotations about the function's return type and arguments
|
|
|
|
// and it'll make sense.
|
|
|
|
let location = borrow.reserve_location;
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("annotate_argument_and_return_for_borrow: location={:?}", location);
|
|
|
|
if let Some(&Statement { kind: StatementKind::Assign(box (ref reservation, _)), .. }) =
|
|
|
|
&self.body[location.block].statements.get(location.statement_index)
|
2019-05-04 08:40:07 +01:00
|
|
|
{
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("annotate_argument_and_return_for_borrow: reservation={:?}", reservation);
|
2019-05-04 08:40:07 +01:00
|
|
|
// Check that the initial assignment of the reserve location is into a temporary.
|
2019-10-20 16:09:36 -04:00
|
|
|
let mut target = match reservation.as_local() {
|
2019-11-06 12:00:46 -05:00
|
|
|
Some(local) if self.body.local_kind(local) == LocalKind::Temp => local,
|
2019-05-04 08:40:07 +01:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Next, look through the rest of the block, checking if we are assigning the
|
|
|
|
// `target` (that is, the place that contains our borrow) to anything.
|
|
|
|
let mut annotated_closure = None;
|
2019-12-22 17:42:04 -05:00
|
|
|
for stmt in &self.body[location.block].statements[location.statement_index + 1..] {
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
|
|
|
|
target, stmt
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(assigned_to) = place.as_local() {
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: assigned_to={:?} \
|
|
|
|
rvalue={:?}",
|
|
|
|
assigned_to, rvalue
|
|
|
|
);
|
|
|
|
// Check if our `target` was captured by a closure.
|
|
|
|
if let Rvalue::Aggregate(
|
|
|
|
box AggregateKind::Closure(def_id, substs),
|
|
|
|
operands,
|
|
|
|
) = rvalue
|
|
|
|
{
|
|
|
|
for operand in operands {
|
|
|
|
let assigned_from = match operand {
|
|
|
|
Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
|
|
|
|
assigned_from
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: assigned_from={:?}",
|
2019-05-04 08:40:07 +01:00
|
|
|
assigned_from
|
2019-10-20 16:09:36 -04:00
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// Find the local from the operand.
|
|
|
|
let assigned_from_local = match assigned_from.local_or_deref_local()
|
|
|
|
{
|
|
|
|
Some(local) => local,
|
|
|
|
None => continue,
|
|
|
|
};
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
if assigned_from_local != target {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// If a closure captured our `target` and then assigned
|
|
|
|
// into a place then we should annotate the closure in
|
|
|
|
// case it ends up being assigned into the return place.
|
2020-03-13 03:23:38 +02:00
|
|
|
annotated_closure =
|
|
|
|
self.annotate_fn_sig(*def_id, substs.as_closure().sig());
|
2019-10-20 16:09:36 -04:00
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: \
|
|
|
|
annotated_closure={:?} assigned_from_local={:?} \
|
|
|
|
assigned_to={:?}",
|
|
|
|
annotated_closure, assigned_from_local, assigned_to
|
|
|
|
);
|
|
|
|
|
|
|
|
if assigned_to == mir::RETURN_PLACE {
|
|
|
|
// If it was assigned directly into the return place, then
|
|
|
|
// return now.
|
|
|
|
return annotated_closure;
|
|
|
|
} else {
|
|
|
|
// Otherwise, update the target.
|
|
|
|
target = assigned_to;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// If none of our closure's operands matched, then skip to the next
|
|
|
|
// statement.
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// Otherwise, look at other types of assignment.
|
|
|
|
let assigned_from = match rvalue {
|
|
|
|
Rvalue::Ref(_, _, assigned_from) => assigned_from,
|
|
|
|
Rvalue::Use(operand) => match operand {
|
|
|
|
Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
|
|
|
|
assigned_from
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
},
|
2019-05-04 08:40:07 +01:00
|
|
|
_ => continue,
|
2019-10-20 16:09:36 -04:00
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: \
|
|
|
|
assigned_from={:?}",
|
|
|
|
assigned_from,
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// Find the local from the rvalue.
|
|
|
|
let assigned_from_local = match assigned_from.local_or_deref_local() {
|
|
|
|
Some(local) => local,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: \
|
|
|
|
assigned_from_local={:?}",
|
|
|
|
assigned_from_local,
|
|
|
|
);
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// Check if our local matches the target - if so, we've assigned our
|
|
|
|
// borrow to a new place.
|
|
|
|
if assigned_from_local != target {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// If we assigned our `target` into a new place, then we should
|
|
|
|
// check if it was the return place.
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: \
|
|
|
|
assigned_from_local={:?} assigned_to={:?}",
|
|
|
|
assigned_from_local, assigned_to
|
|
|
|
);
|
|
|
|
if assigned_to == mir::RETURN_PLACE {
|
|
|
|
// If it was then return the annotated closure if there was one,
|
|
|
|
// else, annotate this function.
|
|
|
|
return annotated_closure.or_else(fallback);
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
// If we didn't assign into the return place, then we just update
|
|
|
|
// the target.
|
|
|
|
target = assigned_to;
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the terminator if we didn't find anything in the statements.
|
2019-11-06 12:00:46 -05:00
|
|
|
let terminator = &self.body[location.block].terminator();
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
|
|
|
|
target, terminator
|
|
|
|
);
|
2019-12-22 17:42:04 -05:00
|
|
|
if let TerminatorKind::Call { destination: Some((place, _)), args, .. } =
|
|
|
|
&terminator.kind
|
2019-05-04 08:40:07 +01:00
|
|
|
{
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(assigned_to) = place.as_local() {
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
2019-10-20 16:09:36 -04:00
|
|
|
"annotate_argument_and_return_for_borrow: assigned_to={:?} args={:?}",
|
|
|
|
assigned_to, args
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
2019-10-20 16:09:36 -04:00
|
|
|
for operand in args {
|
|
|
|
let assigned_from = match operand {
|
|
|
|
Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
|
|
|
|
assigned_from
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
2019-05-04 08:40:07 +01:00
|
|
|
debug!(
|
2019-10-20 16:09:36 -04:00
|
|
|
"annotate_argument_and_return_for_borrow: assigned_from={:?}",
|
|
|
|
assigned_from,
|
2019-05-04 08:40:07 +01:00
|
|
|
);
|
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(assigned_from_local) = assigned_from.local_or_deref_local() {
|
|
|
|
debug!(
|
|
|
|
"annotate_argument_and_return_for_borrow: assigned_from_local={:?}",
|
|
|
|
assigned_from_local,
|
|
|
|
);
|
|
|
|
|
|
|
|
if assigned_to == mir::RETURN_PLACE && assigned_from_local == target {
|
|
|
|
return annotated_closure.or_else(fallback);
|
|
|
|
}
|
2019-05-04 08:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't found an assignment into the return place, then we need not add
|
|
|
|
// any annotations.
|
|
|
|
debug!("annotate_argument_and_return_for_borrow: none found");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Annotate the first argument and return type of a function signature if they are
|
|
|
|
/// references.
|
|
|
|
fn annotate_fn_sig(
|
|
|
|
&self,
|
|
|
|
did: DefId,
|
|
|
|
sig: ty::PolyFnSig<'tcx>,
|
|
|
|
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
|
|
|
|
debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
|
|
|
|
let is_closure = self.infcx.tcx.is_closure(did);
|
2020-08-12 12:22:56 +02:00
|
|
|
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did.as_local()?);
|
2019-05-04 08:40:07 +01:00
|
|
|
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
|
|
|
|
|
|
|
|
// We need to work out which arguments to highlight. We do this by looking
|
|
|
|
// at the return type, where there are three cases:
|
|
|
|
//
|
|
|
|
// 1. If there are named arguments, then we should highlight the return type and
|
|
|
|
// highlight any of the arguments that are also references with that lifetime.
|
|
|
|
// If there are no arguments that have the same lifetime as the return type,
|
|
|
|
// then don't highlight anything.
|
|
|
|
// 2. The return type is a reference with an anonymous lifetime. If this is
|
|
|
|
// the case, then we can take advantage of (and teach) the lifetime elision
|
|
|
|
// rules.
|
|
|
|
//
|
|
|
|
// We know that an error is being reported. So the arguments and return type
|
|
|
|
// must satisfy the elision rules. Therefore, if there is a single argument
|
|
|
|
// then that means the return type and first (and only) argument have the same
|
|
|
|
// lifetime and the borrow isn't meeting that, we can highlight the argument
|
|
|
|
// and return type.
|
|
|
|
//
|
|
|
|
// If there are multiple arguments then the first argument must be self (else
|
|
|
|
// it would not satisfy the elision rules), so we can highlight self and the
|
|
|
|
// return type.
|
|
|
|
// 3. The return type is not a reference. In this case, we don't highlight
|
|
|
|
// anything.
|
|
|
|
let return_ty = sig.output();
|
2020-08-03 00:49:11 +02:00
|
|
|
match return_ty.skip_binder().kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
|
|
|
|
// This is case 1 from above, return type is a named reference so we need to
|
|
|
|
// search for relevant arguments.
|
|
|
|
let mut arguments = Vec::new();
|
|
|
|
for (index, argument) in sig.inputs().skip_binder().iter().enumerate() {
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Ref(argument_region, _, _) = argument.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
if argument_region == return_region {
|
2020-03-29 16:41:09 +02:00
|
|
|
// Need to use the `rustc_middle::ty` types to compare against the
|
2020-01-05 02:37:57 +01:00
|
|
|
// `return_region`. Then use the `rustc_hir` type to get only
|
2019-05-04 08:40:07 +01:00
|
|
|
// the lifetime span.
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].kind {
|
2019-05-04 08:40:07 +01:00
|
|
|
// With access to the lifetime, we can get
|
|
|
|
// the span of it.
|
|
|
|
arguments.push((*argument, lifetime.span));
|
|
|
|
} else {
|
|
|
|
bug!("ty type is a ref but hir type is not");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to have arguments. This shouldn't happen, but it's worth checking.
|
|
|
|
if arguments.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a mix of the HIR and the Ty types to get information
|
|
|
|
// as the HIR doesn't have full types for closure arguments.
|
2020-06-24 23:40:33 +02:00
|
|
|
let return_ty = sig.output().skip_binder();
|
2019-05-04 08:40:07 +01:00
|
|
|
let mut return_span = fn_decl.output.span();
|
2020-02-15 12:10:59 +09:00
|
|
|
if let hir::FnRetTy::Return(ty) = &fn_decl.output {
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::Rptr(lifetime, _) = ty.kind {
|
2019-05-04 08:40:07 +01:00
|
|
|
return_span = lifetime.span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(AnnotatedBorrowFnSignature::NamedFunction {
|
|
|
|
arguments,
|
|
|
|
return_ty,
|
|
|
|
return_span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ty::Ref(_, _, _) if is_closure => {
|
|
|
|
// This is case 2 from above but only for closures, return type is anonymous
|
|
|
|
// reference so we select
|
|
|
|
// the first argument.
|
|
|
|
let argument_span = fn_decl.inputs.first()?.span;
|
|
|
|
let argument_ty = sig.inputs().skip_binder().first()?;
|
|
|
|
|
|
|
|
// Closure arguments are wrapped in a tuple, so we need to get the first
|
|
|
|
// from that.
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Tuple(elems) = argument_ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
let argument_ty = elems.first()?.expect_ty();
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Ref(_, _, _) = argument_ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
return Some(AnnotatedBorrowFnSignature::Closure {
|
|
|
|
argument_ty,
|
|
|
|
argument_span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
ty::Ref(_, _, _) => {
|
|
|
|
// This is also case 2 from above but for functions, return type is still an
|
|
|
|
// anonymous reference so we select the first argument.
|
|
|
|
let argument_span = fn_decl.inputs.first()?.span;
|
|
|
|
let argument_ty = sig.inputs().skip_binder().first()?;
|
|
|
|
|
|
|
|
let return_span = fn_decl.output.span();
|
2020-06-24 23:40:33 +02:00
|
|
|
let return_ty = sig.output().skip_binder();
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
// We expect the first argument to be a reference.
|
2020-08-03 00:49:11 +02:00
|
|
|
match argument_ty.kind() {
|
2019-05-04 08:40:07 +01:00
|
|
|
ty::Ref(_, _, _) => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(AnnotatedBorrowFnSignature::AnonymousFunction {
|
|
|
|
argument_ty,
|
|
|
|
argument_span,
|
|
|
|
return_ty,
|
|
|
|
return_span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// This is case 3 from above, return type is not a reference so don't highlight
|
|
|
|
// anything.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum AnnotatedBorrowFnSignature<'tcx> {
|
|
|
|
NamedFunction {
|
|
|
|
arguments: Vec<(Ty<'tcx>, Span)>,
|
|
|
|
return_ty: Ty<'tcx>,
|
|
|
|
return_span: Span,
|
|
|
|
},
|
|
|
|
AnonymousFunction {
|
|
|
|
argument_ty: Ty<'tcx>,
|
|
|
|
argument_span: Span,
|
|
|
|
return_ty: Ty<'tcx>,
|
|
|
|
return_span: Span,
|
|
|
|
},
|
|
|
|
Closure {
|
|
|
|
argument_ty: Ty<'tcx>,
|
|
|
|
argument_span: Span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
|
|
|
|
/// Annotate the provided diagnostic with information about borrow from the fn signature that
|
|
|
|
/// helps explain.
|
2020-12-30 18:48:40 +01:00
|
|
|
pub(crate) fn emit(
|
2019-05-04 08:40:07 +01:00
|
|
|
&self,
|
2019-06-14 00:48:52 +03:00
|
|
|
cx: &mut MirBorrowckCtxt<'_, 'tcx>,
|
2019-05-04 08:40:07 +01:00
|
|
|
diag: &mut DiagnosticBuilder<'_>,
|
|
|
|
) -> String {
|
|
|
|
match self {
|
2019-12-22 17:42:04 -05:00
|
|
|
AnnotatedBorrowFnSignature::Closure { argument_ty, argument_span } => {
|
2019-05-04 08:40:07 +01:00
|
|
|
diag.span_label(
|
|
|
|
*argument_span,
|
|
|
|
format!("has type `{}`", cx.get_name_for_ty(argument_ty, 0)),
|
|
|
|
);
|
|
|
|
|
|
|
|
cx.get_region_name_for_ty(argument_ty, 0)
|
|
|
|
}
|
|
|
|
AnnotatedBorrowFnSignature::AnonymousFunction {
|
|
|
|
argument_ty,
|
|
|
|
argument_span,
|
|
|
|
return_ty,
|
|
|
|
return_span,
|
|
|
|
} => {
|
|
|
|
let argument_ty_name = cx.get_name_for_ty(argument_ty, 0);
|
|
|
|
diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
|
|
|
|
|
|
|
|
let return_ty_name = cx.get_name_for_ty(return_ty, 0);
|
|
|
|
let types_equal = return_ty_name == argument_ty_name;
|
|
|
|
diag.span_label(
|
|
|
|
*return_span,
|
|
|
|
format!(
|
|
|
|
"{}has type `{}`",
|
|
|
|
if types_equal { "also " } else { "" },
|
|
|
|
return_ty_name,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
diag.note(
|
|
|
|
"argument and return type have the same lifetime due to lifetime elision rules",
|
|
|
|
);
|
|
|
|
diag.note(
|
|
|
|
"to learn more, visit <https://doc.rust-lang.org/book/ch10-03-\
|
|
|
|
lifetime-syntax.html#lifetime-elision>",
|
|
|
|
);
|
|
|
|
|
|
|
|
cx.get_region_name_for_ty(return_ty, 0)
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
AnnotatedBorrowFnSignature::NamedFunction { arguments, return_ty, return_span } => {
|
2019-05-04 08:40:07 +01:00
|
|
|
// Region of return type and arguments checked to be the same earlier.
|
|
|
|
let region_name = cx.get_region_name_for_ty(return_ty, 0);
|
|
|
|
for (_, argument_span) in arguments {
|
|
|
|
diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
diag.span_label(*return_span, format!("also has lifetime `{}`", region_name,));
|
2019-05-04 08:40:07 +01:00
|
|
|
|
|
|
|
diag.help(&format!(
|
|
|
|
"use data from the highlighted arguments which match the `{}` lifetime of \
|
|
|
|
the return type",
|
|
|
|
region_name,
|
|
|
|
));
|
|
|
|
|
|
|
|
region_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|