Skip to content

Commit 37cfa75

Browse files
Add error codes for librustc_borrowck
1 parent 8dfb890 commit 37cfa75

File tree

3 files changed

+81
-54
lines changed

3 files changed

+81
-54
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

+34-40
Original file line numberDiff line numberDiff line change
@@ -464,40 +464,36 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
464464

465465
match (new_loan.kind, old_loan.kind) {
466466
(ty::MutBorrow, ty::MutBorrow) => {
467-
self.bccx.span_err(
468-
new_loan.span,
469-
&format!("cannot borrow `{}`{} as mutable \
470-
more than once at a time",
471-
nl, new_loan_msg))
467+
span_err!(self.bccx, new_loan.span, E0499,
468+
"cannot borrow `{}`{} as mutable \
469+
more than once at a time",
470+
nl, new_loan_msg);
472471
}
473472

474473
(ty::UniqueImmBorrow, _) => {
475-
self.bccx.span_err(
476-
new_loan.span,
477-
&format!("closure requires unique access to `{}` \
478-
but {} is already borrowed{}",
479-
nl, ol_pronoun, old_loan_msg));
474+
span_err!(self.bccx, new_loan.span, E0500,
475+
"closure requires unique access to `{}` \
476+
but {} is already borrowed{}",
477+
nl, ol_pronoun, old_loan_msg);
480478
}
481479

482480
(_, ty::UniqueImmBorrow) => {
483-
self.bccx.span_err(
484-
new_loan.span,
485-
&format!("cannot borrow `{}`{} as {} because \
486-
previous closure requires unique access",
487-
nl, new_loan_msg, new_loan.kind.to_user_str()));
481+
span_err!(self.bccx, new_loan.span, E0501,
482+
"cannot borrow `{}`{} as {} because \
483+
previous closure requires unique access",
484+
nl, new_loan_msg, new_loan.kind.to_user_str());
488485
}
489486

490487
(_, _) => {
491-
self.bccx.span_err(
492-
new_loan.span,
493-
&format!("cannot borrow `{}`{} as {} because \
494-
{} is also borrowed as {}{}",
495-
nl,
496-
new_loan_msg,
497-
new_loan.kind.to_user_str(),
498-
ol_pronoun,
499-
old_loan.kind.to_user_str(),
500-
old_loan_msg));
488+
span_err!(self.bccx, new_loan.span, E0502,
489+
"cannot borrow `{}`{} as {} because \
490+
{} is also borrowed as {}{}",
491+
nl,
492+
new_loan_msg,
493+
new_loan.kind.to_user_str(),
494+
ol_pronoun,
495+
old_loan.kind.to_user_str(),
496+
old_loan_msg);
501497
}
502498
}
503499

