Skip to content

Commit 52c5173

Browse files
committed
improve borrowck error messages to explain regions better
1 parent 99af0d5 commit 52c5173

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2917,7 +2917,7 @@ class parser {
29172917
body: d_body},
29182918
span: d_s}
29192919
};
2920-
2920+
29212921
kind = struct_variant_kind(@{
29222922
traits: ~[],
29232923
members: ms,

src/rustc/middle/borrowck.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ import syntax::visit;
220220
import syntax::ast_util;
221221
import syntax::ast_map;
222222
import syntax::codemap::span;
223-
import util::ppaux::{ty_to_str, region_to_str};
223+
import util::ppaux::{ty_to_str, region_to_str, explain_region};
224224
import std::map::{int_hash, hashmap, set};
225225
import std::list;
226226
import std::list::{list, cons, nil};
@@ -626,16 +626,16 @@ impl to_str_methods for borrowck_ctxt {
626626
~"rooting is not permitted"
627627
}
628628
err_out_of_root_scope(super_scope, sub_scope) => {
629-
fmt!{"managed value would have to be rooted for lifetime %s, \
630-
but can only be rooted for lifetime %s",
631-
self.region_to_str(sub_scope),
632-
self.region_to_str(super_scope)}
629+
fmt!{"managed value would have to be rooted for %s, \
630+
but can only be rooted for %s",
631+
explain_region(self.tcx, sub_scope),
632+
explain_region(self.tcx, super_scope)}
633633
}
634634
err_out_of_scope(super_scope, sub_scope) => {
635-
fmt!{"borrowed pointer has lifetime %s, \
636-
but the borrowed value only has lifetime %s",
637-
self.region_to_str(sub_scope),
638-
self.region_to_str(super_scope)}
635+
fmt!{"borrowed pointer must be valid for %s, \
636+
but the borrowed value is only valid for %s",
637+
explain_region(self.tcx, sub_scope),
638+
explain_region(self.tcx, super_scope)}
639639
}
640640
}
641641
}

src/rustc/util/ppaux.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import driver::session::session;
2727
fn explain_region(cx: ctxt, region: ty::region) -> ~str {
2828
return match region {
2929
re_scope(node_id) => {
30-
let scope_str = match cx.items.find(node_id) {
30+
match cx.items.find(node_id) {
3131
some(ast_map::node_block(blk)) => {
3232
explain_span(cx, ~"block", blk.span)
3333
}
@@ -42,36 +42,36 @@ fn explain_region(cx: ctxt, region: ty::region) -> ~str {
4242
// this really should not happen
4343
fmt!{"unknown scope: %d. Please report a bug.", node_id}
4444
}
45-
};
46-
fmt!{"reference valid for the %s", scope_str}
45+
}
4746
}
4847

4948
re_free(id, br) => {
5049
match cx.items.find(id) {
5150
some(ast_map::node_block(blk)) => {
52-
fmt!{"reference with lifetime %s as defined on %s",
51+
fmt!{"the lifetime %s as defined on %s",
5352
bound_region_to_str(cx, br),
5453
explain_span(cx, ~"the block", blk.span)}
5554
}
5655
some(_) | none => {
5756
// this really should not happen
58-
fmt!{"reference with lifetime %s as defined on node %d",
57+
fmt!{"the lifetime %s as defined on node %d",
5958
bound_region_to_str(cx, br), id}
6059
}
6160
}
6261
}
6362
64-
re_static => { ~"reference to static data" }
63+
re_static => { ~"the static lifetime" }
6564
66-
// I believe these cases should not occur.
65+
// I believe these cases should not occur (except when debugging,
66+
// perhaps)
6767
re_var(_) | re_bound(_) => {
68-
fmt!{"reference with lifetime %?", region}
68+
fmt!{"lifetime %?", region}
6969
}
7070
};
7171
7272
fn explain_span(cx: ctxt, heading: ~str, span: span) -> ~str {
7373
let lo = codemap::lookup_char_pos_adj(cx.sess.codemap, span.lo);
74-
fmt!{"%s at %u:%u", heading, lo.line, lo.col}
74+
fmt!{"the %s at %u:%u", heading, lo.line, lo.col}
7575
}
7676
}
7777
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Here we are checking that a reasonable error msg is provided.
2+
//
3+
// The current message is not ideal, but we used to say "borrowed
4+
// pointer has lifetime &, but the borrowed value only has lifetime &"
5+
// which is definitely no good.
6+
7+
8+
fn get() -> &int {
9+
let x = 3;
10+
return &x;
11+
//~^ ERROR illegal borrow: borrowed pointer must be valid for the lifetime & as defined on the the block at 8:17, but the borrowed value is only valid for the block at 8:17
12+
}
13+
14+
fn main() {}

src/test/compile-fail/regions-infer-borrow-scope-within-loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn foo(cond: fn() -> bool, box: fn() -> @int) {
77

88
// Here we complain because the resulting region
99
// of this borrow is the fn body as a whole.
10-
y = borrow(x); //~ ERROR managed value would have to be rooted for lifetime
10+
y = borrow(x); //~ ERROR illegal borrow: managed value would have to be rooted
1111

1212
assert *x == *y;
1313
if cond() { break; }

0 commit comments

Comments
 (0)