2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
2023-09-30 20:23:34 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex};
|
2022-12-08 09:02:54 -08:00
|
|
|
use rustc_span::{BytePos, Span};
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
use crate::diagnostics::CapturedMessageOpt;
|
2022-07-23 00:35:35 +03:00
|
|
|
use crate::diagnostics::{DescribePlaceOpt, UseSpans};
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::prefixes::PrefixSet;
|
|
|
|
use crate::MirBorrowckCtxt;
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2023-09-30 20:23:34 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum IllegalMoveOriginKind<'tcx> {
|
|
|
|
/// Illegal move due to attempt to move from behind a reference.
|
|
|
|
BorrowedContent {
|
|
|
|
/// The place the reference refers to: if erroneous code was trying to
|
|
|
|
/// move from `(*x).f` this will be `*x`.
|
|
|
|
target_place: Place<'tcx>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Illegal move due to attempt to move from field of an ADT that
|
|
|
|
/// implements `Drop`. Rust maintains invariant that all `Drop`
|
|
|
|
/// ADT's remain fully-initialized so that user-defined destructor
|
|
|
|
/// can safely read from all of the ADT's fields.
|
|
|
|
InteriorOfTypeWithDestructor { container_ty: Ty<'tcx> },
|
|
|
|
|
|
|
|
/// Illegal move due to attempt to move out of a slice or array.
|
|
|
|
InteriorOfSliceOrArray { ty: Ty<'tcx>, is_index: bool },
|
|
|
|
}
|
|
|
|
|
2023-09-30 15:14:07 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct MoveError<'tcx> {
|
|
|
|
place: Place<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MoveError<'tcx> {
|
|
|
|
pub(crate) fn new(
|
|
|
|
place: Place<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
) -> Self {
|
|
|
|
MoveError { place, location, kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
// Often when desugaring a pattern match we may have many individual moves in
|
|
|
|
// MIR that are all part of one operation from the user's point-of-view. For
|
|
|
|
// example:
|
|
|
|
//
|
|
|
|
// let (x, y) = foo()
|
|
|
|
//
|
|
|
|
// would move x from the 0 field of some temporary, and y from the 1 field. We
|
|
|
|
// group such errors together for cleaner error reporting.
|
|
|
|
//
|
|
|
|
// Errors are kept separate if they are from places with different parent move
|
|
|
|
// paths. For example, this generates two errors:
|
|
|
|
//
|
|
|
|
// let (&x, &y) = (&String::new(), &String::new());
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum GroupedMoveError<'tcx> {
|
2018-08-07 01:02:39 -07:00
|
|
|
// Place expression can't be moved from,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., match x[0] { s => (), } where x: &[String]
|
2018-08-07 01:02:39 -07:00
|
|
|
MovesFromPlace {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
span: Span,
|
|
|
|
move_from: Place<'tcx>,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
binds_to: Vec<Local>,
|
|
|
|
},
|
2018-08-07 01:02:39 -07:00
|
|
|
// Part of a value expression can't be moved from,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., match &String::new() { &x => (), }
|
2018-08-07 01:02:39 -07:00
|
|
|
MovesFromValue {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
span: Span,
|
|
|
|
move_from: MovePathIndex,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
binds_to: Vec<Local>,
|
|
|
|
},
|
|
|
|
// Everything that isn't from pattern matching.
|
|
|
|
OtherIllegalMove {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2020-07-25 07:04:13 -04:00
|
|
|
use_spans: UseSpans<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
2023-09-30 15:14:07 +00:00
|
|
|
pub(crate) fn report_move_errors(&mut self) {
|
|
|
|
let grouped_errors = self.group_move_errors();
|
2018-06-27 22:06:54 +01:00
|
|
|
for error in grouped_errors {
|
|
|
|
self.report(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 15:14:07 +00:00
|
|
|
fn group_move_errors(&mut self) -> Vec<GroupedMoveError<'tcx>> {
|
2018-06-27 22:06:54 +01:00
|
|
|
let mut grouped_errors = Vec::new();
|
2023-09-30 15:14:07 +00:00
|
|
|
let errors = std::mem::take(&mut self.move_errors);
|
|
|
|
for error in errors {
|
|
|
|
self.append_to_grouped_errors(&mut grouped_errors, error);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
grouped_errors
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_to_grouped_errors(
|
2018-07-13 21:56:04 +01:00
|
|
|
&self,
|
2018-06-27 22:06:54 +01:00
|
|
|
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
|
|
|
|
error: MoveError<'tcx>,
|
|
|
|
) {
|
2023-09-30 15:14:07 +00:00
|
|
|
let MoveError { place: original_path, location, kind } = error;
|
|
|
|
|
|
|
|
// Note: that the only time we assign a place isn't a temporary
|
|
|
|
// to a user variable is when initializing it.
|
|
|
|
// If that ever stops being the case, then the ever initialized
|
|
|
|
// flow could be used.
|
|
|
|
if let Some(StatementKind::Assign(box (place, Rvalue::Use(Operand::Move(move_from))))) =
|
|
|
|
self.body.basic_blocks[location.block]
|
|
|
|
.statements
|
|
|
|
.get(location.statement_index)
|
|
|
|
.map(|stmt| &stmt.kind)
|
|
|
|
{
|
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
let local_decl = &self.body.local_decls[local];
|
|
|
|
// opt_match_place is the
|
|
|
|
// match_span is the span of the expression being matched on
|
|
|
|
// match *x.y { ... } match_place is Some(*x.y)
|
|
|
|
// ^^^^ match_span is the span of *x.y
|
|
|
|
//
|
|
|
|
// opt_match_place is None for let [mut] x = ... statements,
|
|
|
|
// whether or not the right-hand side is a place expression
|
|
|
|
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
|
|
|
|
opt_match_place: Some((opt_match_place, match_span)),
|
|
|
|
binding_mode: _,
|
|
|
|
opt_ty_info: _,
|
|
|
|
pat_span: _,
|
|
|
|
})) = *local_decl.local_info()
|
2018-06-27 22:06:54 +01:00
|
|
|
{
|
2023-09-30 15:14:07 +00:00
|
|
|
let stmt_source_info = self.body.source_info(location);
|
|
|
|
self.append_binding_error(
|
|
|
|
grouped_errors,
|
|
|
|
kind,
|
|
|
|
original_path,
|
|
|
|
*move_from,
|
|
|
|
local,
|
|
|
|
opt_match_place,
|
|
|
|
match_span,
|
|
|
|
stmt_source_info.span,
|
|
|
|
);
|
|
|
|
return;
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 15:14:07 +00:00
|
|
|
|
|
|
|
let move_spans = self.move_spans(original_path.as_ref(), location);
|
|
|
|
grouped_errors.push(GroupedMoveError::OtherIllegalMove {
|
|
|
|
use_spans: move_spans,
|
|
|
|
original_path,
|
|
|
|
kind,
|
|
|
|
});
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn append_binding_error(
|
2018-07-13 21:56:04 +01:00
|
|
|
&self,
|
2018-06-27 22:06:54 +01:00
|
|
|
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2020-03-31 12:19:29 -03:00
|
|
|
move_from: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
bind_to: Local,
|
2020-03-31 12:19:29 -03:00
|
|
|
match_place: Option<Place<'tcx>>,
|
2018-06-27 22:06:54 +01:00
|
|
|
match_span: Span,
|
2018-07-22 21:38:31 +01:00
|
|
|
statement_span: Span,
|
2018-06-27 22:06:54 +01:00
|
|
|
) {
|
2022-12-08 09:02:54 -08:00
|
|
|
debug!(?match_place, ?match_span, "append_binding_error");
|
2018-06-27 22:06:54 +01:00
|
|
|
|
|
|
|
let from_simple_let = match_place.is_none();
|
2020-03-31 12:19:29 -03:00
|
|
|
let match_place = match_place.unwrap_or(move_from);
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2019-07-21 22:38:30 +02:00
|
|
|
match self.move_data.rev_lookup.find(match_place.as_ref()) {
|
2018-06-27 22:06:54 +01:00
|
|
|
// Error with the match place
|
|
|
|
LookupResult::Parent(_) => {
|
|
|
|
for ge in &mut *grouped_errors {
|
2022-04-09 00:01:40 +09:00
|
|
|
if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge
|
|
|
|
&& match_span == *span
|
|
|
|
{
|
2022-12-08 09:02:54 -08:00
|
|
|
debug!("appending local({bind_to:?}) to list");
|
2022-04-09 00:01:40 +09:00
|
|
|
if !binds_to.is_empty() {
|
|
|
|
binds_to.push(bind_to);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2022-04-09 00:01:40 +09:00
|
|
|
return;
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("found a new move error location");
|
|
|
|
|
|
|
|
// Don't need to point to x in let x = ... .
|
2018-07-22 21:38:31 +01:00
|
|
|
let (binds_to, span) = if from_simple_let {
|
|
|
|
(vec![], statement_span)
|
2018-06-27 22:06:54 +01:00
|
|
|
} else {
|
2018-07-22 21:38:31 +01:00
|
|
|
(vec![bind_to], match_span)
|
2018-06-27 22:06:54 +01:00
|
|
|
};
|
2018-08-07 01:02:39 -07:00
|
|
|
grouped_errors.push(GroupedMoveError::MovesFromPlace {
|
2018-07-22 21:38:31 +01:00
|
|
|
span,
|
2020-03-31 12:19:29 -03:00
|
|
|
move_from,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind,
|
|
|
|
binds_to,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Error with the pattern
|
|
|
|
LookupResult::Exact(_) => {
|
2022-02-19 00:48:49 +01:00
|
|
|
let LookupResult::Parent(Some(mpi)) =
|
|
|
|
self.move_data.rev_lookup.find(move_from.as_ref())
|
|
|
|
else {
|
2018-06-27 22:06:54 +01:00
|
|
|
// move_from should be a projection from match_place.
|
2022-02-19 00:48:49 +01:00
|
|
|
unreachable!("Probably not unreachable...");
|
2018-06-27 22:06:54 +01:00
|
|
|
};
|
|
|
|
for ge in &mut *grouped_errors {
|
2018-08-07 01:02:39 -07:00
|
|
|
if let GroupedMoveError::MovesFromValue {
|
2018-06-27 22:06:54 +01:00
|
|
|
span,
|
|
|
|
move_from: other_mpi,
|
|
|
|
binds_to,
|
|
|
|
..
|
|
|
|
} = ge
|
|
|
|
{
|
|
|
|
if match_span == *span && mpi == *other_mpi {
|
2022-12-08 09:02:54 -08:00
|
|
|
debug!("appending local({bind_to:?}) to list");
|
2018-06-27 22:06:54 +01:00
|
|
|
binds_to.push(bind_to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("found a new move error location");
|
2018-08-07 01:02:39 -07:00
|
|
|
grouped_errors.push(GroupedMoveError::MovesFromValue {
|
2018-06-27 22:06:54 +01:00
|
|
|
span: match_span,
|
|
|
|
move_from: mpi,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind,
|
|
|
|
binds_to: vec![bind_to],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:10:08 -03:00
|
|
|
fn report(&mut self, error: GroupedMoveError<'tcx>) {
|
2018-06-27 22:06:54 +01:00
|
|
|
let (mut err, err_span) = {
|
2022-06-27 15:43:23 -07:00
|
|
|
let (span, use_spans, original_path, kind) = match error {
|
|
|
|
GroupedMoveError::MovesFromPlace { span, original_path, ref kind, .. }
|
|
|
|
| GroupedMoveError::MovesFromValue { span, original_path, ref kind, .. } => {
|
|
|
|
(span, None, original_path, kind)
|
2019-05-05 12:02:17 +01:00
|
|
|
}
|
2020-03-31 12:19:29 -03:00
|
|
|
GroupedMoveError::OtherIllegalMove { use_spans, original_path, ref kind } => {
|
2022-06-27 15:43:23 -07:00
|
|
|
(use_spans.args_or_use(), Some(use_spans), original_path, kind)
|
2018-08-07 17:06:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
debug!(
|
|
|
|
"report: original_path={:?} span={:?}, kind={:?} \
|
|
|
|
original_path.is_upvar_field_projection={:?}",
|
|
|
|
original_path,
|
|
|
|
span,
|
|
|
|
kind,
|
2019-07-21 22:38:30 +02:00
|
|
|
self.is_upvar_field_projection(original_path.as_ref())
|
|
|
|
);
|
2019-05-05 11:16:56 +01:00
|
|
|
(
|
|
|
|
match kind {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
&IllegalMoveOriginKind::BorrowedContent { target_place } => self
|
2019-05-05 11:16:56 +01:00
|
|
|
.report_cannot_move_from_borrowed_content(
|
|
|
|
original_path,
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
target_place,
|
2019-05-24 19:10:00 -07:00
|
|
|
span,
|
2019-12-11 23:04:29 +09:00
|
|
|
use_spans,
|
2019-05-05 11:16:56 +01:00
|
|
|
),
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
&IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_move_out_of_interior_of_drop(span, ty)
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
&IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => {
|
|
|
|
self.cannot_move_out_of_interior_noncopy(span, ty, Some(is_index))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-05 11:16:56 +01:00
|
|
|
},
|
|
|
|
span,
|
|
|
|
)
|
2018-06-27 22:06:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.add_move_hints(error, &mut err, err_span);
|
2022-02-06 12:15:39 -08:00
|
|
|
self.buffer_error(err);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
fn report_cannot_move_from_static(
|
|
|
|
&mut self,
|
2020-03-31 12:19:29 -03:00
|
|
|
place: Place<'tcx>,
|
2019-05-05 11:16:56 +01:00
|
|
|
span: Span,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2019-11-18 23:04:06 +00:00
|
|
|
let description = if place.projection.len() == 1 {
|
2020-03-26 01:55:16 +01:00
|
|
|
format!("static item {}", self.describe_any_place(place.as_ref()))
|
2019-05-05 11:16:56 +01:00
|
|
|
} else {
|
2020-01-24 12:50:13 -03:00
|
|
|
let base_static = PlaceRef { local: place.local, projection: &[ProjectionElem::Deref] };
|
2019-04-30 18:58:24 +02:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
format!(
|
2020-03-25 11:17:06 +01:00
|
|
|
"{} as {} is a static item",
|
2020-03-26 01:55:16 +01:00
|
|
|
self.describe_any_place(place.as_ref()),
|
|
|
|
self.describe_any_place(base_static),
|
2019-05-05 11:16:56 +01:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_move_out_of(span, &description)
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn report_cannot_move_from_borrowed_content(
|
|
|
|
&mut self,
|
2020-03-31 12:19:29 -03:00
|
|
|
move_place: Place<'tcx>,
|
|
|
|
deref_target_place: Place<'tcx>,
|
2019-05-05 11:16:56 +01:00
|
|
|
span: Span,
|
2020-07-25 07:04:13 -04:00
|
|
|
use_spans: Option<UseSpans<'tcx>>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
2019-05-05 11:16:56 +01:00
|
|
|
// Inspect the type of the content behind the
|
|
|
|
// borrow to provide feedback about why this
|
|
|
|
// was a move rather than a copy.
|
2020-04-12 10:28:41 -07:00
|
|
|
let ty = deref_target_place.ty(self.body, self.infcx.tcx).ty;
|
2019-07-21 22:38:30 +02:00
|
|
|
let upvar_field = self
|
|
|
|
.prefixes(move_place.as_ref(), PrefixSet::All)
|
2019-07-11 19:25:37 +02:00
|
|
|
.find_map(|p| self.is_upvar_field_projection(p));
|
2019-05-05 11:16:56 +01:00
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
let deref_base = match deref_target_place.projection.as_ref() {
|
2021-01-02 20:09:17 +01:00
|
|
|
[proj_base @ .., ProjectionElem::Deref] => {
|
2020-01-14 02:10:05 -03:00
|
|
|
PlaceRef { local: deref_target_place.local, projection: &proj_base }
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2019-05-05 11:16:56 +01:00
|
|
|
_ => bug!("deref_target_place is not a deref projection"),
|
|
|
|
};
|
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
if let PlaceRef { local, projection: [] } = deref_base {
|
2020-01-14 02:10:05 -03:00
|
|
|
let decl = &self.body.local_decls[local];
|
2019-05-05 11:16:56 +01:00
|
|
|
if decl.is_ref_for_guard() {
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut err = self.cannot_move_out_of(
|
2019-05-05 11:16:56 +01:00
|
|
|
span,
|
2020-01-14 02:10:05 -03:00
|
|
|
&format!("`{}` in pattern guard", self.local_names[local].unwrap()),
|
2019-05-05 11:16:56 +01:00
|
|
|
);
|
|
|
|
err.note(
|
|
|
|
"variables bound in patterns cannot be moved from \
|
|
|
|
until after the end of the pattern guard",
|
|
|
|
);
|
|
|
|
return err;
|
2019-11-18 23:04:06 +00:00
|
|
|
} else if decl.is_ref_to_static() {
|
|
|
|
return self.report_cannot_move_from_static(move_place, span);
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("report: ty={:?}", ty);
|
2020-08-03 00:49:11 +02:00
|
|
|
let mut err = match ty.kind() {
|
2019-05-05 11:16:56 +01:00
|
|
|
ty::Array(..) | ty::Slice(..) => {
|
2019-07-12 20:37:06 +01:00
|
|
|
self.cannot_move_out_of_interior_noncopy(span, ty, None)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Closure(def_id, closure_args)
|
2020-10-04 17:23:02 -07:00
|
|
|
if def_id.as_local() == Some(self.mir_def_id()) && upvar_field.is_some() =>
|
2019-05-05 11:16:56 +01:00
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
let closure_kind_ty = closure_args.as_closure().kind_ty();
|
2021-07-25 20:05:41 +02:00
|
|
|
let closure_kind = match closure_kind_ty.to_opt_closure_kind() {
|
|
|
|
Some(kind @ (ty::ClosureKind::Fn | ty::ClosureKind::FnMut)) => kind,
|
2019-05-05 11:16:56 +01:00
|
|
|
Some(ty::ClosureKind::FnOnce) => {
|
|
|
|
bug!("closure kind does not match first argument type")
|
|
|
|
}
|
|
|
|
None => bug!("closure kind not inferred by borrowck"),
|
|
|
|
};
|
2021-07-25 20:05:41 +02:00
|
|
|
let capture_description =
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("captured variable in an `{closure_kind}` closure");
|
2019-05-05 11:16:56 +01:00
|
|
|
|
|
|
|
let upvar = &self.upvars[upvar_field.unwrap().index()];
|
2020-12-13 00:11:15 -05:00
|
|
|
let upvar_hir_id = upvar.place.get_root_variable();
|
2021-03-17 02:51:27 -04:00
|
|
|
let upvar_name = upvar.place.to_string(self.infcx.tcx);
|
2019-06-14 18:58:55 +02:00
|
|
|
let upvar_span = self.infcx.tcx.hir().span(upvar_hir_id);
|
2019-05-05 11:16:56 +01:00
|
|
|
|
2020-03-26 01:55:16 +01:00
|
|
|
let place_name = self.describe_any_place(move_place.as_ref());
|
2019-05-05 11:16:56 +01:00
|
|
|
|
2020-03-25 11:17:06 +01:00
|
|
|
let place_description =
|
|
|
|
if self.is_upvar_field_projection(move_place.as_ref()).is_some() {
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("{place_name}, a {capture_description}")
|
2020-03-25 11:17:06 +01:00
|
|
|
} else {
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("{place_name}, as `{upvar_name}` is a {capture_description}")
|
2020-03-25 11:17:06 +01:00
|
|
|
};
|
2019-05-05 11:16:56 +01:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
"report: closure_kind_ty={:?} closure_kind={:?} place_description={:?}",
|
|
|
|
closure_kind_ty, closure_kind, place_description,
|
|
|
|
);
|
|
|
|
|
2019-07-12 20:37:06 +01:00
|
|
|
let mut diag = self.cannot_move_out_of(span, &place_description);
|
2019-05-05 11:16:56 +01:00
|
|
|
|
|
|
|
diag.span_label(upvar_span, "captured outer variable");
|
2021-07-25 20:05:41 +02:00
|
|
|
diag.span_label(
|
2022-08-27 14:36:14 +02:00
|
|
|
self.infcx.tcx.def_span(def_id),
|
2022-04-09 00:01:40 +09:00
|
|
|
format!("captured by this `{closure_kind}` closure"),
|
2021-07-25 20:05:41 +02:00
|
|
|
);
|
2019-05-05 11:16:56 +01:00
|
|
|
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
_ => {
|
2019-07-19 20:53:31 +02:00
|
|
|
let source = self.borrowed_content_source(deref_base);
|
2022-07-23 00:35:35 +03:00
|
|
|
let move_place_ref = move_place.as_ref();
|
|
|
|
match (
|
|
|
|
self.describe_place_with_options(
|
|
|
|
move_place_ref,
|
|
|
|
DescribePlaceOpt {
|
|
|
|
including_downcast: false,
|
|
|
|
including_tuple_field: false,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
self.describe_name(move_place_ref),
|
|
|
|
source.describe_for_named_place(),
|
|
|
|
) {
|
|
|
|
(Some(place_desc), Some(name), Some(source_desc)) => self.cannot_move_out_of(
|
|
|
|
span,
|
|
|
|
&format!("`{place_desc}` as enum variant `{name}` which is behind a {source_desc}"),
|
|
|
|
),
|
|
|
|
(Some(place_desc), Some(name), None) => self.cannot_move_out_of(
|
|
|
|
span,
|
|
|
|
&format!("`{place_desc}` as enum variant `{name}`"),
|
|
|
|
),
|
|
|
|
(Some(place_desc), _, Some(source_desc)) => self.cannot_move_out_of(
|
2019-05-05 11:16:56 +01:00
|
|
|
span,
|
2022-04-09 00:01:40 +09:00
|
|
|
&format!("`{place_desc}` which is behind a {source_desc}"),
|
2019-05-05 11:16:56 +01:00
|
|
|
),
|
2022-07-23 00:35:35 +03:00
|
|
|
(_, _, _) => self.cannot_move_out_of(
|
2020-04-08 04:26:48 +03:00
|
|
|
span,
|
|
|
|
&source.describe_for_unnamed_place(self.infcx.tcx),
|
|
|
|
),
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-02-10 16:58:32 +08:00
|
|
|
let msg_opt = CapturedMessageOpt {
|
|
|
|
is_partial_move: false,
|
|
|
|
is_loop_message: false,
|
|
|
|
is_move_msg: false,
|
|
|
|
is_loop_move: false,
|
|
|
|
maybe_reinitialized_locations_is_empty: true,
|
|
|
|
};
|
2022-06-27 15:43:23 -07:00
|
|
|
if let Some(use_spans) = use_spans {
|
2023-02-10 16:58:32 +08:00
|
|
|
self.explain_captures(&mut err, span, span, use_spans, move_place, msg_opt);
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diagnostic, span: Span) {
|
2018-06-27 22:06:54 +01:00
|
|
|
match error {
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
|
2022-12-09 13:01:41 -08:00
|
|
|
self.add_borrow_suggestions(err, span);
|
2019-05-05 11:16:56 +01:00
|
|
|
if binds_to.is_empty() {
|
2020-04-12 10:28:41 -07:00
|
|
|
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
|
2019-07-21 22:38:30 +02:00
|
|
|
let place_desc = match self.describe_place(move_from.as_ref()) {
|
2022-04-09 00:01:40 +09:00
|
|
|
Some(desc) => format!("`{desc}`"),
|
2020-02-29 00:35:24 +01:00
|
|
|
None => "value".to_string(),
|
2019-05-05 11:16:56 +01:00
|
|
|
};
|
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
|
|
|
is_partial_move: false,
|
|
|
|
ty: place_ty,
|
|
|
|
place: &place_desc,
|
|
|
|
span,
|
|
|
|
});
|
2019-05-05 11:16:56 +01:00
|
|
|
} else {
|
|
|
|
binds_to.sort();
|
|
|
|
binds_to.dedup();
|
|
|
|
|
|
|
|
self.add_move_error_details(err, &binds_to);
|
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromValue { mut binds_to, .. } => {
|
2018-06-27 22:06:54 +01:00
|
|
|
binds_to.sort();
|
|
|
|
binds_to.dedup();
|
2018-08-07 01:02:39 -07:00
|
|
|
self.add_move_error_suggestions(err, &binds_to);
|
2018-08-14 12:54:31 -07:00
|
|
|
self.add_move_error_details(err, &binds_to);
|
2018-08-07 01:02:39 -07:00
|
|
|
}
|
|
|
|
// No binding. Nothing to suggest.
|
2019-05-05 12:02:17 +01:00
|
|
|
GroupedMoveError::OtherIllegalMove { ref original_path, use_spans, .. } => {
|
|
|
|
let span = use_spans.var_or_use();
|
2020-04-12 10:28:41 -07:00
|
|
|
let place_ty = original_path.ty(self.body, self.infcx.tcx).ty;
|
2019-07-21 22:38:30 +02:00
|
|
|
let place_desc = match self.describe_place(original_path.as_ref()) {
|
2022-04-09 00:01:40 +09:00
|
|
|
Some(desc) => format!("`{desc}`"),
|
2020-02-29 00:35:24 +01:00
|
|
|
None => "value".to_string(),
|
2019-05-05 11:16:56 +01:00
|
|
|
};
|
2023-02-10 16:58:32 +08:00
|
|
|
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
|
|
|
is_partial_move: false,
|
|
|
|
ty: place_ty,
|
|
|
|
place: &place_desc,
|
|
|
|
span,
|
|
|
|
});
|
2019-05-05 12:02:17 +01:00
|
|
|
|
2023-02-10 16:58:32 +08:00
|
|
|
use_spans.args_subdiag(err, |args_span| {
|
|
|
|
crate::session_diagnostics::CaptureArgLabel::MoveOutPlace {
|
|
|
|
place: place_desc,
|
|
|
|
args_span,
|
|
|
|
}
|
|
|
|
});
|
2019-05-05 11:16:56 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2022-12-09 13:01:41 -08:00
|
|
|
fn add_borrow_suggestions(&self, err: &mut Diagnostic, span: Span) {
|
|
|
|
match self.infcx.tcx.sess.source_map().span_to_snippet(span) {
|
|
|
|
Ok(snippet) if snippet.starts_with('*') => {
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
span.with_hi(span.lo() + BytePos(1)),
|
|
|
|
"consider removing the dereference here",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
span.shrink_to_lo(),
|
|
|
|
"consider borrowing here",
|
2023-02-27 14:27:13 +09:00
|
|
|
'&',
|
2022-12-09 13:01:41 -08:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
fn add_move_error_suggestions(&self, err: &mut Diagnostic, binds_to: &[Local]) {
|
2022-12-08 17:14:56 -08:00
|
|
|
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
|
2018-08-07 01:02:39 -07:00
|
|
|
for local in binds_to {
|
2019-11-06 12:00:46 -05:00
|
|
|
let bind_to = &self.body.local_decls[*local];
|
2023-03-09 16:55:20 +00:00
|
|
|
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span, .. })) =
|
|
|
|
*bind_to.local_info()
|
2019-11-18 23:04:06 +00:00
|
|
|
{
|
2022-12-08 09:02:54 -08:00
|
|
|
let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
};
|
2022-12-08 17:14:56 -08:00
|
|
|
let Some(stripped) = pat_snippet.strip_prefix('&') else {
|
|
|
|
suggestions.push((
|
|
|
|
bind_to.source_info.span.shrink_to_lo(),
|
|
|
|
"consider borrowing the pattern binding".to_string(),
|
|
|
|
"ref ".to_string(),
|
|
|
|
));
|
|
|
|
continue;
|
|
|
|
};
|
2022-12-08 09:02:54 -08:00
|
|
|
let inner_pat_snippet = stripped.trim_start();
|
|
|
|
let (pat_span, suggestion, to_remove) = if inner_pat_snippet.starts_with("mut")
|
|
|
|
&& inner_pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
|
2019-08-24 18:25:34 +01:00
|
|
|
{
|
2022-12-08 21:09:56 -08:00
|
|
|
let inner_pat_snippet = inner_pat_snippet["mut".len()..].trim_start();
|
2022-12-08 09:02:54 -08:00
|
|
|
let pat_span = pat_span.with_hi(
|
|
|
|
pat_span.lo()
|
|
|
|
+ BytePos((pat_snippet.len() - inner_pat_snippet.len()) as u32),
|
|
|
|
);
|
|
|
|
(pat_span, String::new(), "mutable borrow")
|
|
|
|
} else {
|
|
|
|
let pat_span = pat_span.with_hi(
|
|
|
|
pat_span.lo()
|
|
|
|
+ BytePos(
|
|
|
|
(pat_snippet.len() - inner_pat_snippet.trim_start().len()) as u32,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
(pat_span, String::new(), "borrow")
|
|
|
|
};
|
2022-12-08 17:14:56 -08:00
|
|
|
suggestions.push((
|
|
|
|
pat_span,
|
|
|
|
format!("consider removing the {to_remove}"),
|
|
|
|
suggestion.to_string(),
|
|
|
|
));
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 03:17:53 -07:00
|
|
|
suggestions.sort_unstable_by_key(|&(span, _, _)| span);
|
|
|
|
suggestions.dedup_by_key(|&mut (span, _, _)| span);
|
2022-12-08 17:14:56 -08:00
|
|
|
for (span, msg, suggestion) in suggestions {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
err.span_suggestion_verbose(span, msg, suggestion, Applicability::MachineApplicable);
|
2018-08-13 03:17:53 -07:00
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:41:46 +00:00
|
|
|
fn add_move_error_details(&self, err: &mut Diagnostic, binds_to: &[Local]) {
|
2020-02-29 03:05:14 +01:00
|
|
|
for (j, local) in binds_to.iter().enumerate() {
|
2019-11-06 12:00:46 -05:00
|
|
|
let bind_to = &self.body.local_decls[*local];
|
2018-08-07 01:02:39 -07:00
|
|
|
let binding_span = bind_to.source_info.span;
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
if j == 0 {
|
2018-10-16 15:10:59 +02:00
|
|
|
err.span_label(binding_span, "data moved here");
|
2018-08-07 01:02:39 -07:00
|
|
|
} else {
|
2018-10-16 15:10:59 +02:00
|
|
|
err.span_label(binding_span, "...and here");
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
|
2018-08-13 14:51:27 -07:00
|
|
|
if binds_to.len() == 1 {
|
2023-02-10 16:58:32 +08:00
|
|
|
let place_desc = &format!("`{}`", self.local_names[*local].unwrap());
|
|
|
|
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
|
|
|
|
is_partial_move: false,
|
|
|
|
ty: bind_to.ty,
|
|
|
|
place: &place_desc,
|
|
|
|
span: binding_span,
|
|
|
|
});
|
2018-08-13 14:51:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if binds_to.len() > 1 {
|
2019-11-25 12:32:57 -08:00
|
|
|
err.note(
|
2022-12-08 09:02:54 -08:00
|
|
|
"move occurs because these variables have types that don't implement the `Copy` \
|
|
|
|
trait",
|
2018-08-07 01:02:39 -07:00
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|