Skip to content

Commit

Permalink
Replace some aborts with Results
Browse files Browse the repository at this point in the history
Fixes #31207

by removing abort_if_new_errors
  • Loading branch information
nrc committed Jan 31, 2016
1 parent 9041b93 commit b6e4f18
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 83 deletions.
11 changes: 7 additions & 4 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -93,9 +93,12 @@ type Scope<'a> = &'a ScopeChain<'a>;

static ROOT_SCOPE: ScopeChain<'static> = RootScope;

pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &DefMap) -> NamedRegionMap {
pub fn krate(sess: &Session,
krate: &hir::Crate,
def_map: &DefMap)
-> Result<NamedRegionMap, usize> {
let mut named_region_map = NodeMap();
sess.abort_if_new_errors(|| {
try!(sess.track_errors(|| {
krate.visit_all_items(&mut LifetimeContext {
sess: sess,
named_region_map: &mut named_region_map,
Expand All @@ -104,8 +107,8 @@ pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &DefMap) -> NamedRegio
trait_ref_hack: false,
labels_in_fn: vec![],
});
});
named_region_map
}));
Ok(named_region_map)
}

impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
Expand Down
23 changes: 12 additions & 11 deletions src/librustc/session/mod.rs
Expand Up @@ -188,17 +188,6 @@ impl Session {
Err(count)
}
}
pub fn abort_if_new_errors<F, T>(&self, f: F) -> T
where F: FnOnce() -> T
{
match self.track_errors(f) {
Ok(result) => result,
Err(_) => {
self.abort_if_errors();
unreachable!();
}
}
}
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.diagnostic().span_warn(sp, msg)
}
Expand Down Expand Up @@ -515,3 +504,15 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
};
emitter.emit(None, msg, None, errors::Level::Warning);
}

// Err(0) means compilation was stopped, but no errors were found.
// This would be better as a dedicated enum, but using try! is so convenient.
pub type CompileResult = Result<(), usize>;

pub fn compile_result_from_err_count(err_count: usize) -> CompileResult {
if err_count == 0 {
Ok(())
} else {
Err(err_count)
}
}
26 changes: 14 additions & 12 deletions src/librustc_driver/driver.rs
Expand Up @@ -12,7 +12,7 @@ use rustc::front;
use rustc::front::map as hir_map;
use rustc_mir as mir;
use rustc_mir::mir_map::MirMap;
use rustc::session::Session;
use rustc::session::{Session, CompileResult, compile_result_from_err_count};
use rustc::session::config::{self, Input, OutputFilenames, OutputType};
use rustc::session::search_paths::PathKind;
use rustc::lint;
Expand All @@ -35,7 +35,7 @@ use rustc_plugin as plugin;
use rustc_front::hir;
use rustc_front::lowering::{lower_crate, LoweringContext};
use rustc_passes::{no_asm, loops, consts, const_fn, rvalues, static_recursion};
use super::{Compilation, CompileResult, compile_result_from_err_count};
use super::Compilation;

use serialize::json;

Expand Down Expand Up @@ -659,9 +659,9 @@ pub fn phase_2_configure_and_expand(sess: &Session,
})
}));

time(time_passes,
"const fn bodies and arguments",
|| const_fn::check_crate(sess, &krate));
try!(time(time_passes,
"const fn bodies and arguments",
|| const_fn::check_crate(sess, &krate)));

if sess.opts.debugging_opts.input_stats {
println!("Post-expansion node count: {}", count_nodes(&krate));
Expand Down Expand Up @@ -739,9 +739,11 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
"resolution",
|| resolve::resolve_crate(sess, &hir_map, make_glob_map));

let named_region_map = time(time_passes,
"lifetime resolution",
|| middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));
let named_region_map = try!(time(time_passes,
"lifetime resolution",
|| middle::resolve_lifetime::krate(sess,
krate,
&def_map.borrow())));

time(time_passes,
"looking for entry point",
Expand All @@ -759,9 +761,9 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
"loop checking",
|| loops::check_crate(sess, krate));

time(time_passes,
"static item recursion checking",
|| static_recursion::check_crate(sess, krate, &def_map.borrow(), &hir_map));
try!(time(time_passes,
"static item recursion checking",
|| static_recursion::check_crate(sess, krate, &def_map.borrow(), &hir_map)));

