Skip to content

Commit

Permalink
rustc: move the check_loop pass earlier.
Browse files Browse the repository at this point in the history
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 its more useful to be told that
the `break` is outside a loop (rather than a type error) when it is.

Closes #13292.
  • Loading branch information
huonw committed Apr 4, 2014
1 parent e5f1b9f commit 3766453
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/librustc/driver/driver.rs
Expand Up @@ -323,6 +323,9 @@ pub fn phase_3_run_analysis_passes(sess: Session,
let region_map = time(time_passes, "region resolution", (), |_|
middle::region::resolve_crate(&sess, krate));

time(time_passes, "loop checking", (), |_|
middle::check_loop::check_crate(&sess, krate));

let ty_cx = ty::mk_ctxt(sess, def_map, named_region_map, ast_map,
freevars, region_map, lang_items);

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

time(time_passes, "loop checking", (), |_|
middle::check_loop::check_crate(&ty_cx, krate));

let middle::moves::MoveMaps {moves_map, moved_variables_set,
capture_map} =
time(time_passes, "compute moves", (), |_|
Expand Down
14 changes: 6 additions & 8 deletions src/librustc/middle/check_loop.rs
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use middle::ty;
use driver::session::Session;

use syntax::ast;
use syntax::codemap::Span;
Expand All @@ -21,11 +21,11 @@ enum Context {
}

struct CheckLoopVisitor<'a> {
tcx: &'a ty::ctxt,
sess: &'a Session,
}

pub fn check_crate(tcx: &ty::ctxt, krate: &ast::Crate) {
visit::walk_crate(&mut CheckLoopVisitor { tcx: tcx }, krate, Normal)
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
visit::walk_crate(&mut CheckLoopVisitor { sess: sess }, krate, Normal)
}

impl<'a> Visitor<Context> for CheckLoopVisitor<'a> {
Expand Down Expand Up @@ -57,12 +57,10 @@ impl<'a> CheckLoopVisitor<'a> {
match cx {
Loop => {}
Closure => {
self.tcx.sess.span_err(span, format!("`{}` inside of a closure",
name));
self.sess.span_err(span, format!("`{}` inside of a closure", name));
}
Normal => {
self.tcx.sess.span_err(span, format!("`{}` outside of loop",
name));
self.sess.span_err(span, format!("`{}` outside of loop", name));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/break-outside-loop.rs
Expand Up @@ -30,4 +30,6 @@ fn main() {
}

let rs: Foo = Foo{t: pth};

let unconstrained = break; //~ ERROR: `break` outside of loop
}

5 comments on commit 3766453

@bors
Copy link
Contributor

@bors bors commented on 3766453 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at huonw@3766453

@bors
Copy link
Contributor

@bors bors commented on 3766453 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/loop-error = 3766453 into auto

@bors
Copy link
Contributor

@bors bors commented on 3766453 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/loop-error = 3766453 merged ok, testing candidate = e714859

@bors
Copy link
Contributor

@bors bors commented on 3766453 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 3766453 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e714859

Please sign in to comment.