Skip to content

Commit 3a87278

Browse files
Move more uses of panictry! out of libsyntax
[breaking-change] for syntax extensions
1 parent 0bf6394 commit 3a87278

File tree

12 files changed

+107
-119
lines changed

12 files changed

+107
-119
lines changed

src/librustc_driver/driver.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ use std::fs;
4848
use std::io::{self, Write};
4949
use std::path::{Path, PathBuf};
5050
use syntax::ast::{self, NodeIdAssigner};
51-
use syntax::attr;
52-
use syntax::attr::AttrMetaMethods;
51+
use syntax::attr::{self, AttrMetaMethods};
5352
use syntax::diagnostics;
5453
use syntax::fold::Folder;
55-
use syntax::parse;
56-
use syntax::parse::token;
54+
use syntax::parse::{self, PResult, token};
5755
use syntax::util::node_count::NodeCounter;
5856
use syntax::visit;
5957
use syntax;
@@ -86,7 +84,7 @@ pub fn compile_input(sess: &Session,
8684
// possible to keep the peak memory usage low
8785
let (outputs, trans) = {
8886
let (outputs, expanded_crate, id) = {
89-
let krate = phase_1_parse_input(sess, cfg, input);
87+
let krate = panictry!(phase_1_parse_input(sess, cfg, input));
9088

9189
controller_entry_point!(after_parse,
9290
sess,
@@ -415,17 +413,20 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
415413
}
416414
}
417415

418-
pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input) -> ast::Crate {
416+
pub fn phase_1_parse_input<'a>(sess: &'a Session,
417+
cfg: ast::CrateConfig,
418+
input: &Input)
419+
-> PResult<'a, ast::Crate> {
419420
// These may be left in an incoherent state after a previous compile.
420421
// `clear_tables` and `get_ident_interner().clear()` can be used to free
421422
// memory, but they do not restore the initial state.
422423
syntax::ext::mtwt::reset_tables();
423424
token::reset_ident_interner();
424425

425-
let krate = time(sess.time_passes(), "parsing", || {
426+
let krate = try!(time(sess.time_passes(), "parsing", || {
426427
match *input {
427428
Input::File(ref file) => {
428-
parse::parse_crate_from_file(&(*file), cfg.clone(), &sess.parse_sess)
429+
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
429430
}
430431
Input::Str(ref src) => {
431432
parse::parse_crate_from_source_str(anon_src().to_string(),
@@ -434,7 +435,7 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
434435
&sess.parse_sess)
435436
}
436437
}
437-
});
438+
}));
438439

439440
if sess.opts.debugging_opts.ast_json_noexpand {
440441
println!("{}", json::as_json(&krate));
@@ -449,7 +450,7 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
449450
syntax::show_span::run(sess.diagnostic(), s, &krate);
450451
}
451452

452-
krate
453+
Ok(krate)
453454
}
454455

