Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add new error codes
  • Loading branch information
GuillaumeGomez committed May 27, 2017
1 parent 3e7908f commit 8be89f5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -708,7 +708,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
fn bckerr_to_diag(&self, err: &BckError<'tcx>) -> DiagnosticBuilder<'a> {
let span = err.span.clone();

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

match err.cause {
MutabilityViolation => {
format!("cannot assign to {}", descr)
struct_span_err!(self.tcx.sess, span, E0594, "cannot assign to {}", descr)
}
BorrowViolation(euv::ClosureCapture(_)) => {
format!("closure cannot assign to {}", descr)
struct_span_err!(self.tcx.sess, span, E0595,
"closure cannot assign to {}", descr)
}
BorrowViolation(euv::OverloadedOperator) |
BorrowViolation(euv::AddrOf) |
Expand All @@ -744,7 +745,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
BorrowViolation(euv::AutoUnsafe) |
BorrowViolation(euv::ForLoop) |
BorrowViolation(euv::MatchDiscriminant) => {
format!("cannot borrow {} as mutable", descr)
struct_span_err!(self.tcx.sess, span, E0596,
"cannot borrow {} as mutable", descr)
}
BorrowViolation(euv::ClosureInvocation) => {
span_bug!(err.span,
Expand All @@ -759,17 +761,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
format!("`{}`", self.loan_path_to_string(&lp))
}
};
format!("{} does not live long enough", msg)
struct_span_err!(self.tcx.sess, span, E0597, "{} does not live long enough", msg)
}
err_borrowed_pointer_too_short(..) => {
let descr = self.cmt_to_path_or_string(&err.cmt);
format!("lifetime of {} is too short to guarantee \
its contents can be safely reborrowed",
descr)
struct_span_err!(self.tcx.sess, span, E0598,
"lifetime of {} is too short to guarantee \
its contents can be safely reborrowed",
descr)
}
};

self.struct_span_err(span, &msg)
}
}

pub fn report_aliasability_violation(&self,
Expand Down Expand Up @@ -1176,7 +1177,7 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
if kind == ty::ClosureKind::Fn {
db.span_help(self.tcx.hir.span(upvar_id.closure_expr_id),
"consider changing this closure to take \
self by mutable reference");
self by mutable reference");
}
}
_ => {
Expand Down
54 changes: 54 additions & 0 deletions src/librustc_borrowck/diagnostics.rs
Expand Up @@ -1114,9 +1114,63 @@ fn main() {
```
"##,

E0596: r##"
This error occurs because you tried to mutably borrow a non-mutable variable.
Example of erroneous code:
```compile_fail,E0596
let x = 1;
let y = &mut x; // error: cannot borrow mutably
```
In here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it
fails. To fix this error, you need to make `x` mutable:
```
let mut x = 1;
let y = &mut x; // ok!
```
"##,

E0597: r##"
This error occurs because a borrow was made inside a variable which has a
greater lifetime than the borrowed one.
Example of erroneous code:
```compile_fail,E0597
struct Foo<'a> {
x: Option<&'a u32>,
}
let mut x = Foo { x: None };
let y = 0;
x.x = Some(&y); // error: `y` does not live long enough
```
In here, `x` is created before `y` and therefore has a greater lifetime. Always
keep in mind that values in a scope are dropped in the opposite order they are
created. So to fix the previous example, just make the `y` lifetime greater than
the `x`'s one:
```
struct Foo<'a> {
x: Option<&'a u32>,
}
let y = 0;
let mut x = Foo { x: None };
x.x = Some(&y);
```
"##,

}

register_diagnostics! {
// E0385, // {} in an aliasable location
E0524, // two closures require unique access to `..` at the same time
E0594, // cannot assign to {}
E0595, // closure cannot assign to {}
E0598, // lifetime of {} is too short to guarantee its contents can be...
}
14 changes: 14 additions & 0 deletions src/test/compile-fail/E0596.rs
@@ -0,0 +1,14 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let x = 1;
let y = &mut x; //~ ERROR E0596
}
19 changes: 19 additions & 0 deletions src/test/compile-fail/E0597.rs
@@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct Foo<'a> {
x: Option<&'a u32>,
}

fn main() {
let mut x = Foo { x: None };
let y = 0;
x.x = Some(&y);
} //~ `y` does not live long enough [E0597]

0 comments on commit 8be89f5

Please sign in to comment.