Skip to content

Commit 8be89f5

Browse files
Add new error codes
1 parent 3e7908f commit 8be89f5

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

src/librustc_borrowck/borrowck/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
708708
fn bckerr_to_diag(&self, err: &BckError<'tcx>) -> DiagnosticBuilder<'a> {
709709
let span = err.span.clone();
710710

711-
let msg = match err.code {
711+
match err.code {
712712
err_mutbl => {
713713
let descr = match err.cmt.note {
714714
mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
@@ -732,10 +732,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
732732

733733
match err.cause {
734734
MutabilityViolation => {
735-
format!("cannot assign to {}", descr)
735+
struct_span_err!(self.tcx.sess, span, E0594, "cannot assign to {}", descr)
736736
}
737737
BorrowViolation(euv::ClosureCapture(_)) => {
738-
format!("closure cannot assign to {}", descr)
738+
struct_span_err!(self.tcx.sess, span, E0595,
739+
"closure cannot assign to {}", descr)
739740
}
740741
BorrowViolation(euv::OverloadedOperator) |
741742
BorrowViolation(euv::AddrOf) |
@@ -744,7 +745,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
744745
BorrowViolation(euv::AutoUnsafe) |
745746
BorrowViolation(euv::ForLoop) |
746747
BorrowViolation(euv::MatchDiscriminant) => {
747-
format!("cannot borrow {} as mutable", descr)
748+
struct_span_err!(self.tcx.sess, span, E0596,
749+
"cannot borrow {} as mutable", descr)
748750
}
749751
BorrowViolation(euv::ClosureInvocation) => {
750752
span_bug!(err.span,
@@ -759,17 +761,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
759761
format!("`{}`", self.loan_path_to_string(&lp))
760762
}
761763
};
762-
format!("{} does not live long enough", msg)
764+
struct_span_err!(self.tcx.sess, span, E0597, "{} does not live long enough", msg)
763765
}
764766
err_borrowed_pointer_too_short(..) => {
765767
let descr = self.cmt_to_path_or_string(&err.cmt);
766-
format!("lifetime of {} is too short to guarantee \
767-
its contents can be safely reborrowed",
768-
descr)
768+
struct_span_err!(self.tcx.sess, span, E0598,
769+
"lifetime of {} is too short to guarantee \
770+
its contents can be safely reborrowed",
771+
descr)
769772
}
770-
};
771-
772-
self.struct_span_err(span, &msg)
773+
}
773774
}
774775

775776
pub fn report_aliasability_violation(&self,
@@ -1176,7 +1177,7 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
11761177
if kind == ty::ClosureKind::Fn {
11771178
db.span_help(self.tcx.hir.span(upvar_id.closure_expr_id),
11781179
"consider changing this closure to take \
1179-
self by mutable reference");
1180+
self by mutable reference");
11801181
}
11811182
}
11821183
_ => {

src/librustc_borrowck/diagnostics.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,63 @@ fn main() {
11141114
```
11151115
"##,
11161116

1117+
E0596: r##"
1118+
This error occurs because you tried to mutably borrow a non-mutable variable.
1119+
1120+
Example of erroneous code:
1121+
1122+
```compile_fail,E0596
1123+
let x = 1;
1124+
let y = &mut x; // error: cannot borrow mutably
1125+
```
1126+
1127+
In here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it
1128+
fails. To fix this error, you need to make `x` mutable:
1129+
1130+
```
1131+
let mut x = 1;
1132+
let y = &mut x; // ok!
1133+
```
1134+
"##,
1135+
1136+
E0597: r##"
1137+
This error occurs because a borrow was made inside a variable which has a
1138+
greater lifetime than the borrowed one.
1139+
1140+
Example of erroneous code:
1141+
1142+
```compile_fail,E0597
1143+
struct Foo<'a> {
1144+
x: Option<&'a u32>,
1145+
}
1146+
1147+
let mut x = Foo { x: None };
1148+
let y = 0;
1149+
x.x = Some(&y); // error: `y` does not live long enough
1150+
```
1151+
1152+
In here, `x` is created before `y` and therefore has a greater lifetime. Always
1153+
keep in mind that values in a scope are dropped in the opposite order they are
1154+
created. So to fix the previous example, just make the `y` lifetime greater than
1155+
the `x`'s one:
1156+
1157+
```
1158+
struct Foo<'a> {
1159+
x: Option<&'a u32>,
1160+
}
1161+
1162+
let y = 0;
1163+
let mut x = Foo { x: None };
1164+
x.x = Some(&y);
1165+
```
1166+
"##,
1167+
11171168
}
11181169

11191170
register_diagnostics! {
11201171
// E0385, // {} in an aliasable location
11211172
E0524, // two closures require unique access to `..` at the same time
1173+
E0594, // cannot assign to {}
1174+
E0595, // closure cannot assign to {}
1175+
E0598, // lifetime of {} is too short to guarantee its contents can be...
11221176
}

src/test/compile-fail/E0596.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = 1;
13+
let y = &mut x; //~ ERROR E0596
14+
}

src/test/compile-fail/E0597.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo<'a> {
12+
x: Option<&'a u32>,
13+
}
14+
15+
fn main() {
16+
let mut x = Foo { x: None };
17+
let y = 0;
18+
x.x = Some(&y);
19+
} //~ `y` does not live long enough [E0597]

0 commit comments

Comments
 (0)