1
Fork 0

Make top-level rustc_parse functions fallible.

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.)
This commit is contained in:
Nicholas Nethercote 2024-05-31 15:43:18 +10:00
parent 264dbe4d81
commit b9037339cb
16 changed files with 105 additions and 75 deletions

View file

@ -1,5 +1,7 @@
use crate::parser::ForceCollect;
use crate::{new_parser_from_source_str, parser::Parser, source_str_to_stream};
use crate::{
new_parser_from_source_str, parser::Parser, source_str_to_stream, unwrap_or_emit_fatal,
};
use ast::token::IdentIsRaw;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, Token};
@ -29,7 +31,11 @@ fn psess() -> ParseSess {
/// Map string to parser (via tts).
fn string_to_parser(psess: &ParseSess, source_str: String) -> Parser<'_> {
new_parser_from_source_str(psess, PathBuf::from("bogofile").into(), source_str)
unwrap_or_emit_fatal(new_parser_from_source_str(
psess,
PathBuf::from("bogofile").into(),
source_str,
))
}
fn create_test_handler() -> (DiagCtxt, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
@ -82,7 +88,12 @@ where
/// Maps a string to tts, using a made-up filename.
pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
let psess = psess();
source_str_to_stream(&psess, PathBuf::from("bogofile").into(), source_str, None)
unwrap_or_emit_fatal(source_str_to_stream(
&psess,
PathBuf::from("bogofile").into(),
source_str,
None,
))
}
/// Parses a string, returns a crate.
@ -1064,7 +1075,8 @@ fn parse_item_from_source_str(
source: String,
psess: &ParseSess,
) -> PResult<'_, Option<P<ast::Item>>> {
new_parser_from_source_str(psess, name, source).parse_item(ForceCollect::No)
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source))
.parse_item(ForceCollect::No)
}
// Produces a `rustc_span::span`.
@ -1345,7 +1357,7 @@ fn ttdelim_span() {
source: String,
psess: &ParseSess,
) -> PResult<'_, P<ast::Expr>> {
new_parser_from_source_str(psess, name, source).parse_expr()
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source)).parse_expr()
}
create_default_session_globals_then(|| {