Skip to content

Commit e714859

Browse files
committed
auto merge of #13330 : huonw/rust/loop-error, r=alexcrichton
rustc: move the check_loop pass earlier. This pass is purely AST based, and by running it earlier we emit more useful error messages, e.g. type inference fails in the case of `let r = break;` with few constraints on `r`, but it's more useful to be told that the `break` is outside the loop (rather than a type error) when it is. Closes #13292.
2 parents 3bfa050 + 3766453 commit e714859

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/librustc/driver/driver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ pub fn phase_3_run_analysis_passes(sess: Session,
323323
let region_map = time(time_passes, "region resolution", (), |_|
324324
middle::region::resolve_crate(&sess, krate));
325325

326+
time(time_passes, "loop checking", (), |_|
327+
middle::check_loop::check_crate(&sess, krate));
328+
326329
let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map,
327330
freevars, region_map, lang_items);
328331

@@ -348,9 +351,6 @@ pub fn phase_3_run_analysis_passes(sess: Session,
348351
time(time_passes, "effect checking", (), |_|
349352
middle::effect::check_crate(&ty_cx, method_map, krate));
350353

351-
time(time_passes, "loop checking", (), |_|
352-
middle::check_loop::check_crate(&ty_cx, krate));
353-
354354
let middle::moves::MoveMaps {moves_map, moved_variables_set,
355355
capture_map} =
356356
time(time_passes, "compute moves", (), |_|

src/librustc/middle/check_loop.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use middle::ty;
11+
use driver::session::Session;
1212

1313
use syntax::ast;
1414
use syntax::codemap::Span;
@@ -21,11 +21,11 @@ enum Context {
2121
}
2222

2323
struct CheckLoopVisitor<'a> {
24-
tcx: &'a ty::ctxt,
24+
sess: &'a Session,
2525
}
2626

27-
pub fn check_crate(tcx: &ty::ctxt, krate: &ast::Crate) {
28-
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, krate, Normal)
27+
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
28+
visit::walk_crate(&mut CheckLoopVisitor { sess: sess }, krate, Normal)
2929
}
3030

3131
impl<'a> Visitor<Context> for CheckLoopVisitor<'a> {
@@ -57,12 +57,10 @@ impl<'a> CheckLoopVisitor<'a> {
5757
match cx {
5858
Loop => {}
5959
Closure => {
60-
self.tcx.sess.span_err(span, format!("`{}` inside of a closure",
61-
name));
60+
self.sess.span_err(span, format!("`{}` inside of a closure", name));
6261
}
6362
Normal => {
64-
self.tcx.sess.span_err(span, format!("`{}` outside of loop",
65-
name));
63+
self.sess.span_err(span, format!("`{}` outside of loop", name));
6664
}
6765
}
6866
}

src/test/compile-fail/break-outside-loop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ fn main() {
3030
}
3131

3232
let rs: Foo = Foo{t: pth};
33+
34+
let unconstrained = break; //~ ERROR: `break` outside of loop
3335
}

0 commit comments

Comments
 (0)