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

+3-3
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

+3-3
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

+4-2
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

+2-4
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

+10-13
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

+3-3
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

+2-2
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

+1-2
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

+3-3
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

+8-8
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
}

src/libsyntax/ext/source_util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree
109109
}
110110
impl<'a> base::MacResult for ExpandResult<'a> {
111111
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
112-
Some(self.p.parse_expr())
112+
Some(panictry!(self.p.parse_expr_nopanic()))
113113
}
114114
fn make_items(mut self: Box<ExpandResult<'a>>)
115115
-> Option<SmallVector<P<ast::Item>>> {
116116
let mut ret = SmallVector::zero();
117117
while self.p.token != token::Eof {
118-
match self.p.parse_item() {
118+
match panictry!(self.p.parse_item_nopanic()) {
119119
Some(item) => ret.push(item),
120120
None => panic!(self.p.span_fatal(
121121
self.p.span,

src/libsyntax/ext/tt/macro_parser.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ use codemap::{BytePos, mk_sp, Span};
8585
use codemap;
8686
use parse::lexer::*; //resolve bug?
8787
use parse::ParseSess;
88-
use parse::attr::ParserAttr;
8988
use parse::parser::{LifetimeAndTypesWithoutColons, Parser};
9089
use parse::token::{Eof, DocComment, MatchNt, SubstNt};
9190
use parse::token::{Token, Nonterminal};
@@ -503,18 +502,18 @@ pub fn parse_nt(p: &mut Parser, sp: Span, name: &str) -> Nonterminal {
503502
// check at the beginning and the parser checks after each bump
504503
panictry!(p.check_unknown_macro_variable());
505504
match name {
506-
"item" => match p.parse_item() {
505+
"item" => match panictry!(p.parse_item_nopanic()) {
507506
Some(i) => token::NtItem(i),
508507
None => panic!(p.fatal("expected an item keyword"))
509508
},
510509
"block" => token::NtBlock(panictry!(p.parse_block())),
511-
"stmt" => match p.parse_stmt() {
510+
"stmt" => match panictry!(p.parse_stmt_nopanic()) {
512511
Some(s) => token::NtStmt(s),
513512
None => panic!(p.fatal("expected a statement"))
514513
},
515-
"pat" => token::NtPat(p.parse_pat()),
516-
"expr" => token::NtExpr(p.parse_expr()),
517-
"ty" => token::NtTy(p.parse_ty()),
514+
"pat" => token::NtPat(panictry!(p.parse_pat_nopanic())),
515+
"expr" => token::NtExpr(panictry!(p.parse_expr_nopanic())),
516+
"ty" => token::NtTy(panictry!(p.parse_ty_nopanic())),
518517
// this could be handled like a token, since it is one
519518
"ident" => match p.token {
520519
token::Ident(sn,b) => { panictry!(p.bump()); token::NtIdent(Box::new(sn),b) }
@@ -527,7 +526,7 @@ pub fn parse_nt(p: &mut Parser, sp: Span, name: &str) -> Nonterminal {
527526
"path" => {
528527
token::NtPath(Box::new(panictry!(p.parse_path(LifetimeAndTypesWithoutColons))))
529528
},
530-
"meta" => token::NtMeta(p.parse_meta_item()),
529+
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
531530
_ => {
532531
panic!(p.span_fatal_help(sp,
533532
&format!("invalid fragment specifier `{}`", name),

src/libsyntax/ext/tt/macro_rules.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ impl<'a> ParserAnyMacro<'a> {
6666

6767
impl<'a> MacResult for ParserAnyMacro<'a> {
6868
fn make_expr(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Expr>> {
69-
let ret = self.parser.borrow_mut().parse_expr();
69+
let ret = panictry!(self.parser.borrow_mut().parse_expr_nopanic());
7070
self.ensure_complete_parse(true);
7171
Some(ret)
7272
}
7373
fn make_pat(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Pat>> {
74-
let ret = self.parser.borrow_mut().parse_pat();
74+
let ret = panictry!(self.parser.borrow_mut().parse_pat_nopanic());
7575
self.ensure_complete_parse(false);
7676
Some(ret)
7777
}
7878
fn make_items(self: Box<ParserAnyMacro<'a>>) -> Option<SmallVector<P<ast::Item>>> {
7979
let mut ret = SmallVector::zero();
80-
while let Some(item) = self.parser.borrow_mut().parse_item() {
80+
while let Some(item) = panictry!(self.parser.borrow_mut().parse_item_nopanic()) {
8181
ret.push(item);
8282
}
8383
self.ensure_complete_parse(false);
@@ -119,7 +119,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
119119
}
120120

121121
fn make_ty(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Ty>> {
122-
let ret = self.parser.borrow_mut().parse_ty();
122+
let ret = panictry!(self.parser.borrow_mut().parse_ty_nopanic());
123123
self.ensure_complete_parse(true);
124124
Some(ret)
125125
}

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#![feature(filling_drop)]
3131
#![feature(libc)]
3232
#![feature(rustc_private)]
33-
#![feature(set_stdio)]
3433
#![feature(staged_api)]
3534
#![feature(str_char)]
3635
#![feature(str_escape)]

0 commit comments

Comments
 (0)