Skip to content

Commit

Permalink
Move E0508 diagnostic into mod borrowck_errors shared between ast- an…
Browse files Browse the repository at this point in the history
…d mir-borrowck.
  • Loading branch information
pnkfelix committed Oct 4, 2017
1 parent a12cefb commit a995b56
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
15 changes: 2 additions & 13 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Expand Up @@ -147,19 +147,8 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &'a BorrowckCtxt<'a, 'tcx>,
move_from.span, &move_from.descriptive_string(bccx.tcx), Origin::Ast)
}
Categorization::Interior(ref b, mc::InteriorElement(ik)) => {
let type_name = match (&b.ty.sty, ik) {
(&ty::TyArray(_, _), Kind::Index) => "array",
(&ty::TySlice(_), _) => "slice",
_ => {
span_bug!(move_from.span, "this path should not cause illegal move");
},
};
let mut err = struct_span_err!(bccx, move_from.span, E0508,
"cannot move out of type `{}`, \
a non-copy {}",
b.ty, type_name);
err.span_label(move_from.span, "cannot move out of here");
err
bccx.cannot_move_out_of_interior_noncopy(
move_from.span, b.ty, ik == Kind::Index, Origin::Ast)
}

Categorization::Downcast(ref b, _) |
Expand Down
45 changes: 0 additions & 45 deletions src/librustc_borrowck/diagnostics.rs
Expand Up @@ -317,51 +317,6 @@ fn main() {
```
"##,


E0508: r##"
A value was moved out of a non-copy fixed-size array.
Example of erroneous code:
```compile_fail,E0508
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
// a non-copy fixed-size array
}
```
The first element was moved out of the array, but this is not
possible because `NonCopy` does not implement the `Copy` trait.
Consider borrowing the element instead of moving it:
```
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
let _value = &array[0]; // Borrowing is allowed, unlike moving.
}
```
Alternatively, if your type implements `Clone` and you need to own the value,
consider borrowing and then cloning:
```
#[derive(Clone)]
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
// Now you can clone the array element.
let _value = array[0].clone();
}
```
"##,

E0509: r##"
This error occurs when an attempt is made to move out of a value whose type
implements the `Drop` trait.
Expand Down
45 changes: 45 additions & 0 deletions src/librustc_mir/diagnostics.rs
Expand Up @@ -1126,6 +1126,51 @@ You can find more information about borrowing in the rust-book:
http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
"##,

E0508: r##"
A value was moved out of a non-copy fixed-size array.
Example of erroneous code:
```compile_fail,E0508
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
// a non-copy fixed-size array
}
```
The first element was moved out of the array, but this is not
possible because `NonCopy` does not implement the `Copy` trait.
Consider borrowing the element instead of moving it:
```
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
let _value = &array[0]; // Borrowing is allowed, unlike moving.
}
```
Alternatively, if your type implements `Clone` and you need to own the value,
consider borrowing and then cloning:
```
#[derive(Clone)]
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
// Now you can clone the array element.
let _value = array[0].clone();
}
```
"##,


}

register_diagnostics! {
Expand Down
20 changes: 20 additions & 0 deletions src/librustc_mir/util/borrowck_errors.rs
Expand Up @@ -203,6 +203,26 @@ pub trait BorrowckErrors {
format!("cannot move out of {}", move_from_desc));
err
}

fn cannot_move_out_of_interior_noncopy(&self,
move_from_span: Span,
ty: ty::Ty,
is_index: bool,
o: Origin)
-> DiagnosticBuilder
{
let type_name = match (&ty.sty, is_index) {
(&ty::TyArray(_, _), true) => "array",
(&ty::TySlice(_), _) => "slice",
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
};
let mut err = struct_span_err!(self, move_from_span, E0508,
"cannot move out of type `{}`, \
a non-copy {}{OGN}",
ty, type_name, OGN=o);
err.span_label(move_from_span, "cannot move out of here");
err
}
}

impl<'b, 'tcx, 'gcx> BorrowckErrors for TyCtxt<'b, 'tcx, 'gcx> {
Expand Down

0 comments on commit a995b56

Please sign in to comment.