ty::ctxt::create_and_enter(sess,
arenas,
Expand All @@ -774,7 +776,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
stability::Index::new(krate),
|tcx| {
// passes are timed inside typeck
typeck::check_crate(tcx, trait_map);
try!(typeck::check_crate(tcx, trait_map));

time(time_passes,
"const checking",
Expand Down
14 changes: 1 addition & 13 deletions src/librustc_driver/lib.rs
Expand Up @@ -63,7 +63,7 @@ use pretty::{PpMode, UserIdentifiedItem};
use rustc_resolve as resolve;
use rustc_trans::back::link;
use rustc_trans::save;
use rustc::session::{config, Session, build_session};
use rustc::session::{config, Session, build_session, CompileResult};
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
use rustc::middle::cstore::CrateStore;
use rustc::lint::Lint;
Expand Down Expand Up @@ -105,18 +105,6 @@ pub mod target_features;
const BUG_REPORT_URL: &'static str = "https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.\
md#bug-reports";

// Err(0) means compilation was stopped, but no errors were found.
// This would be better as a dedicated enum, but using try! is so convenient.
pub type CompileResult = Result<(), usize>;

pub fn compile_result_from_err_count(err_count: usize) -> CompileResult {
if err_count == 0 {
Ok(())
} else {
Err(err_count)
}
}

#[inline]
fn abort_msg(err_count: usize) -> String {
match err_count {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_passes/const_fn.rs
Expand Up @@ -11,16 +11,16 @@
//! Verifies that const fn arguments are immutable by value bindings
//! and the const fn body doesn't contain any statements

use rustc::session::Session;
use rustc::session::{Session, CompileResult};

use syntax::ast;
use syntax::visit::{self, Visitor, FnKind};
use syntax::codemap::Span;

pub fn check_crate(sess: &Session, krate: &ast::Crate) {
sess.abort_if_new_errors(|| {
pub fn check_crate(sess: &Session, krate: &ast::Crate) -> CompileResult {
sess.track_errors(|| {
visit::walk_crate(&mut CheckConstFn{ sess: sess }, krate);
});
})
}

struct CheckConstFn<'a> {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_passes/static_recursion.rs
Expand Up @@ -12,7 +12,7 @@
// recursively.

use rustc::front::map as ast_map;
use rustc::session::Session;
use rustc::session::{Session, CompileResult};
use rustc::middle::def::{Def, DefMap};
use rustc::util::nodemap::NodeMap;

Expand Down Expand Up @@ -92,16 +92,16 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> {
pub fn check_crate<'ast>(sess: &Session,
krate: &'ast hir::Crate,
def_map: &DefMap,
ast_map: &ast_map::Map<'ast>) {
ast_map: &ast_map::Map<'ast>) -> CompileResult {
let mut visitor = CheckCrateVisitor {
sess: sess,
def_map: def_map,
ast_map: ast_map,
discriminant_map: RefCell::new(NodeMap()),
};
sess.abort_if_new_errors(|| {
sess.track_errors(|| {
krate.visit_all_items(&mut visitor);
});
})
}

struct CheckItemRecursionVisitor<'a, 'ast: 'a> {
Expand Down
26 changes: 13 additions & 13 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -105,7 +105,7 @@ use middle::ty::fold::{TypeFolder, TypeFoldable};
use middle::ty::util::Representability;
use require_c_abi_if_variadic;
use rscope::{ElisionFailureInfo, RegionScope};
use session::Session;
use session::{Session, CompileResult};
use {CrateCtxt, lookup_full_def};
use TypeAndSubsts;
use lint;
Expand Down Expand Up @@ -383,29 +383,29 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckItemBodiesVisitor<'a, 'tcx> {
}
}

pub fn check_wf_new(ccx: &CrateCtxt) {
ccx.tcx.sess.abort_if_new_errors(|| {
pub fn check_wf_new(ccx: &CrateCtxt) -> CompileResult {
ccx.tcx.sess.track_errors(|| {
let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(ccx);
ccx.tcx.visit_all_items_in_krate(DepNode::WfCheck, &mut visit);
});
})
}

pub fn check_item_types(ccx: &CrateCtxt) {
ccx.tcx.sess.abort_if_new_errors(|| {
pub fn check_item_types(ccx: &CrateCtxt) -> CompileResult {
ccx.tcx.sess.track_errors(|| {
let mut visit = CheckItemTypesVisitor { ccx: ccx };
ccx.tcx.visit_all_items_in_krate(DepNode::TypeckItemType, &mut visit);
});
})
}

pub fn check_item_bodies(ccx: &CrateCtxt) {
ccx.tcx.sess.abort_if_new_errors(|| {
pub fn check_item_bodies(ccx: &CrateCtxt) -> CompileResult {
ccx.tcx.sess.track_errors(|| {
let mut visit = CheckItemBodiesVisitor { ccx: ccx };
ccx.tcx.visit_all_items_in_krate(DepNode::TypeckItemBody, &mut visit);
});
})
}

pub fn check_drop_impls(ccx: &CrateCtxt) {
ccx.tcx.sess.abort_if_new_errors(|| {
pub fn check_drop_impls(ccx: &CrateCtxt) -> CompileResult {
ccx.tcx.sess.track_errors(|| {
let _task = ccx.tcx.dep_graph.in_task(DepNode::Dropck);
let drop_trait = match ccx.tcx.lang_items.drop_trait() {
Some(id) => ccx.tcx.lookup_trait_def(id), None => { return }
Expand All @@ -421,7 +421,7 @@ pub fn check_drop_impls(ccx: &CrateCtxt) {
}
}
});
});
})
}

fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
Expand Down
36 changes: 21 additions & 15 deletions src/librustc_typeck/lib.rs
Expand Up @@ -105,7 +105,7 @@ use middle::def::Def;
use middle::infer::{self, TypeOrigin};
use middle::subst;
use middle::ty::{self, Ty, TypeFoldable};
use session::config;
use session::{config, CompileResult};
use util::common::time;
use rustc_front::hir;

Expand Down Expand Up @@ -323,7 +323,7 @@ fn check_for_entry_fn(ccx: &CrateCtxt) {
}
}

pub fn check_crate(tcx: &ty::ctxt, trait_map: ty::TraitMap) {
pub fn check_crate(tcx: &ty::ctxt, trait_map: ty::TraitMap) -> CompileResult {
let time_passes = tcx.sess.time_passes();
let ccx = CrateCtxt {
trait_map: trait_map,
Expand All @@ -333,34 +333,40 @@ pub fn check_crate(tcx: &ty::ctxt, trait_map: ty::TraitMap) {

// this ensures that later parts of type checking can assume that items
// have valid types and not error
tcx.sess.abort_if_new_errors(|| {
try!(tcx.sess.track_errors(|| {
time(time_passes, "type collecting", ||
collect::collect_item_types(tcx));

});
}));

time(time_passes, "variance inference", ||
variance::infer_variance(tcx));

tcx.sess.abort_if_new_errors(|| {
try!(tcx.sess.track_errors(|| {
time(time_passes, "coherence checking", ||
coherence::check_coherence(&ccx));
});
}));

time(time_passes, "wf checking", ||
check::check_wf_new(&ccx));
try!(time(time_passes, "wf checking", ||
check::check_wf_new(&ccx)));

time(time_passes, "item-types checking", ||
check::check_item_types(&ccx));
try!(time(time_passes, "item-types checking", ||
check::check_item_types(&ccx)));

time(time_passes, "item-bodies checking", ||
check::check_item_bodies(&ccx));
try!(time(time_passes, "item-bodies checking", ||
check::check_item_bodies(&ccx)));

time(time_passes, "drop-impl checking", ||
check::check_drop_impls(&ccx));
try!(time(time_passes, "drop-impl checking", ||
check::check_drop_impls(&ccx)));

check_for_entry_fn(&ccx);
tcx.sess.abort_if_errors();

let err_count = tcx.sess.err_count();
if err_count == 0 {
Ok(())
} else {
Err(err_count)
}
}

__build_diagnostic_array! { librustc_typeck, DIAGNOSTICS }
10 changes: 3 additions & 7 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -2483,8 +2483,7 @@ impl<'a> Parser<'a> {
float.trunc() as usize,
format!(".{}", fstr.splitn(2, ".").last().unwrap())));
}
err.emit();
self.abort_if_errors();
return Err(err);

}
_ => {
Expand Down Expand Up @@ -4117,9 +4116,7 @@ impl<'a> Parser<'a> {
or did you mean the comma-separated arguments \
'a, Type?");
err.span_note(mk_sp(span_lo, span_hi), &msg);
err.emit();

self.abort_if_errors()
return Err(err);
}

// First parse types.
Expand Down Expand Up @@ -5189,8 +5186,7 @@ impl<'a> Parser<'a> {
of possibly redeclaring it",
paths.name));
}
err.emit();
self.abort_if_errors();
return Err(err);
}

match paths.result {
Expand Down

0 comments on commit b6e4f18

Please sign in to comment.