Skip to content

Commit 0ac8915

Browse files
committed
The war on abort_if_errors
1 parent b1b6b33 commit 0ac8915

File tree

11 files changed

+73
-74
lines changed

11 files changed

+73
-74
lines changed

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
270270
let say = |s: &str| {
271271
match (sp, sess) {
272272
(_, None) => panic!("{}", s),
273-
(Some(sp), Some(sess)) => sess.span_err(sp, s),
274-
(None, Some(sess)) => sess.err(s),
273+
(Some(sp), Some(sess)) => sess.span_fatal(sp, s),
274+
(None, Some(sess)) => sess.fatal(s),
275275
}
276276
};
277277
if s.is_empty() {
@@ -282,10 +282,6 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
282282
if c == '_' { continue }
283283
say(&format!("invalid character `{}` in crate name: `{}`", c, s));
284284
}
285-
match sess {
286-
Some(sess) => sess.abort_if_errors(),
287-
None => {}
288-
}
289285
}
290286

291287
/// A dummy crate store that does not support any non-local crates,

src/librustc/middle/lang_items.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ pub fn collect_language_items(session: &Session,
239239
collector.collect(krate);
240240
let LanguageItemCollector { mut items, .. } = collector;
241241
weak_lang_items::check_crate(krate, session, &mut items);
242-
session.abort_if_errors();
243242
items
244243
}
245244

