Skip to content

Commit dcc4b16

Browse files
committed
When NLL has illegal move due to borrowed content, provide feedback about why the move wasn't a copy.
This should address rust-lang#51190.
1 parent 20af72b commit dcc4b16

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &'a BorrowckCtxt<'a, 'tcx>,
148148
}
149149
Categorization::Interior(ref b, mc::InteriorElement(ik)) => {
150150
bccx.cannot_move_out_of_interior_noncopy(
151-
move_from.span, b.ty, ik == Kind::Index, Origin::Ast)
151+
move_from.span, b.ty, Some(ik == Kind::Index), Origin::Ast)
152152
}
153153

154154
Categorization::Downcast(ref b, _) |

src/librustc_mir/borrow_check/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,21 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
132132
IllegalMoveOriginKind::Static => {
133133
tcx.cannot_move_out_of(span, "static item", origin)
134134
}
135-
IllegalMoveOriginKind::BorrowedContent => {
136-
tcx.cannot_move_out_of(span, "borrowed content", origin)
135+
IllegalMoveOriginKind::BorrowedContent { target_ty: ty } => {
136+
// Inspect the type of the content behind the
137+
// borrow to provide feedback about why this
138+
// was a move rather than a copy.
139+
match ty.sty {
140+
ty::TyArray(..) | ty::TySlice(..) =>
141+
tcx.cannot_move_out_of_interior_noncopy(span, ty, None, origin),
142+
_ => tcx.cannot_move_out_of(span, "borrowed content", origin)
143+
}
137144
}
138145
IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
139146
tcx.cannot_move_out_of_interior_of_drop(span, ty, origin)
140147
}
141148
IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => {
142-
tcx.cannot_move_out_of_interior_noncopy(span, ty, is_index, origin)
149+
tcx.cannot_move_out_of_interior_noncopy(span, ty, Some(is_index), origin)
143150
}
144151
};
145152
err.emit();

src/librustc_mir/dataflow/move_paths/builder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
119119
}
120120

121121
fn create_move_path(&mut self, place: &Place<'tcx>) {
122-
// This is an assignment, not a move, so this not being a valid
123-
// move path is OK.
122+
// This is an non-moving access (such as an overwrite or
123+
// drop), so this not being a valid move path is OK.
124124
let _ = self.move_path_for(place);
125125
}
126126

@@ -135,8 +135,9 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
135135
let place_ty = proj.base.ty(mir, tcx).to_ty(tcx);
136136
match place_ty.sty {
137137
ty::TyRef(..) | ty::TyRawPtr(..) =>
138-
return Err(MoveError::cannot_move_out_of(mir.source_info(self.loc).span,
139-
BorrowedContent)),
138+
return Err(MoveError::cannot_move_out_of(
139+
mir.source_info(self.loc).span,
140+
BorrowedContent { target_ty: place.ty(mir, tcx).to_ty(tcx) })),
140141
ty::TyAdt(adt, _) if adt.has_dtor(tcx) && !adt.is_box() =>
141142
return Err(MoveError::cannot_move_out_of(mir.source_info(self.loc).span,
142143
InteriorOfTypeWithDestructor {

src/librustc_mir/dataflow/move_paths/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,23 @@ pub struct IllegalMoveOrigin<'tcx> {
277277

278278
#[derive(Debug)]
279279
pub(crate) enum IllegalMoveOriginKind<'tcx> {
280+
/// Illegal move due to attempt to move from `static` variable.
280281
Static,
281-
BorrowedContent,
282+
283+
/// Illegal move due to attempt to move from behind a reference.
284+
BorrowedContent {
285+
/// The content's type: if erroneous code was trying to move
286+
/// from `*x` where `x: &T`, then this will be `T`.
287+
target_ty: ty::Ty<'tcx>,
288+
},
289+
290+
/// Illegal move due to attempt to move from field of an ADT that
291+
/// implements `Drop`. Rust maintains invariant that all `Drop`
292+
/// ADT's remain fully-initialized so that user-defined destructor
293+
/// can safely read from all of the ADT's fields.
282294
InteriorOfTypeWithDestructor { container_ty: ty::Ty<'tcx> },
295+
296+
/// Illegal move due to attempt to move out of a slice or array.
283297
InteriorOfSliceOrArray { ty: ty::Ty<'tcx>, is_index: bool, },
284298
}
285299

src/librustc_mir/util/borrowck_errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,19 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
312312
self.cancel_if_wrong_origin(err, o)
313313
}
314314

315+
/// Signal an error due to an attempt to move out of the interior
316+
/// of an array or slice. `is_index` is None when error origin
317+
/// didn't capture whether there was an indexing operation or not.
315318
fn cannot_move_out_of_interior_noncopy(self,
316319
move_from_span: Span,
317320
ty: ty::Ty,
318-
is_index: bool,
321+
is_index: Option<bool>,
319322
o: Origin)
320323
-> DiagnosticBuilder<'cx>
321324
{
322325
let type_name = match (&ty.sty, is_index) {
323-
(&ty::TyArray(_, _), true) => "array",
326+
(&ty::TyArray(_, _), Some(true)) |
327+
(&ty::TyArray(_, _), None) => "array",
324328
(&ty::TySlice(_), _) => "slice",
325329
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
326330
};

0 commit comments

Comments
 (0)