455456
fn count_nodes(krate: &ast::Crate) -> usize {

src/librustc_driver/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ use std::thread;
8888
use rustc::session::early_error;
8989

9090
use syntax::ast;
91-
use syntax::parse;
91+
use syntax::parse::{self, PResult};
9292
use syntax::errors;
9393
use syntax::errors::emitter::Emitter;
9494
use syntax::diagnostics;
@@ -529,7 +529,7 @@ impl RustcDefaultCalls {
529529
return Compilation::Continue;
530530
}
531531

532-
let attrs = input.map(|input| parse_crate_attrs(sess, input));
532+
let attrs = input.map(|input| panictry!(parse_crate_attrs(sess, input)));
533533
for req in &sess.opts.prints {
534534
match *req {
535535
PrintRequest::TargetList => {
@@ -920,8 +920,8 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
920920
Some(matches)
921921
}
922922

923-
fn parse_crate_attrs(sess: &Session, input: &Input) -> Vec<ast::Attribute> {
924-
let result = match *input {
923+
fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<ast::Attribute>> {
924+
match *input {
925925
Input::File(ref ifile) => {
926926
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
927927
}
@@ -931,8 +931,7 @@ fn parse_crate_attrs(sess: &Session, input: &Input) -> Vec<ast::Attribute> {
931931
Vec::new(),
932932
&sess.parse_sess)
933933
}
934-
};
935-
result.into_iter().collect()
934+
}
936935
}
937936

938937
/// Run a procedure which will detect panics in the compiler and print nicer

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ pub fn pretty_print_input(sess: Session,
686686
ppm: PpMode,
687687
opt_uii: Option<UserIdentifiedItem>,
688688
ofile: Option<PathBuf>) {
689-
let krate = driver::phase_1_parse_input(&sess, cfg, input);
689+
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, input));
690690

691691
let krate = if let PpmSource(PpmEveryBodyLoops) = ppm {
692692
let mut fold = ReplaceBodyWithLoop::new();

src/librustc_driver/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn test_env<F>(source_string: &str,
114114
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
115115
let krate_config = Vec::new();
116116
let input = config::Input::Str(source_string.to_string());
117-
let krate = driver::phase_1_parse_input(&sess, krate_config, &input);
117+
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
118118
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
119119
.expect("phase 2 aborted");
120120

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
133133
let mut cfg = config::build_configuration(&sess);
134134
target_features::add_configuration(&mut cfg, &sess);
135135

136-
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
136+
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
137137

138138
let name = link::find_crate_name(Some(&sess), &krate.attrs,
139139
&input);

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extern crate rustc_back;
4343
extern crate rustc_front;
4444
extern crate rustc_metadata;
4545
extern crate serialize;
46-
extern crate syntax;
46+
#[macro_use] extern crate syntax;
4747
extern crate test as testing;
4848
extern crate rustc_unicode;
4949
#[macro_use] extern crate log;

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn run(input: &str,
9191

9292
let mut cfg = config::build_configuration(&sess);
9393
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
94-
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
94+
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
9595
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate,
9696
"rustdoc-test", None)
9797
.expect("phase_2_configure_and_expand aborted in rustdoc!");

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ pub struct WhereEqPredicate {
458458

459459
/// The set of MetaItems that define the compilation environment of the crate,
460460
/// used to drive conditional compilation
461-
pub type CrateConfig = Vec<P<MetaItem>> ;
461+
pub type CrateConfig = Vec<P<MetaItem>>;
462462

463463
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
464464
pub struct Crate {

src/libsyntax/ext/expand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ mod tests {
14761476
let crate_ast = parse::parse_crate_from_source_str(
14771477
"<test>".to_string(),
14781478
src,
1479-
Vec::new(), &sess);
1479+
Vec::new(), &sess).unwrap();
14801480
// should fail:
14811481
let mut gated_cfgs = vec![];
14821482
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
@@ -1492,7 +1492,7 @@ mod tests {
14921492
let crate_ast = parse::parse_crate_from_source_str(
14931493
"<test>".to_string(),
14941494
src,
1495-
Vec::new(), &sess);
1495+
Vec::new(), &sess).unwrap();
14961496
let mut gated_cfgs = vec![];
14971497
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
14981498
expand_crate(ecx, vec![], vec![], crate_ast);
@@ -1506,7 +1506,7 @@ mod tests {
15061506
let crate_ast = parse::parse_crate_from_source_str(
15071507
"<test>".to_string(),
15081508
src,
1509-
Vec::new(), &sess);
1509+
Vec::new(), &sess).unwrap();
15101510
let mut gated_cfgs = vec![];
15111511
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
15121512
expand_crate(ecx, vec![], vec![], crate_ast);

src/libsyntax/ext/quote.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ use parse::token::*;
1818
use parse::token;
1919
use ptr::P;
2020

21-
/// Quasiquoting works via token trees.
21+
/// Quasiquoting works via token trees.
2222
///
23-
/// This is registered as a set of expression syntax extension called quote!
24-
/// that lifts its argument token-tree to an AST representing the
25-
/// construction of the same token tree, with token::SubstNt interpreted
26-
/// as antiquotes (splices).
23+
/// This is registered as a set of expression syntax extension called quote!
24+
/// that lifts its argument token-tree to an AST representing the
25+
/// construction of the same token tree, with token::SubstNt interpreted
26+
/// as antiquotes (splices).
2727
2828
pub mod rt {
2929
use ast;
@@ -319,34 +319,36 @@ pub mod rt {
319319
}
320320

321321
impl<'a> ExtParseUtils for ExtCtxt<'a> {
322-
323322
fn parse_item(&self, s: String) -> P<ast::Item> {
324-
parse::parse_item_from_source_str(
323+
panictry!(parse::parse_item_from_source_str(
325324
"<quote expansion>".to_string(),
326325
s,
327326
self.cfg(),
328-
self.parse_sess()).expect("parse error")
327+
self.parse_sess())).expect("parse error")
329328
}
330329

331330
fn parse_stmt(&self, s: String) -> ast::Stmt {
332-
parse::parse_stmt_from_source_str("<quote expansion>".to_string(),
333-
s,
334-
self.cfg(),
335-
self.parse_sess()).expect("parse error")
331+
panictry!(parse::parse_stmt_from_source_str(
332+
"<quote expansion>".to_string(),
333+
s,
334+
self.cfg(),
335+
self.parse_sess())).expect("parse error")
336336
}
337337

338338
fn parse_expr(&self, s: String) -> P<ast::Expr> {
339-
parse::parse_expr_from_source_str("<quote expansion>".to_string(),
340-
s,
341-
self.cfg(),
342-
self.parse_sess())
339+
panictry!(parse::parse_expr_from_source_str(
340+
"<quote expansion>".to_string(),
341+
s,
342+
self.cfg(),
343+
self.parse_sess()))
343344
}
344345

345346
fn parse_tts(&self, s: String) -> Vec<TokenTree> {
346-
parse::parse_tts_from_source_str("<quote expansion>".to_string(),
347-
s,
348-
self.cfg(),
349-
self.parse_sess())
347+
panictry!(parse::parse_tts_from_source_str(
348+
"<quote expansion>".to_string(),
349+
s,
350+
self.cfg(),
351+
self.parse_sess()))
350352
}
351353
}
352354
}

0 commit comments

Comments
 (0)