src/librustc/session/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,15 @@ impl Session {
176176
pub fn abort_if_errors(&self) {
177177
self.diagnostic().abort_if_errors();
178178
}
179-
pub fn abort_if_new_errors<F>(&self, mut f: F)
180-
where F: FnMut()
179+
pub fn abort_if_new_errors<F, T>(&self, f: F) -> T
180+
where F: FnOnce() -> T
181181
{
182182
let count = self.err_count();
183-
f();
183+
let result = f();
184184
if self.err_count() > count {
185185
self.abort_if_errors();
186186
}
187+
result
187188
}
188189
pub fn span_warn(&self, sp: Span, msg: &str) {
189190
self.diagnostic().span_warn(sp, msg)

src/librustc_driver/driver.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub fn compile_input(sess: Session,
6969
let state = $make_state;
7070
(control.$point.callback)(state);
7171

72-
$tsess.abort_if_errors();
7372
if control.$point.stop == Compilation::Stop {
7473
return;
7574
}
@@ -481,13 +480,15 @@ pub fn phase_2_configure_and_expand(sess: &Session,
481480
});
482481

483482
time(time_passes, "gated macro checking", || {
484-
let features = syntax::feature_gate::check_crate_macros(sess.codemap(),
485-
&sess.parse_sess.span_diagnostic,
486-
&krate);
487-
488-
// these need to be set "early" so that expansion sees `quote` if enabled.
489-
*sess.features.borrow_mut() = features;
490-
sess.abort_if_errors();
483+
sess.abort_if_new_errors(|| {
484+
let features =
485+
syntax::feature_gate::check_crate_macros(sess.codemap(),
486+
&sess.parse_sess.span_diagnostic,
487+
&krate);
488+
489+
// these need to be set "early" so that expansion sees `quote` if enabled.
490+
*sess.features.borrow_mut() = features;
491+
});
491492
});
492493

493494

@@ -525,7 +526,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
525526
let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
526527
llvm_passes, attributes, .. } = registry;
527528

528-
{
529+
sess.abort_if_new_errors(|| {
529530
let mut ls = sess.lint_store.borrow_mut();
530531
for pass in early_lint_passes {
531532
ls.register_early_pass(Some(sess), true, pass);
@@ -540,17 +541,14 @@ pub fn phase_2_configure_and_expand(sess: &Session,
540541

541542
*sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
542543
*sess.plugin_attributes.borrow_mut() = attributes.clone();
543-
}
544+
});
544545

545546
// Lint plugins are registered; now we can process command line flags.
546547
if sess.opts.describe_lints {
547548
super::describe_lints(&*sess.lint_store.borrow(), true);
548549
return None;
549550
}
550-
sess.lint_store.borrow_mut().process_command_line(sess);
551-
552-
// Abort if there are errors from lint processing or a plugin registrar.
553-
sess.abort_if_errors();
551+
sess.abort_if_new_errors(|| sess.lint_store.borrow_mut().process_command_line(sess));
554552

555553
krate = time(time_passes, "expansion", || {
556554
// Windows dlls do not have rpaths, so they don't know how to find their
@@ -594,13 +592,14 @@ pub fn phase_2_configure_and_expand(sess: &Session,
594592
// much as possible (e.g. help the programmer avoid platform
595593
// specific differences)
596594
time(time_passes, "complete gated feature checking 1", || {
597-
let features = syntax::feature_gate::check_crate(sess.codemap(),
598-
&sess.parse_sess.span_diagnostic,
599-
&krate,
600-
&attributes,
601-
sess.opts.unstable_features);
602-
*sess.features.borrow_mut() = features;
603-
sess.abort_if_errors();
595+
sess.abort_if_new_errors(|| {
596+
let features = syntax::feature_gate::check_crate(sess.codemap(),
597+
&sess.parse_sess.span_diagnostic,
598+
&krate,
599+
&attributes,
600+
sess.opts.unstable_features);
601+
*sess.features.borrow_mut() = features;
602+
});
604603
});
605604

606605
// JBC: make CFG processing part of expansion to avoid this problem:
@@ -639,13 +638,14 @@ pub fn phase_2_configure_and_expand(sess: &Session,
639638
// later, to make sure we've got everything (e.g. configuration
640639
// can insert new attributes via `cfg_attr`)
641640
time(time_passes, "complete gated feature checking 2", || {
642-
let features = syntax::feature_gate::check_crate(sess.codemap(),
643-
&sess.parse_sess.span_diagnostic,
644-
&krate,
645-
&attributes,
646-
sess.opts.unstable_features);
647-
*sess.features.borrow_mut() = features;
648-
sess.abort_if_errors();
641+
sess.abort_if_new_errors(|| {
642+
let features = syntax::feature_gate::check_crate(sess.codemap(),
643+
&sess.parse_sess.span_diagnostic,
644+
&krate,
645+
&attributes,
646+
sess.opts.unstable_features);
647+
*sess.features.borrow_mut() = features;
648+
});
649649
});
650650

651651
time(time_passes,
@@ -711,9 +711,11 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
711711
"external crate/lib resolution",
712712
|| LocalCrateReader::new(sess, cstore, &hir_map).read_crates(krate));
713713

714-
let lang_items = time(time_passes,
715-
"language item collection",
716-
|| middle::lang_items::collect_language_items(&sess, &hir_map));
714+
let lang_items = time(time_passes, "language item collection", || {
715+
sess.abort_if_new_errors(|| {
716+
middle::lang_items::collect_language_items(&sess, &hir_map)
717+
})
718+
});
717719

718720
let resolve::CrateMap {
719721
def_map,

src/librustc_metadata/creader.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,14 @@ impl<'a> CrateReader<'a> {
258258
metadata: &MetadataBlob) {
259259
let crate_rustc_version = decoder::crate_rustc_version(metadata.as_slice());
260260
if crate_rustc_version != Some(rustc_version()) {
261-
span_err!(self.sess, span, E0514,
262-
"the crate `{}` has been compiled with {}, which is \
263-
incompatible with this version of rustc",
264-
name,
265-
crate_rustc_version
266-
.as_ref().map(|s|&**s)
267-
.unwrap_or("an old version of rustc")
261+
span_fatal!(self.sess, span, E0514,
262+
"the crate `{}` has been compiled with {}, which is \
263+
incompatible with this version of rustc",
264+
name,
265+
crate_rustc_version
266+
.as_ref().map(|s|&**s)
267+
.unwrap_or("an old version of rustc")
268268
);
269-
self.sess.abort_if_errors();
270269
}
271270
}
272271

@@ -511,7 +510,6 @@ impl<'a> CrateReader<'a> {
511510
}
512511
};
513512
let span = mk_sp(lo, p.last_span.hi);
514-
p.abort_if_errors();
515513

516514
// Mark the attrs as used
517515
for attr in &attrs {
@@ -554,8 +552,7 @@ impl<'a> CrateReader<'a> {
554552
name,
555553
config::host_triple(),
556554
self.sess.opts.target_triple);
557-
span_err!(self.sess, span, E0456, "{}", &message[..]);
558-
self.sess.abort_if_errors();
555+
span_fatal!(self.sess, span, E0456, "{}", &message[..]);
559556
}
560557

561558
let registrar =

src/librustc_passes/const_fn.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use syntax::visit::{self, Visitor, FnKind};
1818
use syntax::codemap::Span;
1919

2020
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
21-
visit::walk_crate(&mut CheckConstFn{ sess: sess }, krate);
22-
sess.abort_if_errors();
21+
sess.abort_if_new_errors(|| {
22+
visit::walk_crate(&mut CheckConstFn{ sess: sess }, krate);
23+
});
2324
}
2425

2526
struct CheckConstFn<'a> {

src/librustc_resolve/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,10 +4019,8 @@ pub fn create_resolver<'a, 'tcx>(session: &'a Session,
40194019
resolver.callback = callback;
40204020

40214021
build_reduced_graph::build_reduced_graph(&mut resolver, krate);
4022-
session.abort_if_errors();
40234022

40244023
resolve_imports::resolve_imports(&mut resolver);
4025-
session.abort_if_errors();
40264024

40274025
resolver
40284026
}

src/libsyntax/errors/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ impl Handler {
555555
pub enum Level {
556556
Bug,
557557
Fatal,
558+
// An error which while not immediately fatal, should stop the compiler
559+
// progressing beyond the current phase.
560+
PhaseFatal,
558561
Error,
559562
Warning,
560563
Note,
@@ -573,7 +576,7 @@ impl fmt::Display for Level {
573576
impl Level {
574577
fn color(self) -> term::color::Color {
575578
match self {
576-
Bug | Fatal | Error => term::color::BRIGHT_RED,
579+
Bug | Fatal | PhaseFatal | Error => term::color::BRIGHT_RED,
577580
Warning => term::color::BRIGHT_YELLOW,
578581
Note => term::color::BRIGHT_GREEN,
579582
Help => term::color::BRIGHT_CYAN,
@@ -584,7 +587,7 @@ impl Level {
584587
fn to_str(self) -> &'static str {
585588
match self {
586589
Bug => "error: internal compiler error",
587-
Fatal | Error => "error",
590+
Fatal | PhaseFatal | Error => "error",
588591
Warning => "warning",
589592
Note => "note",
590593
Help => "help",

src/libsyntax/ext/expand.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,9 +1304,14 @@ pub fn expand_crate(mut cx: ExtCtxt,
13041304
expander.cx.syntax_env.insert(name, extension);
13051305
}
13061306

1307+
let err_count = cx.parse_sess.span_diagnostic.err_count();
13071308
let mut ret = expander.fold_crate(c);
13081309
ret.exported_macros = expander.cx.exported_macros.clone();
1309-
cx.parse_sess.span_diagnostic.abort_if_errors();
1310+
1311+
if cx.parse_sess.span_diagnostic.err_count() > err_count {
1312+
cx.parse_sess.span_diagnostic.abort_if_errors();
1313+
}
1314+
13101315
ret
13111316
};
13121317
return (ret, cx.syntax_env.names);

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn parse_crate_from_source_str(name: String,
9898
cfg,
9999
name,
100100
source);
101-
maybe_aborted(panictry!(p.parse_crate_mod()),p)
101+
panictry!(p.parse_crate_mod())
102102
}
103103

104104
pub fn parse_crate_attrs_from_source_str(name: String,
@@ -110,7 +110,7 @@ pub fn parse_crate_attrs_from_source_str(name: String,
110110
cfg,
111111
name,
112112
source);
113-
maybe_aborted(panictry!(p.parse_inner_attributes()), p)
113+
panictry!(p.parse_inner_attributes())
114114
}
115115

116116
pub fn parse_expr_from_source_str(name: String,
@@ -119,7 +119,7 @@ pub fn parse_expr_from_source_str(name: String,
119119
sess: &ParseSess)
120120
-> P<ast::Expr> {
121121
let mut p = new_parser_from_source_str(sess, cfg, name, source);
122-
maybe_aborted(panictry!(p.parse_expr()), p)
122+
panictry!(p.parse_expr())
123123
}
124124

125125
pub fn parse_item_from_source_str(name: String,
@@ -128,7 +128,7 @@ pub fn parse_item_from_source_str(name: String,
128128
sess: &ParseSess)
129129
-> Option<P<ast::Item>> {
130130
let mut p = new_parser_from_source_str(sess, cfg, name, source);
131-
maybe_aborted(panictry!(p.parse_item()), p)
131+
panictry!(p.parse_item())
132132
}
133133

134134
pub fn parse_meta_from_source_str(name: String,
@@ -137,7 +137,7 @@ pub fn parse_meta_from_source_str(name: String,
137137
sess: &ParseSess)
138138
-> P<ast::MetaItem> {
139139
let mut p = new_parser_from_source_str(sess, cfg, name, source);
140-
maybe_aborted(panictry!(p.parse_meta_item()), p)
140+
panictry!(p.parse_meta_item())
141141
}
142142

143143
pub fn parse_stmt_from_source_str(name: String,
@@ -151,7 +151,7 @@ pub fn parse_stmt_from_source_str(name: String,
151151
name,
152152
source
153153
);
154-
maybe_aborted(panictry!(p.parse_stmt()), p)
154+
panictry!(p.parse_stmt())
155155
}
156156

157157
// Warning: This parses with quote_depth > 0, which is not the default.
@@ -168,7 +168,7 @@ pub fn parse_tts_from_source_str(name: String,
168168
);
169169
p.quote_depth += 1;
170170
// right now this is re-creating the token trees from ... token trees.
171-
maybe_aborted(panictry!(p.parse_all_token_trees()),p)
171+
panictry!(p.parse_all_token_trees())
172172
}
173173

174174
// Create a new parser from a source string
@@ -265,16 +265,10 @@ pub fn tts_to_parser<'a>(sess: &'a ParseSess,
265265
p
266266
}
267267

268-
/// Abort if necessary
269-
pub fn maybe_aborted<T>(result: T, p: Parser) -> T {
270-
p.abort_if_errors();
271-
result
272-
}
273268

274269
fn abort_if_errors<'a, T>(result: PResult<'a, T>, p: &Parser) -> T {
275270
match result {
276271
Ok(c) => {
277-
p.abort_if_errors();
278272
c
279273
}
280274
Err(mut e) => {

src/libsyntax/parse/parser.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,11 @@ impl<'a> Parser<'a> {
23572357

23582358
// Assuming we have just parsed `.foo` (i.e., a dot and an ident), continue
23592359
// parsing into an expression.
2360-
fn parse_dot_suffix(&mut self, ident: Ident, ident_span: Span, self_value: P<Expr>) -> PResult<'a, P<Expr>> {
2360+
fn parse_dot_suffix(&mut self,
2361+
ident: Ident,
2362+
ident_span: Span,
2363+
self_value: P<Expr>)
2364+
-> PResult<'a, P<Expr>> {
23612365
let (_, tys, bindings) = if self.eat(&token::ModSep) {
23622366
try!(self.expect_lt());
23632367
try!(self.parse_generic_values_after_lt())
@@ -2463,7 +2467,6 @@ impl<'a> Parser<'a> {
24632467

24642468
}
24652469
_ => {
2466-
// TODO special case lifetime
24672470
// FIXME Could factor this out into non_fatal_unexpected or something.
24682471
let actual = self.this_token_to_string();
24692472
self.span_err(self.span, &format!("unexpected token: `{}`", actual));

0 commit comments

Comments
 (0)