From 3766453a422198c77df4970159dd3a56f05e1861 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 5 Apr 2014 10:05:31 +1100 Subject: [PATCH] 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 its more useful to be told that the `break` is outside a loop (rather than a type error) when it is. Closes #13292. --- src/librustc/driver/driver.rs | 6 +++--- src/librustc/middle/check_loop.rs | 14 ++++++-------- src/test/compile-fail/break-outside-loop.rs | 2 ++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index af5b3f8b0cd..7cb8a3c4efd 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -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); @@ -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", (), |_| diff --git a/src/librustc/middle/check_loop.rs b/src/librustc/middle/check_loop.rs index 4b99ce28505..4f08d818f4a 100644 --- a/src/librustc/middle/check_loop.rs +++ b/src/librustc/middle/check_loop.rs @@ -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; @@ -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 for CheckLoopVisitor<'a> { @@ -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)); } } } diff --git a/src/test/compile-fail/break-outside-loop.rs b/src/test/compile-fail/break-outside-loop.rs index 4633ca66289..210e3d3af80 100644 --- a/src/test/compile-fail/break-outside-loop.rs +++ b/src/test/compile-fail/break-outside-loop.rs @@ -30,4 +30,6 @@ fn main() { } let rs: Foo = Foo{t: pth}; + + let unconstrained = break; //~ ERROR: `break` outside of loop }