From 3a872782d396e9ed3827a515a54d1f0a634c0b77 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 13 Feb 2016 18:05:16 +0100 Subject: Move more uses of `panictry!` out of libsyntax [breaking-change] for syntax extensions --- src/libsyntax/parse/mod.rs | 130 ++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 72 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 1ec2479058c..9bcf1079a64 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -71,95 +71,93 @@ impl ParseSess { // uses a HOF to parse anything, and includes file and // source_str. -pub fn parse_crate_from_file( - input: &Path, - cfg: ast::CrateConfig, - sess: &ParseSess -) -> ast::Crate { +pub fn parse_crate_from_file<'a>(input: &Path, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, ast::Crate> { let mut parser = new_parser_from_file(sess, cfg, input); - abort_if_errors(parser.parse_crate_mod(), &parser) + parser.parse_crate_mod() } -pub fn parse_crate_attrs_from_file( - input: &Path, - cfg: ast::CrateConfig, - sess: &ParseSess -) -> Vec { +pub fn parse_crate_attrs_from_file<'a>(input: &Path, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, Vec> { let mut parser = new_parser_from_file(sess, cfg, input); - abort_if_errors(parser.parse_inner_attributes(), &parser) + parser.parse_inner_attributes() } -pub fn parse_crate_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> ast::Crate { +pub fn parse_crate_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, ast::Crate> { let mut p = new_parser_from_source_str(sess, cfg, name, source); - panictry!(p.parse_crate_mod()) + p.parse_crate_mod() } -pub fn parse_crate_attrs_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> Vec { +pub fn parse_crate_attrs_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, Vec> { let mut p = new_parser_from_source_str(sess, cfg, name, source); - panictry!(p.parse_inner_attributes()) + p.parse_inner_attributes() } -pub fn parse_expr_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> P { +pub fn parse_expr_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, P> { let mut p = new_parser_from_source_str(sess, cfg, name, source); - panictry!(p.parse_expr()) + p.parse_expr() } -pub fn parse_item_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> Option> { +pub fn parse_item_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, Option>> { let mut p = new_parser_from_source_str(sess, cfg, name, source); - panictry!(p.parse_item()) + p.parse_item() } -pub fn parse_meta_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> P { +pub fn parse_meta_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, P> { let mut p = new_parser_from_source_str(sess, cfg, name, source); - panictry!(p.parse_meta_item()) + p.parse_meta_item() } -pub fn parse_stmt_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> Option { +pub fn parse_stmt_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, Option> { let mut p = new_parser_from_source_str( sess, cfg, name, source ); - panictry!(p.parse_stmt()) + p.parse_stmt() } // Warning: This parses with quote_depth > 0, which is not the default. -pub fn parse_tts_from_source_str(name: String, - source: String, - cfg: ast::CrateConfig, - sess: &ParseSess) - -> Vec { +pub fn parse_tts_from_source_str<'a>(name: String, + source: String, + cfg: ast::CrateConfig, + sess: &'a ParseSess) + -> PResult<'a, Vec> { let mut p = new_parser_from_source_str( sess, cfg, @@ -168,7 +166,7 @@ pub fn parse_tts_from_source_str(name: String, ); p.quote_depth += 1; // right now this is re-creating the token trees from ... token trees. - panictry!(p.parse_all_token_trees()) + p.parse_all_token_trees() } // Create a new parser from a source string @@ -265,20 +263,6 @@ pub fn tts_to_parser<'a>(sess: &'a ParseSess, p } - -fn abort_if_errors<'a, T>(result: PResult<'a, T>, p: &Parser) -> T { - match result { - Ok(c) => { - c - } - Err(mut e) => { - e.emit(); - p.abort_if_errors(); - unreachable!(); - } - } -} - /// Parse a string representing a character literal into its final form. /// Rather than just accepting/rejecting a given literal, unescapes it as /// well. Can take any slice prefixed by a character escape. Returns the @@ -1078,19 +1062,21 @@ mod tests { let name = "".to_string(); let source = "/// doc comment\r\nfn foo() {}".to_string(); - let item = parse_item_from_source_str(name.clone(), source, Vec::new(), &sess).unwrap(); + let item = parse_item_from_source_str(name.clone(), source, Vec::new(), &sess) + .unwrap().unwrap(); let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap(); assert_eq!(&doc[..], "/// doc comment"); let source = "/// doc comment\r\n/// line 2\r\nfn foo() {}".to_string(); - let item = parse_item_from_source_str(name.clone(), source, Vec::new(), &sess).unwrap(); + let item = parse_item_from_source_str(name.clone(), source, Vec::new(), &sess) + .unwrap().unwrap(); let docs = item.attrs.iter().filter(|a| &*a.name() == "doc") .map(|a| a.value_str().unwrap().to_string()).collect::>(); let b: &[_] = &["/// doc comment".to_string(), "/// line 2".to_string()]; assert_eq!(&docs[..], b); let source = "/** doc comment\r\n * with CRLF */\r\nfn foo() {}".to_string(); - let item = parse_item_from_source_str(name, source, Vec::new(), &sess).unwrap(); + let item = parse_item_from_source_str(name, source, Vec::new(), &sess).unwrap().unwrap(); let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap(); assert_eq!(&doc[..], "/** doc comment\n * with CRLF */"); } @@ -1099,7 +1085,7 @@ mod tests { fn ttdelim_span() { let sess = ParseSess::new(); let expr = parse::parse_expr_from_source_str("foo".to_string(), - "foo!( fn main() { body } )".to_string(), vec![], &sess); + "foo!( fn main() { body } )".to_string(), vec![], &sess).unwrap(); let tts = match expr.node { ast::ExprKind::Mac(ref mac) => mac.node.tts.clone(), -- cgit 1.4.1-3-g733a5 From 11e0ba43401b0fcf4d61a4e91ad8d7020da74994 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 19 Feb 2016 14:43:13 +0100 Subject: Do less panicking in general --- src/librustc_driver/driver.rs | 8 +++++++- src/librustc_driver/lib.rs | 14 +++++++++++++- src/libsyntax/parse/mod.rs | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 736633c1614..6ef42984905 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -84,7 +84,13 @@ pub fn compile_input(sess: &Session, // possible to keep the peak memory usage low let (outputs, trans) = { let (outputs, expanded_crate, id) = { - let krate = panictry!(phase_1_parse_input(sess, cfg, input)); + let krate = match phase_1_parse_input(sess, cfg, input) { + Ok(krate) => krate, + Err(mut parse_error) => { + parse_error.emit(); + return Err(1); + } + }; controller_entry_point!(after_parse, sess, diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 05317097d0c..6f28d475aa6 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -529,7 +529,19 @@ impl RustcDefaultCalls { return Compilation::Continue; } - let attrs = input.map(|input| panictry!(parse_crate_attrs(sess, input))); + let attrs = match input { + None => None, + Some(input) => { + let result = parse_crate_attrs(sess, input); + match result { + Ok(attrs) => Some(attrs), + Err(mut parse_error) => { + parse_error.emit(); + return Compilation::Stop; + } + } + } + }; for req in &sess.opts.prints { match *req { PrintRequest::TargetList => { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 9bcf1079a64..ea5d6739e6d 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -120,6 +120,10 @@ pub fn parse_expr_from_source_str<'a>(name: String, p.parse_expr() } +/// Parses an item. +/// +/// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and`Err` +/// when a syntax error occurred. pub fn parse_item_from_source_str<'a>(name: String, source: String, cfg: ast::CrateConfig, -- cgit 1.4.1-3-g733a5