Skip to content

Commit a995b56

Browse files
committed
Move E0508 diagnostic into mod borrowck_errors shared between ast- and mir-borrowck.
1 parent a12cefb commit a995b56

File tree

4 files changed

+67
-58
lines changed

4 files changed

+67
-58
lines changed

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,8 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &'a BorrowckCtxt<'a, 'tcx>,
147147
move_from.span, &move_from.descriptive_string(bccx.tcx), Origin::Ast)
148148
}
149149
Categorization::Interior(ref b, mc::InteriorElement(ik)) => {
150-
let type_name = match (&b.ty.sty, ik) {
151-
(&ty::TyArray(_, _), Kind::Index) => "array",
152-
(&ty::TySlice(_), _) => "slice",
153-
_ => {
154-
span_bug!(move_from.span, "this path should not cause illegal move");
155-
},
156-
};
157-
let mut err = struct_span_err!(bccx, move_from.span, E0508,
158-
"cannot move out of type `{}`, \
159-
a non-copy {}",
160-
b.ty, type_name);
161-
err.span_label(move_from.span, "cannot move out of here");
162-
err
150+
bccx.cannot_move_out_of_interior_noncopy(
151+
move_from.span, b.ty, ik == Kind::Index, Origin::Ast)
163152
}
164153

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

src/librustc_borrowck/diagnostics.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -317,51 +317,6 @@ fn main() {
317317
```
318318
"##,
319319

320-
321-
E0508: r##"
322-
A value was moved out of a non-copy fixed-size array.
323-
324-
Example of erroneous code:
325-
326-
```compile_fail,E0508
327-
struct NonCopy;
328-
329-
fn main() {
330-
let array = [NonCopy; 1];
331-
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
332-
// a non-copy fixed-size array
333-
}
334-
```
335-
336-
The first element was moved out of the array, but this is not
337-
possible because `NonCopy` does not implement the `Copy` trait.
338-
339-
Consider borrowing the element instead of moving it:
340-
341-
```
342-
struct NonCopy;
343-
344-
fn main() {
345-
let array = [NonCopy; 1];
346-
let _value = &array[0]; // Borrowing is allowed, unlike moving.
347-
}
348-
```
349-
350-
Alternatively, if your type implements `Clone` and you need to own the value,
351-
consider borrowing and then cloning:
352-
353-
```
354-
#[derive(Clone)]
355-
struct NonCopy;
356-
357-
fn main() {
358-
let array = [NonCopy; 1];
359-
// Now you can clone the array element.
360-
let _value = array[0].clone();
361-
}
362-
```
363-
"##,
364-
365320
E0509: r##"
366321
This error occurs when an attempt is made to move out of a value whose type
367322
implements the `Drop` trait.

src/librustc_mir/diagnostics.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,51 @@ You can find more information about borrowing in the rust-book:
11261126
http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
11271127
"##,
11281128

1129+
E0508: r##"
1130+
A value was moved out of a non-copy fixed-size array.
1131+
1132+
Example of erroneous code:
1133+
1134+
```compile_fail,E0508
1135+
struct NonCopy;
1136+
1137+
fn main() {
1138+
let array = [NonCopy; 1];
1139+
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
1140+
// a non-copy fixed-size array
1141+
}
1142+
```
1143+
1144+
The first element was moved out of the array, but this is not
1145+
possible because `NonCopy` does not implement the `Copy` trait.
1146+
1147+
Consider borrowing the element instead of moving it:
1148+
1149+
```
1150+
struct NonCopy;
1151+
1152+
fn main() {
1153+
let array = [NonCopy; 1];
1154+
let _value = &array[0]; // Borrowing is allowed, unlike moving.
1155+
}
1156+
```
1157+
1158+
Alternatively, if your type implements `Clone` and you need to own the value,
1159+
consider borrowing and then cloning:
1160+
1161+
```
1162+
#[derive(Clone)]
1163+
struct NonCopy;
1164+
1165+
fn main() {
1166+
let array = [NonCopy; 1];
1167+
// Now you can clone the array element.
1168+
let _value = array[0].clone();
1169+
}
1170+
```
1171+
"##,
1172+
1173+
11291174
}
11301175

11311176
register_diagnostics! {

src/librustc_mir/util/borrowck_errors.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ pub trait BorrowckErrors {
203203
format!("cannot move out of {}", move_from_desc));
204204
err
205205
}
206+
207+
fn cannot_move_out_of_interior_noncopy(&self,
208+
move_from_span: Span,
209+
ty: ty::Ty,
210+
is_index: bool,
211+
o: Origin)
212+
-> DiagnosticBuilder
213+
{
214+
let type_name = match (&ty.sty, is_index) {
215+
(&ty::TyArray(_, _), true) => "array",
216+
(&ty::TySlice(_), _) => "slice",
217+
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
218+
};
219+
let mut err = struct_span_err!(self, move_from_span, E0508,
220+
"cannot move out of type `{}`, \
221+
a non-copy {}{OGN}",
222+
ty, type_name, OGN=o);
223+
err.span_label(move_from_span, "cannot move out of here");
224+
err
225+
}
206226
}
207227

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

0 commit comments

Comments
 (0)