Skip to content

Commit b7fbfb6

Browse files
committed
Auto merge of #29285 - eefriedman:libsyntax-panic, r=nrc
A set of commits which pushes some panics out of core parser methods, and into users of those parser methods.
2 parents 749625a + e7d3ae6 commit b7fbfb6

File tree

21 files changed

+174
-190
lines changed

21 files changed

+174
-190
lines changed

src/librustc/session/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,15 +657,15 @@ pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config {
657657
let target = match Target::search(&opts.target_triple) {
658658
Ok(t) => t,
659659
Err(e) => {
660-
sp.handler().fatal(&format!("Error loading target specification: {}", e));
660+
panic!(sp.handler().fatal(&format!("Error loading target specification: {}", e)));
661661
}
662662
};
663663

664664
let (int_type, uint_type) = match &target.target_pointer_width[..] {
665665
"32" => (ast::TyI32, ast::TyU32),
666666
"64" => (ast::TyI64, ast::TyU64),
667-
w => sp.handler().fatal(&format!("target specification was invalid: unrecognized \
668-
target-pointer-width {}", w))
667+
w => panic!(sp.handler().fatal(&format!("target specification was invalid: \
668+
unrecognized target-pointer-width {}", w))),
669669
};
670670

671671
Config {

src/librustc/session/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Session {
9494
if self.opts.treat_err_as_bug {
9595
self.bug(msg);
9696
}
97-
self.diagnostic().handler().fatal(msg)
97+
panic!(self.diagnostic().handler().fatal(msg))
9898
}
9999
pub fn span_err_or_warn(&self, is_warning: bool, sp: Span, msg: &str) {
100100
if is_warning {
@@ -415,8 +415,8 @@ pub fn build_session_(sopts: config::Options,
415415
let host = match Target::search(config::host_triple()) {
416416
Ok(t) => t,
417417
Err(e) => {
418-
span_diagnostic.handler()
419-
.fatal(&format!("Error loading host specification: {}", e));
418+
panic!(span_diagnostic.handler()
419+
.fatal(&format!("Error loading host specification: {}", e)));
420420
}
421421
};
422422
let target_cfg = config::build_target_config(&sopts, &span_diagnostic);

src/librustc_back/target/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ impl Target {
268268
.map(|s| s.as_string())
269269
.and_then(|os| os.map(|s| s.to_string())) {
270270
Some(val) => val,
271-
None =>
272-
handler.fatal(&format!("Field {} in target specification is required", name))
271+
None => {
272+
panic!(handler.fatal(&format!("Field {} in target specification is required",
273+
name)))
274+
}
273275
}
274276
};
275277

src/librustc_trans/back/write.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ pub fn llvm_err(handler: &diagnostic::Handler, msg: String) -> ! {
3838
unsafe {
3939
let cstr = llvm::LLVMRustGetLastError();
4040
if cstr == ptr::null() {
41-
handler.fatal(&msg[..]);
41+
panic!(handler.fatal(&msg[..]));
4242
} else {
4343
let err = CStr::from_ptr(cstr).to_bytes();
4444
let err = String::from_utf8_lossy(err).to_string();
4545
libc::free(cstr as *mut _);
46-
handler.fatal(&format!("{}: {}",
47-
&msg[..],
48-
&err[..]));
46+
panic!(handler.fatal(&format!("{}: {}", &msg[..], &err[..])));
4947
}
5048
}
5149
}

src/libsyntax/diagnostic.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ impl Handler {
206206
can_emit_warnings: can_emit_warnings
207207
}
208208
}
209-
pub fn fatal(&self, msg: &str) -> ! {
209+
pub fn fatal(&self, msg: &str) -> FatalError {
210210
self.emit.borrow_mut().emit(None, msg, None, Fatal);
211-
212-
// Suppress the fatal error message from the panic below as we've
213-
// already terminated in our own "legitimate" fashion.
214-
io::set_panic(Box::new(io::sink()));
215-
panic!(FatalError);
211+
FatalError
216212
}
217213
pub fn err(&self, msg: &str) {
218214
self.emit.borrow_mut().emit(None, msg, None, Error);
@@ -230,14 +226,15 @@ impl Handler {
230226
pub fn abort_if_errors(&self) {
231227
let s;
232228
match self.err_count.get() {
233-
0 => return,
234-
1 => s = "aborting due to previous error".to_string(),
235-
_ => {
236-
s = format!("aborting due to {} previous errors",
237-
self.err_count.get());
238-
}
229+
0 => return,
230+
1 => s = "aborting due to previous error".to_string(),
231+
_ => {
232+
s = format!("aborting due to {} previous errors",
233+
self.err_count.get());
234+
}
239235
}
240-
self.fatal(&s[..]);
236+
237+
panic!(self.fatal(&s[..]));
241238
}
242239
pub fn warn(&self, msg: &str) {
243240
self.emit.borrow_mut().emit(None, msg, None, Warning);

src/libsyntax/ext/asm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
7979
cx.span_err(sp, "malformed inline assembly");
8080
return DummyResult::expr(sp);
8181
}
82-
let (s, style) = match expr_to_string(cx, p.parse_expr(),
82+
let (s, style) = match expr_to_string(cx, panictry!(p.parse_expr_nopanic()),
8383
"inline assembly must be a string literal") {
8484
Some((s, st)) => (s, st),
8585
// let compilation continue
@@ -102,7 +102,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
102102
let span = p.last_span;
103103

104104
panictry!(p.expect(&token::OpenDelim(token::Paren)));
105-
let out = p.parse_expr();
105+
let out = panictry!(p.parse_expr_nopanic());
106106
panictry!(p.expect(&token::CloseDelim(token::Paren)));
107107

108108
// Expands a read+write operand into two operands.
@@ -146,7 +146,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
146146
}
147147

148148
panictry!(p.expect(&token::OpenDelim(token::Paren)));
149-
let input = p.parse_expr();
149+
let input = panictry!(p.parse_expr_nopanic());
150150
panictry!(p.expect(&token::CloseDelim(token::Paren)));
151151

152152
inputs.push((constraint, input));

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ pub fn get_single_str_from_tts(cx: &mut ExtCtxt,
809809
cx.span_err(sp, &format!("{} takes 1 argument", name));
810810
return None
811811
}
812-
let ret = cx.expander().fold_expr(p.parse_expr());
812+
let ret = cx.expander().fold_expr(panictry!(p.parse_expr_nopanic()));
813813
if p.token != token::Eof {
814814
cx.span_err(sp, &format!("{} takes 1 argument", name));
815815
}
@@ -826,7 +826,7 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
826826
let mut p = cx.new_parser_from_tts(tts);
827827
let mut es = Vec::new();
828828
while p.token != token::Eof {
829-
es.push(cx.expander().fold_expr(p.parse_expr()));
829+
es.push(cx.expander().fold_expr(panictry!(p.parse_expr_nopanic())));
830830
if panictry!(p.eat(&token::Comma)){
831831
continue;
832832
}

src/libsyntax/ext/cfg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ use ext::base;
1919
use ext::build::AstBuilder;
2020
use attr;
2121
use attr::*;
22-
use parse::attr::ParserAttr;
2322
use parse::token;
2423

2524
pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
2625
sp: Span,
2726
tts: &[ast::TokenTree])
2827
-> Box<base::MacResult+'static> {
2928
let mut p = cx.new_parser_from_tts(tts);
30-
let cfg = p.parse_meta_item();
29+
let cfg = panictry!(p.parse_meta_item());
3130

3231
if !panictry!(p.eat(&token::Eof)){
3332
cx.span_err(sp, "expected 1 cfg-pattern");

src/libsyntax/ext/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
9393
ecx.span_err(sp, "requires at least a format string argument");
9494
return None;
9595
}
96-
let fmtstr = p.parse_expr();
96+
let fmtstr = panictry!(p.parse_expr_nopanic());
9797
let mut named = false;
9898
while p.token != token::Eof {
9999
if !panictry!(p.eat(&token::Comma)) {
@@ -124,7 +124,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
124124
let name: &str = &ident.name.as_str();
125125

126126
panictry!(p.expect(&token::Eq));
127-
let e = p.parse_expr();
127+
let e = panictry!(p.parse_expr_nopanic());
128128
match names.get(name) {
129129
None => {}
130130
Some(prev) => {
@@ -138,7 +138,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
138138
order.push(name.to_string());
139139
names.insert(name.to_string(), e);
140140
} else {
141-
args.push(p.parse_expr());
141+
args.push(panictry!(p.parse_expr_nopanic()));
142142
}
143143
}
144144
Some((fmtstr, args, order, names))

src/libsyntax/ext/quote.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,55 +327,55 @@ pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
327327
sp: Span,
328328
tts: &[ast::TokenTree])
329329
-> Box<base::MacResult+'cx> {
330-
let expanded = expand_parse_call(cx, sp, "parse_expr", vec!(), tts);
330+
let expanded = expand_parse_call(cx, sp, "parse_expr_panic", vec!(), tts);
331331
base::MacEager::expr(expanded)
332332
}
333333

334334
pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
335335
sp: Span,
336336
tts: &[ast::TokenTree])
337337
-> Box<base::MacResult+'cx> {
338-
let expanded = expand_parse_call(cx, sp, "parse_item", vec!(), tts);
338+
let expanded = expand_parse_call(cx, sp, "parse_item_panic", vec!(), tts);
339339
base::MacEager::expr(expanded)
340340
}
341341

342342
pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
343343
sp: Span,
344344
tts: &[ast::TokenTree])
345345
-> Box<base::MacResult+'cx> {
346-
let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
346+
let expanded = expand_parse_call(cx, sp, "parse_pat_panic", vec!(), tts);
347347
base::MacEager::expr(expanded)
348348
}
349349

350350
pub fn expand_quote_arm(cx: &mut ExtCtxt,
351351
sp: Span,
352352
tts: &[ast::TokenTree])
353353
-> Box<base::MacResult+'static> {
354-
let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
354+
let expanded = expand_parse_call(cx, sp, "parse_arm_panic", vec!(), tts);
355355
base::MacEager::expr(expanded)
356356
}
357357

358358
pub fn expand_quote_ty(cx: &mut ExtCtxt,
359359
sp: Span,
360360
tts: &[ast::TokenTree])
361361
-> Box<base::MacResult+'static> {
362-
let expanded = expand_parse_call(cx, sp, "parse_ty", vec!(), tts);
362+
let expanded = expand_parse_call(cx, sp, "parse_ty_panic", vec!(), tts);
363363
base::MacEager::expr(expanded)
364364
}
365365

366366
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
367367
sp: Span,
368368
tts: &[ast::TokenTree])
369369
-> Box<base::MacResult+'static> {
370-
let expanded = expand_parse_call(cx, sp, "parse_stmt", vec!(), tts);
370+
let expanded = expand_parse_call(cx, sp, "parse_stmt_panic", vec!(), tts);
371371
base::MacEager::expr(expanded)
372372
}
373373

374374
pub fn expand_quote_attr(cx: &mut ExtCtxt,
375375
sp: Span,
376376
tts: &[ast::TokenTree])
377377
-> Box<base::MacResult+'static> {
378-
let expanded = expand_parse_call(cx, sp, "parse_attribute",
378+
let expanded = expand_parse_call(cx, sp, "parse_attribute_panic",
379379
vec!(cx.expr_bool(sp, true)), tts);
380380

381381
base::MacEager::expr(expanded)
@@ -694,7 +694,7 @@ fn parse_arguments_to_quote(cx: &ExtCtxt, tts: &[ast::TokenTree])
694694
let mut p = cx.new_parser_from_tts(tts);
695695
p.quote_depth += 1;
696696

697-
let cx_expr = p.parse_expr();
697+
let cx_expr = panictry!(p.parse_expr_nopanic());
698698
if !panictry!(p.eat(&token::Comma)) {
699699
panic!(p.fatal("expected token `,`"));
700700
}

0 commit comments

Comments
 (0)