Skip to content

Commit

Permalink
Auto merge of #31250 - nrc:more-aborts, r=@nikomatsakis
Browse files Browse the repository at this point in the history
With this PR we can save-analysis on code with errors, essential foundation work for IDE support.
  • Loading branch information
bors committed Jan 31, 2016
2 parents 9041b93 + c0ac539 commit 2a39e73
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 166 deletions.
11 changes: 7 additions & 4 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
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
10 changes: 10 additions & 0 deletions src/librustc/middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,16 @@ impl<'tcx> ctxt<'tcx> {
})
}

pub fn expr_ty_adjusted_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
self.expr_ty_opt(expr).map(|t| t.adjust(self,
expr.span,
expr.id,
self.tables.borrow().adjustments.get(&expr.id),
|method_call| {
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
}))
}

pub fn expr_span(&self, id: NodeId) -> Span {
match self.map.find(id) {
Some(ast_map::NodeExpr(e)) => {
Expand Down
31 changes: 16 additions & 15 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,13 @@ impl Session {
pub fn track_errors<F, T>(&self, f: F) -> Result<T, usize>
where F: FnOnce() -> T
{
let count = self.err_count();
let old_count = self.err_count();
let result = f();
let count = self.err_count() - count;
if count == 0 {
let errors = self.err_count() - old_count;
if errors == 0 {
Ok(result)
} else {
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!();
}
Err(errors)
}
}
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
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)
}
}
Loading

0 comments on commit 2a39e73

Please sign in to comment.