@@ -617,11 +613,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
617613
match self.analyze_restrictions_on_use(id, copy_path, ty::ImmBorrow) {
618614
UseOk => { }
619615
UseWhileBorrowed(loan_path, loan_span) => {
620-
self.bccx.span_err(
621-
span,
622-
&format!("cannot use `{}` because it was mutably borrowed",
623-
&self.bccx.loan_path_to_string(copy_path))
624-
);
616+
span_err!(self.bccx, span, E0503,
617+
"cannot use `{}` because it was mutably borrowed",
618+
&self.bccx.loan_path_to_string(copy_path));
625619
self.bccx.span_note(
626620
loan_span,
627621
&format!("borrow of `{}` occurs here",
@@ -642,18 +636,19 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
642636
match self.analyze_restrictions_on_use(id, move_path, ty::MutBorrow) {
643637
UseOk => { }
644638
UseWhileBorrowed(loan_path, loan_span) => {
645-
let err_message = match move_kind {
639+
match move_kind {
646640
move_data::Captured =>
647-
format!("cannot move `{}` into closure because it is borrowed",
648-
&self.bccx.loan_path_to_string(move_path)),
641+
span_err!(self.bccx, span, E0504,
642+
"cannot move `{}` into closure because it is borrowed",
643+
&self.bccx.loan_path_to_string(move_path)),
649644
move_data::Declared |
650645
move_data::MoveExpr |
651646
move_data::MovePat =>
652-
format!("cannot move out of `{}` because it is borrowed",
653-
&self.bccx.loan_path_to_string(move_path))
647+
span_err!(self.bccx, span, E0505,
648+
"cannot move out of `{}` because it is borrowed",
649+
&self.bccx.loan_path_to_string(move_path))
654650
};
655651

656-
self.bccx.span_err(span, &err_message[..]);
657652
self.bccx.span_note(
658653
loan_span,
659654
&format!("borrow of `{}` occurs here",
@@ -820,10 +815,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
820815
span: Span,
821816
loan_path: &LoanPath<'tcx>,
822817
loan: &Loan) {
823-
self.bccx.span_err(
824-
span,
825-
&format!("cannot assign to `{}` because it is borrowed",
826-
self.bccx.loan_path_to_string(loan_path)));
818+
span_err!(self.bccx, span, E0506,
819+
"cannot assign to `{}` because it is borrowed",
820+
self.bccx.loan_path_to_string(loan_path));
827821
self.bccx.span_note(
828822
loan.span,
829823
&format!("borrow of `{}` occurs here",

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,18 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
119119
mc::cat_deref(_, _, mc::Implicit(..)) |
120120
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
121121
mc::cat_static_item => {
122-
bccx.span_err(move_from.span,
123-
&format!("cannot move out of {}",
124-
move_from.descriptive_string(bccx.tcx)));
122+
span_err!(bccx, move_from.span, E0507,
123+
"cannot move out of {}",
124+
move_from.descriptive_string(bccx.tcx));
125125
}
126126

127127
mc::cat_interior(ref b, mc::InteriorElement(Kind::Index, _)) => {
128128
let expr = bccx.tcx.map.expect_expr(move_from.id);
129129
if let hir::ExprIndex(..) = expr.node {
130-
bccx.span_err(move_from.span,
131-
&format!("cannot move out of type `{}`, \
132-
a non-copy fixed-size array",
133-
b.ty));
130+
span_err!(bccx, move_from.span, E0508,
131+
"cannot move out of type `{}`, \
132+
a non-copy fixed-size array",
133+
b.ty);
134134
}
135135
}
136136

@@ -139,11 +139,10 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
139139
match b.ty.sty {
140140
ty::TyStruct(def, _) |
141141
ty::TyEnum(def, _) if def.has_dtor() => {
142-
bccx.span_err(
143-
move_from.span,
144-
&format!("cannot move out of type `{}`, \
145-
which defines the `Drop` trait",
146-
b.ty));
142+
span_err!(bccx, move_from.span, E0509,
143+
"cannot move out of type `{}`, \
144+
which defines the `Drop` trait",
145+
b.ty);
147146
},
148147
_ => {
149148
bccx.span_bug(move_from.span, "this path should not cause illegal move")

src/librustc_borrowck/diagnostics.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,46 @@ fn mutable() {
263263
You can read more about cell types in the API documentation:
264264
265265
https://doc.rust-lang.org/std/cell/
266-
"##
266+
"##,
267+
268+
E0499: r##"
269+
A variable was borrowed as mutable more than once. Erroneous code example:
270+
271+
```
272+
let mut i = 0;
273+
let mut x = &mut i;
274+
let mut a = &mut i;
275+
// error: cannot borrow `i` as mutable more than once at a time
276+
```
277+
278+
Please note that in rust, you can have as many reference on a variable as you
279+
want, but only one can be mutable. Example:
280+
281+
282+
```
283+
let mut i = 0;
284+
let mut x = &mut i;
285+
let mut a = &i;
286+
let mut b = &i;
287+
let mut c = &i;
288+
// ...
289+
```
290+
"##,
267291

268292
}
269293

270294
register_diagnostics! {
271295
E0385, // {} in an aliasable location
272296
E0388, // {} in a static location
273-
E0389 // {} in a `&` reference
297+
E0389, // {} in a `&` reference
298+
E0500, // closure requires unique access to `..` but .. is already borrowed
299+
E0501, // cannot borrow `..`.. as .. because previous closure requires unique access
300+
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
301+
E0503, // cannot use `..` because it was mutably borrowed
302+
E0504, // cannot move `..` into closure because it is borrowed
303+
E0505, // cannot move out of `..` because it is borrowed
304+
E0506, // cannot assign to `..` because it is borrowed
305+
E0507, // cannot move out of ..
306+
E0508, // cannot move out of type `..`, a non-copy fixed-size array
307+
E0509, // cannot move out of type `..`, which defines the `Drop` trait
274308
}

0 commit comments

Comments
 (0)