Skip to content

Commit

Permalink
Make top-level rustc_parse functions fallible.
Browse files Browse the repository at this point in the history
Currently we have an awkward mix of fallible and infallible functions:
```
       new_parser_from_source_str
 maybe_new_parser_from_source_str
       new_parser_from_file
(maybe_new_parser_from_file)        // missing
      (new_parser_from_source_file) // missing
 maybe_new_parser_from_source_file
       source_str_to_stream
 maybe_source_file_to_stream
```
We could add the two missing functions, but instead this commit removes
of all the infallible ones and renames the fallible ones leaving us with
these which are all fallible:
```
new_parser_from_source_str
new_parser_from_file
new_parser_from_source_file
source_str_to_stream
source_file_to_stream
```
This requires making `unwrap_or_emit_fatal` public so callers of
formerly infallible functions can still work.

This does make some of the call sites slightly more verbose, but I think
it's worth it for the simpler API. Also, there are two `catch_unwind`
calls and one `catch_fatal_errors` call in this diff that become
removable thanks this change. (I will do that in a follow-up PR.)
  • Loading branch information
nnethercote committed Jun 5, 2024
1 parent eeefcd6 commit 5962aa9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,8 @@ use std::path::{Path, PathBuf};
use rustc_ast::token::TokenKind;
use rustc_ast::{ast, attr, ptr};
use rustc_errors::Diag;
use rustc_parse::{new_parser_from_file, parser::Parser as RawParser};
use rustc_parse::parser::Parser as RawParser;
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal};
use rustc_span::{sym, Span};
use thin_vec::ThinVec;

Expand Down Expand Up @@ -68,10 69,10 @@ impl<'a> ParserBuilder<'a> {
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diag<'a>>>> {
match input {
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
new_parser_from_file(psess, file, None)
unwrap_or_emit_fatal(new_parser_from_file(psess, file, None))
}))
.map_err(|_| None),
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
Input::Text(text) => rustc_parse::new_parser_from_source_str(
psess,
rustc_span::FileName::Custom("stdin".to_owned()),
text,
Expand Down Expand Up @@ -111,7 112,8 @@ impl<'a> Parser<'a> {
span: Span,
) -> Result<(ast::AttrVec, ThinVec<ptr::P<ast::Item>>, Span), ParserError> {
let result = catch_unwind(AssertUnwindSafe(|| {
let mut parser = new_parser_from_file(psess.inner(), path, Some(span));
let mut parser =
unwrap_or_emit_fatal(new_parser_from_file(psess.inner(), path, Some(span)));
match parser.parse_mod(&TokenKind::Eof) {
Ok((a, i, spans)) => Some((a, i, spans.inner_span)),
Err(e) => {
Expand Down

0 comments on commit 5962aa9

Please sign in to comment.