Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5400f1

Browse files
authoredJun 5, 2024
Unrolled build for rust-lang#125815
Rollup merge of rust-lang#125815 - nnethercote:rustc_parse-top-level-cleanups, r=spastorino `rustc_parse` top-level cleanups A bunch of improvements in and around `compiler/rustc_parse/src/lib.rs`. Many of the changes streamline the API in that file from this (12 functions and one macro): ``` name args return type ---- ---- ----------- panictry_buffer! Result<T, Vec<Diag>> T pub parse_crate_from_file path PResult<Crate> pub parse_crate_attrs_from_file path PResult<AttrVec> pub parse_crate_from_source_str name,src PResult<Crate> pub parse_crate_attrs_from_source_str name,src PResult<AttrVec> pub new_parser_from_source_str name,src Parser pub maybe_new_parser_from_source_str name,src Result<Parser, Vec<Diag>> pub new_parser_from_file path,error_sp Parser maybe_source_file_to_parser srcfile Result<Parser, Vec<Diag>> pub parse_stream_from_source_str name,src,override_sp TokenStream pub source_file_to_stream srcfile,override_sp TokenStream maybe_file_to_stream srcfile,override_sp Result<TokenStream, Vec<Diag>> pub stream_to_parser stream,subparser_name Parser ``` to this: ``` name args return type ---- ---- ----------- unwrap_or_emit_fatal Result<T, Vec<Diag>> T pub new_parser_from_source_str name,src Result<Parser, Vec<Diag>> pub new_parser_from_file path,error_sp Result<Parser, Vec<Diag>> new_parser_from_source_file srcfile Result<Parser, Vec<Diag>> pub source_str_to_stream name,src,override_sp Result<TokenStream, Vec<Diag>> source_file_to_stream srcfile,override_sp Result<TokenStream, Vec<Diag>> ``` I found the old API quite confusing, with lots of similar-sounding function names and no clear structure. I think the new API is much better. r? `@spastorino`
2 parents 5ee2dfd + 2d4e7df commit f5400f1

File tree

24 files changed

+213
-255
lines changed

24 files changed

+213
-255
lines changed
 

‎compiler/rustc_builtin_macros/src/cfg_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl CfgEval<'_, '_> {
196196
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
197197
// to the captured `AttrTokenStream` (specifically, we capture
198198
// `AttrTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
199-
let mut parser = rustc_parse::stream_to_parser(&self.cfg.sess.psess, orig_tokens, None);
199+
let mut parser = Parser::new(&self.cfg.sess.psess, orig_tokens, None);
200200
parser.capture_cfg = true;
201201
match parse_annotatable_with(&mut parser) {
202202
Ok(a) => annotatable = a,

‎compiler/rustc_builtin_macros/src/cmdline_attrs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use crate::errors;
44
use rustc_ast::attr::mk_attr;
55
use rustc_ast::token;
66
use rustc_ast::{self as ast, AttrItem, AttrStyle};
7+
use rustc_parse::{new_parser_from_source_str, unwrap_or_emit_fatal};
78
use rustc_session::parse::ParseSess;
89
use rustc_span::FileName;
910

1011
pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
1112
for raw_attr in attrs {
12-
let mut parser = rustc_parse::new_parser_from_source_str(
13+
let mut parser = unwrap_or_emit_fatal(new_parser_from_source_str(
1314
psess,
1415
FileName::cli_crate_attr_source_code(raw_attr),
1516
raw_attr.clone(),
16-
);
17+
));
1718

1819
let start_span = parser.token.span;
1920
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) {

‎compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_expand::base::{
1212
};
1313
use rustc_expand::module::DirOwnership;
1414
use rustc_lint_defs::BuiltinLintDiag;
15-
use rustc_parse::new_parser_from_file;
1615
use rustc_parse::parser::{ForceCollect, Parser};
16+
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal};
1717
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
1818
use rustc_span::source_map::SourceMap;
1919
use rustc_span::symbol::Symbol;
@@ -126,7 +126,7 @@ pub(crate) fn expand_include<'cx>(
126126
return ExpandResult::Ready(DummyResult::any(sp, guar));
127127
}
128128
};
129-
let p = new_parser_from_file(cx.psess(), &file, Some(sp));
129+
let p = unwrap_or_emit_fatal(new_parser_from_file(cx.psess(), &file, Some(sp)));
130130

131131
// If in the included file we have e.g., `mod bar;`,
132132
// then the path of `bar.rs` should be relative to the directory of `file`.

‎compiler/rustc_driver_impl/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use rustc_interface::{interface, Queries};
3232
use rustc_lint::unerased_lint_store;
3333
use rustc_metadata::creader::MetadataLoader;
3434
use rustc_metadata::locator;
35+
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
3536
use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
3637
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType};
3738
use rustc_session::getopts::{self, Matches};
@@ -1264,12 +1265,13 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
12641265
}
12651266

12661267
fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
1267-
match &sess.io.input {
1268-
Input::File(ifile) => rustc_parse::parse_crate_attrs_from_file(ifile, &sess.psess),
1268+
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
1269+
Input::File(file) => new_parser_from_file(&sess.psess, file, None),
12691270
Input::Str { name, input } => {
1270-
rustc_parse::parse_crate_attrs_from_source_str(name.clone(), input.clone(), &sess.psess)
1271+
new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
12711272
}
1272-
}
1273+
});
1274+
parser.parse_inner_attributes()
12731275
}
12741276

12751277
/// Runs a closure and catches unwinds triggered by fatal errors.

‎compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_data_structures::sync::{self, Lrc};
1515
use rustc_errors::{DiagCtxt, ErrorGuaranteed, PResult};
1616
use rustc_feature::Features;
1717
use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools};
18-
use rustc_parse::{parser, MACRO_ARGUMENTS};
18+
use rustc_parse::{parser::Parser, MACRO_ARGUMENTS};
1919
use rustc_session::config::CollapseMacroDebuginfo;
2020
use rustc_session::{parse::ParseSess, Limit, Session};
2121
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
@@ -1149,8 +1149,8 @@ impl<'a> ExtCtxt<'a> {
11491149
pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
11501150
expand::MacroExpander::new(self, true)
11511151
}
1152-
pub fn new_parser_from_tts(&self, stream: TokenStream) -> parser::Parser<'a> {
1153-
rustc_parse::stream_to_parser(&self.sess.psess, stream, MACRO_ARGUMENTS)
1152+
pub fn new_parser_from_tts(&self, stream: TokenStream) -> Parser<'a> {
1153+
Parser::new(&self.sess.psess, stream, MACRO_ARGUMENTS)
11541154
}
11551155
pub fn source_map(&self) -> &'a SourceMap {
11561156
self.sess.psess.source_map()

‎compiler/rustc_expand/src/module.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::errors::{
55
use rustc_ast::ptr::P;
66
use rustc_ast::{token, AttrVec, Attribute, Inline, Item, ModSpans};
77
use rustc_errors::{Diag, ErrorGuaranteed};
8-
use rustc_parse::new_parser_from_file;
98
use rustc_parse::validate_attr;
9+
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal};
1010
use rustc_session::parse::ParseSess;
1111
use rustc_session::Session;
1212
use rustc_span::symbol::{sym, Ident};
@@ -66,7 +66,8 @@ pub(crate) fn parse_external_mod(
6666
}
6767

6868
// Actually parse the external file as a module.
69-
let mut parser = new_parser_from_file(&sess.psess, &mp.file_path, Some(span));
69+
let mut parser =
70+
unwrap_or_emit_fatal(new_parser_from_file(&sess.psess, &mp.file_path, Some(span)));
7071
let (inner_attrs, items, inner_span) =
7172
parser.parse_mod(&token::Eof).map_err(|err| ModError::ParserError(err))?;
7273
attrs.extend(inner_attrs);

‎compiler/rustc_expand/src/proc_macro.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast as ast;
66
use rustc_ast::ptr::P;
77
use rustc_ast::tokenstream::TokenStream;
88
use rustc_errors::ErrorGuaranteed;
9-
use rustc_parse::parser::ForceCollect;
9+
use rustc_parse::parser::{ForceCollect, Parser};
1010
use rustc_session::config::ProcMacroExecutionStrategy;
1111
use rustc_span::profiling::SpannedEventArgRecorder;
1212
use rustc_span::Span;
@@ -154,8 +154,7 @@ impl MultiItemModifier for DeriveProcMacro {
154154
};
155155

156156
let error_count_before = ecx.dcx().err_count();
157-
let mut parser =
158-
rustc_parse::stream_to_parser(&ecx.sess.psess, stream, Some("proc-macro derive"));
157+
let mut parser = Parser::new(&ecx.sess.psess, stream, Some("proc-macro derive"));
159158
let mut items = vec![];
160159

161160
loop {

‎compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::sync::Lrc;
1414
use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan, PResult};
1515
use rustc_parse::lexer::nfc_normalize;
16-
use rustc_parse::parse_stream_from_source_str;
16+
use rustc_parse::parser::Parser;
17+
use rustc_parse::{new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal};
1718
use rustc_session::parse::ParseSess;
1819
use rustc_span::def_id::CrateNum;
1920
use rustc_span::symbol::{self, sym, Symbol};
@@ -466,7 +467,8 @@ impl server::FreeFunctions for Rustc<'_, '_> {
466467

467468
fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, ()> {
468469
let name = FileName::proc_macro_source_code(s);
469-
let mut parser = rustc_parse::new_parser_from_source_str(self.psess(), name, s.to_owned());
470+
let mut parser =
471+
unwrap_or_emit_fatal(new_parser_from_source_str(self.psess(), name, s.to_owned()));
470472

471473
let first_span = parser.token.span.data();
472474
let minus_present = parser.eat(&token::BinOp(token::Minus));
@@ -538,12 +540,12 @@ impl server::TokenStream for Rustc<'_, '_> {
538540
}
539541

540542
fn from_str(&mut self, src: &str) -> Self::TokenStream {
541-
parse_stream_from_source_str(
543+
unwrap_or_emit_fatal(source_str_to_stream(
544+
self.psess(),
542545
FileName::proc_macro_source_code(src),
543546
src.to_string(),
544-
self.psess(),
545547
Some(self.call_site),
546-
)
548+
))
547549
}
548550

549551
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
@@ -553,11 +555,7 @@ impl server::TokenStream for Rustc<'_, '_> {
553555
fn expand_expr(&mut self, stream: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
554556
// Parse the expression from our tokenstream.
555557
let expr: PResult<'_, _> = try {
556-
let mut p = rustc_parse::stream_to_parser(
557-
self.psess(),
558-
stream.clone(),
559-
Some("proc_macro expand expr"),
560-
);
558+
let mut p = Parser::new(self.psess(), stream.clone(), Some("proc_macro expand expr"));
561559
let expr = p.parse_expr()?;
562560
if p.token != token::Eof {
563561
p.unexpected()?;

‎compiler/rustc_interface/src/interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_lint::LintStore;
1414
use rustc_middle::ty;
1515
use rustc_middle::ty::CurrentGcx;
1616
use rustc_middle::util::Providers;
17-
use rustc_parse::maybe_new_parser_from_source_str;
17+
use rustc_parse::new_parser_from_source_str;
1818
use rustc_query_impl::QueryCtxt;
1919
use rustc_query_system::query::print_query_stack;
2020
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
@@ -67,7 +67,7 @@ pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg {
6767
};
6868
}
6969

70-
match maybe_new_parser_from_source_str(&psess, filename, s.to_string()) {
70+
match new_parser_from_source_str(&psess, filename, s.to_string()) {
7171
Ok(mut parser) => match parser.parse_meta_item() {
7272
Ok(meta_item) if parser.token == token::Eof => {
7373
if meta_item.path.segments.len() != 1 {
@@ -166,7 +166,7 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
166166
error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
167167
};
168168

169-
let mut parser = match maybe_new_parser_from_source_str(&psess, filename, s.to_string()) {
169+
let mut parser = match new_parser_from_source_str(&psess, filename, s.to_string()) {
170170
Ok(parser) => parser,
171171
Err(errs) => {
172172
errs.into_iter().for_each(|err| err.cancel());

‎compiler/rustc_interface/src/passes.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use rustc_middle::arena::Arena;
1919
use rustc_middle::dep_graph::DepGraph;
2020
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
2121
use rustc_middle::util::Providers;
22-
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
22+
use rustc_parse::{
23+
new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal, validate_attr,
24+
};
2325
use rustc_passes::{abi_test, hir_stats, layout_test};
2426
use rustc_resolve::Resolver;
2527
use rustc_session::code_stats::VTableSizeInfo;
@@ -42,11 +44,14 @@ use std::{env, fs, iter};
4244
use tracing::{info, instrument};
4345

4446
pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> {
45-
let krate = sess.time("parse_crate", || match &sess.io.input {
46-
Input::File(file) => parse_crate_from_file(file, &sess.psess),
47-
Input::Str { input, name } => {
48-
parse_crate_from_source_str(name.clone(), input.clone(), &sess.psess)
49-
}
47+
let krate = sess.time("parse_crate", || {
48+
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
49+
Input::File(file) => new_parser_from_file(&sess.psess, file, None),
50+
Input::Str { input, name } => {
51+
new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
52+
}
53+
});
54+
parser.parse_crate_mod()
5055
})?;
5156

5257
if sess.opts.unstable_opts.input_stats {

‎compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) struct UnmatchedDelim {
4242
pub candidate_span: Option<Span>,
4343
}
4444

45-
pub(crate) fn parse_token_trees<'psess, 'src>(
45+
pub(crate) fn lex_token_trees<'psess, 'src>(
4646
psess: &'psess ParseSess,
4747
mut src: &'src str,
4848
mut start_pos: BytePos,
@@ -66,7 +66,7 @@ pub(crate) fn parse_token_trees<'psess, 'src>(
6666
last_lifetime: None,
6767
};
6868
let (stream, res, unmatched_delims) =
69-
tokentrees::TokenTreesReader::parse_all_token_trees(string_reader);
69+
tokentrees::TokenTreesReader::lex_all_token_trees(string_reader);
7070
match res {
7171
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
7272
_ => {

‎compiler/rustc_parse/src/lexer/tokentrees.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::diagnostics::report_suspicious_mismatch_block;
22
use super::diagnostics::same_indentation_level;
33
use super::diagnostics::TokenTreeDiagInfo;
44
use super::{StringReader, UnmatchedDelim};
5+
use crate::Parser;
56
use rustc_ast::token::{self, Delimiter, Token};
67
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
78
use rustc_ast_pretty::pprust::token_to_string;
@@ -17,22 +18,21 @@ pub(super) struct TokenTreesReader<'psess, 'src> {
1718
}
1819

1920
impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
20-
pub(super) fn parse_all_token_trees(
21+
pub(super) fn lex_all_token_trees(
2122
string_reader: StringReader<'psess, 'src>,
2223
) -> (TokenStream, Result<(), Vec<PErr<'psess>>>, Vec<UnmatchedDelim>) {
2324
let mut tt_reader = TokenTreesReader {
2425
string_reader,
2526
token: Token::dummy(),
2627
diag_info: TokenTreeDiagInfo::default(),
2728
};
28-
let (_open_spacing, stream, res) =
29-
tt_reader.parse_token_trees(/* is_delimited */ false);
29+
let (_open_spacing, stream, res) = tt_reader.lex_token_trees(/* is_delimited */ false);
3030
(stream, res, tt_reader.diag_info.unmatched_delims)
3131
}
3232

33-
// Parse a stream of tokens into a list of `TokenTree`s. The `Spacing` in
34-
// the result is that of the opening delimiter.
35-
fn parse_token_trees(
33+
// Lex into a token stream. The `Spacing` in the result is that of the
34+
// opening delimiter.
35+
fn lex_token_trees(
3636
&mut self,
3737
is_delimited: bool,
3838
) -> (Spacing, TokenStream, Result<(), Vec<PErr<'psess>>>) {
@@ -42,12 +42,10 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
4242
let mut buf = Vec::new();
4343
loop {
4444
match self.token.kind {
45-
token::OpenDelim(delim) => {
46-
buf.push(match self.parse_token_tree_open_delim(delim) {
47-
Ok(val) => val,
48-
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
49-
})
50-
}
45+
token::OpenDelim(delim) => buf.push(match self.lex_token_tree_open_delim(delim) {
46+
Ok(val) => val,
47+
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
48+
}),
5149
token::CloseDelim(delim) => {
5250
return (
5351
open_spacing,
@@ -95,24 +93,24 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
9593
err
9694
}
9795

98-
fn parse_token_tree_open_delim(
96+
fn lex_token_tree_open_delim(
9997
&mut self,
10098
open_delim: Delimiter,
10199
) -> Result<TokenTree, Vec<PErr<'psess>>> {
102-
// The span for beginning of the delimited section
100+
// The span for beginning of the delimited section.
103101
let pre_span = self.token.span;
104102

105103
self.diag_info.open_braces.push((open_delim, self.token.span));
106104

107-
// Parse the token trees within the delimiters.
105+
// Lex the token trees within the delimiters.
108106
// We stop at any delimiter so we can try to recover if the user
109107
// uses an incorrect delimiter.
110-
let (open_spacing, tts, res) = self.parse_token_trees(/* is_delimited */ true);
108+
let (open_spacing, tts, res) = self.lex_token_trees(/* is_delimited */ true);
111109
if let Err(errs) = res {
112110
return Err(self.unclosed_delim_err(tts, errs));
113111
}
114112

115-
// Expand to cover the entire delimited token tree
113+
// Expand to cover the entire delimited token tree.
116114
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);
117115
let sm = self.string_reader.psess.source_map();
118116

@@ -150,7 +148,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
150148
self.diag_info.last_unclosed_found_span = Some(self.token.span);
151149
// This is a conservative error: only report the last unclosed
152150
// delimiter. The previous unclosed delimiters could actually be
153-
// closed! The parser just hasn't gotten to them yet.
151+
// closed! The lexer just hasn't gotten to them yet.
154152
if let Some(&(_, sp)) = self.diag_info.open_braces.last() {
155153
unclosed_delimiter = Some(sp);
156154
};
@@ -234,11 +232,11 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
234232
) -> Vec<PErr<'psess>> {
235233
// If there are unclosed delims, see if there are diff markers and if so, point them
236234
// out instead of complaining about the unclosed delims.
237-
let mut parser = crate::stream_to_parser(self.string_reader.psess, tts, None);
235+
let mut parser = Parser::new(self.string_reader.psess, tts, None);
238236
let mut diff_errs = vec![];
239-
// Suggest removing a `{` we think appears in an `if`/`while` condition
240-
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition, but
241-
// we have no way of tracking this in the lexer itself, so we piggyback on the parser
237+
// Suggest removing a `{` we think appears in an `if`/`while` condition.
238+
// We want to suggest removing a `{` only if we think we're in an `if`/`while` condition,
239+
// but we have no way of tracking this in the lexer itself, so we piggyback on the parser.
242240
let mut in_cond = false;
243241
while parser.token != token::Eof {
244242
if let Err(diff_err) = parser.err_vcs_conflict_marker() {
@@ -249,14 +247,15 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
249247
parser.token.kind,
250248
token::CloseDelim(Delimiter::Brace) | token::FatArrow
251249
) {
252-
// end of the `if`/`while` body, or the end of a `match` guard
250+
// End of the `if`/`while` body, or the end of a `match` guard.
253251
in_cond = false;
254252
} else if in_cond && parser.token == token::OpenDelim(Delimiter::Brace) {
255253
// Store the `&&` and `let` to use their spans later when creating the diagnostic
256254
let maybe_andand = parser.look_ahead(1, |t| t.clone());
257255
let maybe_let = parser.look_ahead(2, |t| t.clone());
258256
if maybe_andand == token::OpenDelim(Delimiter::Brace) {
259-
// This might be the beginning of the `if`/`while` body (i.e., the end of the condition)
257+
// This might be the beginning of the `if`/`while` body (i.e., the end of the
258+
// condition).
260259
in_cond = false;
261260
} else if maybe_andand == token::AndAnd && maybe_let.is_keyword(kw::Let) {
262261
let mut err = parser.dcx().struct_span_err(
@@ -288,8 +287,7 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
288287
}
289288

290289
fn close_delim_err(&mut self, delim: Delimiter) -> PErr<'psess> {
291-
// An unexpected closing delimiter (i.e., there is no
292-
// matching opening delimiter).
290+
// An unexpected closing delimiter (i.e., there is no matching opening delimiter).
293291
let token_str = token_to_string(&self.token);
294292
let msg = format!("unexpected closing delimiter: `{token_str}`");
295293
let mut err = self.string_reader.psess.dcx.struct_span_err(self.token.span, msg);

‎compiler/rustc_parse/src/lexer/unicode_chars.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,42 +129,42 @@ pub(super) const UNICODE_ARRAY: &[(char, &str, &str)] = &[
129129
('。', "Ideographic Full Stop", "."),
130130
('︒', "Presentation Form For Vertical Ideographic Full Stop", "."),
131131

132-
('՝', "Armenian Comma", "\'"),
133-
(''', "Fullwidth Apostrophe", "\'"),
134-
('‘', "Left Single Quotation Mark", "\'"),
135-
('’', "Right Single Quotation Mark", "\'"),
136-
('‛', "Single High-Reversed-9 Quotation Mark", "\'"),
137-
('′', "Prime", "\'"),
138-
('‵', "Reversed Prime", "\'"),
139-
('՚', "Armenian Apostrophe", "\'"),
140-
('׳', "Hebrew Punctuation Geresh", "\'"),
141-
('`', "Grave Accent", "\'"),
142-
('`', "Greek Varia", "\'"),
143-
('`', "Fullwidth Grave Accent", "\'"),
144-
('´', "Acute Accent", "\'"),
145-
('΄', "Greek Tonos", "\'"),
146-
('´', "Greek Oxia", "\'"),
147-
('᾽', "Greek Koronis", "\'"),
148-
('᾿', "Greek Psili", "\'"),
149-
('῾', "Greek Dasia", "\'"),
150-
('ʹ', "Modifier Letter Prime", "\'"),
151-
('ʹ', "Greek Numeral Sign", "\'"),
152-
('ˈ', "Modifier Letter Vertical Line", "\'"),
153-
('ˊ', "Modifier Letter Acute Accent", "\'"),
154-
('ˋ', "Modifier Letter Grave Accent", "\'"),
155-
('˴', "Modifier Letter Middle Grave Accent", "\'"),
156-
('ʻ', "Modifier Letter Turned Comma", "\'"),
157-
('ʽ', "Modifier Letter Reversed Comma", "\'"),
158-
('ʼ', "Modifier Letter Apostrophe", "\'"),
159-
('ʾ', "Modifier Letter Right Half Ring", "\'"),
160-
('ꞌ', "Latin Small Letter Saltillo", "\'"),
161-
('י', "Hebrew Letter Yod", "\'"),
162-
('ߴ', "Nko High Tone Apostrophe", "\'"),
163-
('ߵ', "Nko Low Tone Apostrophe", "\'"),
164-
('ᑊ', "Canadian Syllabics West-Cree P", "\'"),
165-
('ᛌ', "Runic Letter Short-Twig-Sol S", "\'"),
166-
('𖽑', "Miao Sign Aspiration", "\'"),
167-
('𖽒', "Miao Sign Reformed Voicing", "\'"),
132+
('՝', "Armenian Comma", "'"),
133+
(''', "Fullwidth Apostrophe", "'"),
134+
('‘', "Left Single Quotation Mark", "'"),
135+
('’', "Right Single Quotation Mark", "'"),
136+
('‛', "Single High-Reversed-9 Quotation Mark", "'"),
137+
('′', "Prime", "'"),
138+
('‵', "Reversed Prime", "'"),
139+
('՚', "Armenian Apostrophe", "'"),
140+
('׳', "Hebrew Punctuation Geresh", "'"),
141+
('`', "Grave Accent", "'"),
142+
('`', "Greek Varia", "'"),
143+
('`', "Fullwidth Grave Accent", "'"),
144+
('´', "Acute Accent", "'"),
145+
('΄', "Greek Tonos", "'"),
146+
('´', "Greek Oxia", "'"),
147+
('᾽', "Greek Koronis", "'"),
148+
('᾿', "Greek Psili", "'"),
149+
('῾', "Greek Dasia", "'"),
150+
('ʹ', "Modifier Letter Prime", "'"),
151+
('ʹ', "Greek Numeral Sign", "'"),
152+
('ˈ', "Modifier Letter Vertical Line", "'"),
153+
('ˊ', "Modifier Letter Acute Accent", "'"),
154+
('ˋ', "Modifier Letter Grave Accent", "'"),
155+
('˴', "Modifier Letter Middle Grave Accent", "'"),
156+
('ʻ', "Modifier Letter Turned Comma", "'"),
157+
('ʽ', "Modifier Letter Reversed Comma", "'"),
158+
('ʼ', "Modifier Letter Apostrophe", "'"),
159+
('ʾ', "Modifier Letter Right Half Ring", "'"),
160+
('ꞌ', "Latin Small Letter Saltillo", "'"),
161+
('י', "Hebrew Letter Yod", "'"),
162+
('ߴ', "Nko High Tone Apostrophe", "'"),
163+
('ߵ', "Nko Low Tone Apostrophe", "'"),
164+
('ᑊ', "Canadian Syllabics West-Cree P", "'"),
165+
('ᛌ', "Runic Letter Short-Twig-Sol S", "'"),
166+
('𖽑', "Miao Sign Aspiration", "'"),
167+
('𖽒', "Miao Sign Reformed Voicing", "'"),
168168

169169
('᳓', "Vedic Sign Nihshvasa", "\""),
170170
('"', "Fullwidth Quotation Mark", "\""),
@@ -298,6 +298,7 @@ pub(super) const UNICODE_ARRAY: &[(char, &str, &str)] = &[
298298
('〉', "Right Angle Bracket", ">"),
299299
('》', "Right Double Angle Bracket", ">"),
300300
('>', "Fullwidth Greater-Than Sign", ">"),
301+
301302
('⩵', "Two Consecutive Equals Signs", "==")
302303
];
303304

@@ -332,7 +333,7 @@ const ASCII_ARRAY: &[(&str, &str, Option<token::TokenKind>)] = &[
332333
(">", "Greater-Than Sign", Some(token::Gt)),
333334
// FIXME: Literals are already lexed by this point, so we can't recover gracefully just by
334335
// spitting the correct token out.
335-
("\'", "Single Quote", None),
336+
("'", "Single Quote", None),
336337
("\"", "Quotation Mark", None),
337338
];
338339

‎compiler/rustc_parse/src/lib.rs

Lines changed: 54 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -34,84 +34,41 @@ mod errors;
3434

3535
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
3636

37-
// A bunch of utility functions of the form `parse_<thing>_from_<source>`
38-
// where <thing> includes crate, expr, item, stmt, tts, and one that
39-
// uses a HOF to parse anything, and <source> includes file and
40-
// `source_str`.
41-
42-
/// A variant of 'panictry!' that works on a `Vec<Diag>` instead of a single `Diag`.
43-
macro_rules! panictry_buffer {
44-
($e:expr) => {{
45-
use std::result::Result::{Err, Ok};
46-
match $e {
47-
Ok(e) => e,
48-
Err(errs) => {
49-
for e in errs {
50-
e.emit();
51-
}
52-
FatalError.raise()
37+
// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.
38+
pub fn unwrap_or_emit_fatal<T>(expr: Result<T, Vec<Diag<'_>>>) -> T {
39+
match expr {
40+
Ok(expr) => expr,
41+
Err(errs) => {
42+
for err in errs {
43+
err.emit();
5344
}
45+
FatalError.raise()
5446
}
55-
}};
56-
}
57-
58-
pub fn parse_crate_from_file<'a>(input: &Path, psess: &'a ParseSess) -> PResult<'a, ast::Crate> {
59-
let mut parser = new_parser_from_file(psess, input, None);
60-
parser.parse_crate_mod()
61-
}
62-
63-
pub fn parse_crate_attrs_from_file<'a>(
64-
input: &Path,
65-
psess: &'a ParseSess,
66-
) -> PResult<'a, ast::AttrVec> {
67-
let mut parser = new_parser_from_file(psess, input, None);
68-
parser.parse_inner_attributes()
69-
}
70-
71-
pub fn parse_crate_from_source_str(
72-
name: FileName,
73-
source: String,
74-
psess: &ParseSess,
75-
) -> PResult<'_, ast::Crate> {
76-
new_parser_from_source_str(psess, name, source).parse_crate_mod()
77-
}
78-
79-
pub fn parse_crate_attrs_from_source_str(
80-
name: FileName,
81-
source: String,
82-
psess: &ParseSess,
83-
) -> PResult<'_, ast::AttrVec> {
84-
new_parser_from_source_str(psess, name, source).parse_inner_attributes()
85-
}
86-
87-
pub fn parse_stream_from_source_str(
88-
name: FileName,
89-
source: String,
90-
psess: &ParseSess,
91-
override_span: Option<Span>,
92-
) -> TokenStream {
93-
source_file_to_stream(psess, psess.source_map().new_source_file(name, source), override_span)
94-
}
95-
96-
/// Creates a new parser from a source string.
97-
pub fn new_parser_from_source_str(psess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
98-
panictry_buffer!(maybe_new_parser_from_source_str(psess, name, source))
47+
}
9948
}
10049

101-
/// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
102-
/// token stream; these must be consumed via `emit`, `cancel`, etc., otherwise a panic will occur
103-
/// when they are dropped.
104-
pub fn maybe_new_parser_from_source_str(
50+
/// Creates a new parser from a source string. On failure, the errors must be consumed via
51+
/// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
52+
/// dropped.
53+
pub fn new_parser_from_source_str(
10554
psess: &ParseSess,
10655
name: FileName,
10756
source: String,
10857
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
109-
maybe_source_file_to_parser(psess, psess.source_map().new_source_file(name, source))
58+
let source_file = psess.source_map().new_source_file(name, source);
59+
new_parser_from_source_file(psess, source_file)
11060
}
11161

112-
/// Creates a new parser, aborting if the file doesn't exist. If a span is given, that is used on
113-
/// an error as the source of the problem.
114-
pub fn new_parser_from_file<'a>(psess: &'a ParseSess, path: &Path, sp: Option<Span>) -> Parser<'a> {
62+
/// Creates a new parser from a filename. On failure, the errors must be consumed via
63+
/// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
64+
/// dropped.
65+
///
66+
/// If a span is given, that is used on an error as the source of the problem.
67+
pub fn new_parser_from_file<'a>(
68+
psess: &'a ParseSess,
69+
path: &Path,
70+
sp: Option<Span>,
71+
) -> Result<Parser<'a>, Vec<Diag<'a>>> {
11572
let source_file = psess.source_map().load_file(path).unwrap_or_else(|e| {
11673
let msg = format!("couldn't read {}: {}", path.display(), e);
11774
let mut err = psess.dcx.struct_fatal(msg);
@@ -120,40 +77,37 @@ pub fn new_parser_from_file<'a>(psess: &'a ParseSess, path: &Path, sp: Option<Sp
12077
}
12178
err.emit();
12279
});
123-
124-
panictry_buffer!(maybe_source_file_to_parser(psess, source_file))
80+
new_parser_from_source_file(psess, source_file)
12581
}
12682

12783
/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
12884
/// the initial token stream.
129-
fn maybe_source_file_to_parser(
85+
fn new_parser_from_source_file(
13086
psess: &ParseSess,
13187
source_file: Lrc<SourceFile>,
13288
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
13389
let end_pos = source_file.end_position();
134-
let stream = maybe_file_to_stream(psess, source_file, None)?;
135-
let mut parser = stream_to_parser(psess, stream, None);
90+
let stream = source_file_to_stream(psess, source_file, None)?;
91+
let mut parser = Parser::new(psess, stream, None);
13692
if parser.token == token::Eof {
13793
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt(), None);
13894
}
139-
14095
Ok(parser)
14196
}
14297

143-
// Base abstractions
144-
145-
/// Given a `source_file`, produces a sequence of token trees.
146-
pub fn source_file_to_stream(
98+
pub fn source_str_to_stream(
14799
psess: &ParseSess,
148-
source_file: Lrc<SourceFile>,
100+
name: FileName,
101+
source: String,
149102
override_span: Option<Span>,
150-
) -> TokenStream {
151-
panictry_buffer!(maybe_file_to_stream(psess, source_file, override_span))
103+
) -> Result<TokenStream, Vec<Diag<'_>>> {
104+
let source_file = psess.source_map().new_source_file(name, source);
105+
source_file_to_stream(psess, source_file, override_span)
152106
}
153107

154108
/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
155109
/// parsing the token stream.
156-
fn maybe_file_to_stream<'psess>(
110+
fn source_file_to_stream<'psess>(
157111
psess: &'psess ParseSess,
158112
source_file: Lrc<SourceFile>,
159113
override_span: Option<Span>,
@@ -165,16 +119,7 @@ fn maybe_file_to_stream<'psess>(
165119
));
166120
});
167121

168-
lexer::parse_token_trees(psess, src.as_str(), source_file.start_pos, override_span)
169-
}
170-
171-
/// Given a stream and the `ParseSess`, produces a parser.
172-
pub fn stream_to_parser<'a>(
173-
psess: &'a ParseSess,
174-
stream: TokenStream,
175-
subparser_name: Option<&'static str>,
176-
) -> Parser<'a> {
177-
Parser::new(psess, stream, subparser_name)
122+
lexer::lex_token_trees(psess, src.as_str(), source_file.start_pos, override_span)
178123
}
179124

180125
/// Runs the given subparser `f` on the tokens of the given `attr`'s item.
@@ -195,19 +140,28 @@ pub fn parse_in<'a, T>(
195140
pub fn fake_token_stream_for_item(psess: &ParseSess, item: &ast::Item) -> TokenStream {
196141
let source = pprust::item_to_string(item);
197142
let filename = FileName::macro_expansion_source_code(&source);
198-
parse_stream_from_source_str(filename, source, psess, Some(item.span))
143+
unwrap_or_emit_fatal(source_str_to_stream(psess, filename, source, Some(item.span)))
199144
}
200145

201146
pub fn fake_token_stream_for_crate(psess: &ParseSess, krate: &ast::Crate) -> TokenStream {
202147
let source = pprust::crate_to_string_for_macros(krate);
203148
let filename = FileName::macro_expansion_source_code(&source);
204-
parse_stream_from_source_str(filename, source, psess, Some(krate.spans.inner_span))
149+
unwrap_or_emit_fatal(source_str_to_stream(
150+
psess,
151+
filename,
152+
source,
153+
Some(krate.spans.inner_span),
154+
))
205155
}
206156

207157
pub fn parse_cfg_attr(
208158
attr: &Attribute,
209159
psess: &ParseSess,
210160
) -> Option<(MetaItem, Vec<(AttrItem, Span)>)> {
161+
const CFG_ATTR_GRAMMAR_HELP: &str = "#[cfg_attr(condition, attribute, other_attribute, ...)]";
162+
const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
163+
<https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>";
164+
211165
match attr.get_normal_item().args {
212166
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, ref tokens })
213167
if !tokens.is_empty() =>
@@ -222,16 +176,12 @@ pub fn parse_cfg_attr(
222176
}
223177
}
224178
}
225-
_ => error_malformed_cfg_attr_missing(attr.span, psess),
179+
_ => {
180+
psess.dcx.emit_err(errors::MalformedCfgAttr {
181+
span: attr.span,
182+
sugg: CFG_ATTR_GRAMMAR_HELP,
183+
});
184+
}
226185
}
227186
None
228187
}
229-
230-
const CFG_ATTR_GRAMMAR_HELP: &str = "#[cfg_attr(condition, attribute, other_attribute, ...)]";
231-
const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
232-
<https://doc.rust-lang.org/reference/conditional-compilation.html\
233-
#the-cfg_attr-attribute>";
234-
235-
fn error_malformed_cfg_attr_missing(span: Span, psess: &ParseSess) {
236-
psess.dcx.emit_err(errors::MalformedCfgAttr { span, sugg: CFG_ATTR_GRAMMAR_HELP });
237-
}

‎compiler/rustc_parse/src/parser/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'a> Parser<'a> {
265265
/// terminated by a semicolon.
266266
///
267267
/// Matches `inner_attrs*`.
268-
pub(crate) fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
268+
pub fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
269269
let mut attrs = ast::AttrVec::new();
270270
loop {
271271
let start_pos: u32 = self.num_bump_calls.try_into().unwrap();

‎compiler/rustc_parse/src/parser/tests.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::parser::ForceCollect;
2-
use crate::{new_parser_from_source_str, parser::Parser, source_file_to_stream};
2+
use crate::{
3+
new_parser_from_source_str, parser::Parser, source_str_to_stream, unwrap_or_emit_fatal,
4+
};
35
use ast::token::IdentIsRaw;
46
use rustc_ast::ptr::P;
57
use rustc_ast::token::{self, Delimiter, Token};
@@ -29,7 +31,11 @@ fn psess() -> ParseSess {
2931

3032
/// Map string to parser (via tts).
3133
fn string_to_parser(psess: &ParseSess, source_str: String) -> Parser<'_> {
32-
new_parser_from_source_str(psess, PathBuf::from("bogofile").into(), source_str)
34+
unwrap_or_emit_fatal(new_parser_from_source_str(
35+
psess,
36+
PathBuf::from("bogofile").into(),
37+
source_str,
38+
))
3339
}
3440

3541
fn create_test_handler() -> (DiagCtxt, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
@@ -82,11 +88,12 @@ where
8288
/// Maps a string to tts, using a made-up filename.
8389
pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
8490
let psess = psess();
85-
source_file_to_stream(
91+
unwrap_or_emit_fatal(source_str_to_stream(
8692
&psess,
87-
psess.source_map().new_source_file(PathBuf::from("bogofile").into(), source_str),
93+
PathBuf::from("bogofile").into(),
94+
source_str,
8895
None,
89-
)
96+
))
9097
}
9198

9299
/// Parses a string, returns a crate.
@@ -1068,7 +1075,8 @@ fn parse_item_from_source_str(
10681075
source: String,
10691076
psess: &ParseSess,
10701077
) -> PResult<'_, Option<P<ast::Item>>> {
1071-
new_parser_from_source_str(psess, name, source).parse_item(ForceCollect::No)
1078+
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source))
1079+
.parse_item(ForceCollect::No)
10721080
}
10731081

10741082
// Produces a `rustc_span::span`.
@@ -1349,7 +1357,7 @@ fn ttdelim_span() {
13491357
source: String,
13501358
psess: &ParseSess,
13511359
) -> PResult<'_, P<ast::Expr>> {
1352-
new_parser_from_source_str(psess, name, source).parse_expr()
1360+
unwrap_or_emit_fatal(new_parser_from_source_str(psess, name, source)).parse_expr()
13531361
}
13541362

13551363
create_default_session_globals_then(|| {

‎src/librustdoc/clean/render_macro_matchers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String
6565
let psess = ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec());
6666
let file_name = source_map.span_to_filename(span);
6767
let mut parser =
68-
match rustc_parse::maybe_new_parser_from_source_str(&psess, file_name, snippet.clone()) {
68+
match rustc_parse::new_parser_from_source_str(&psess, file_name, snippet.clone()) {
6969
Ok(parser) => parser,
7070
Err(errs) => {
7171
errs.into_iter().for_each(|err| err.cancel());

‎src/librustdoc/doctest.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_interface::interface;
99
use rustc_middle::hir::map::Map;
1010
use rustc_middle::hir::nested_filter;
1111
use rustc_middle::ty::TyCtxt;
12-
use rustc_parse::maybe_new_parser_from_source_str;
12+
use rustc_parse::new_parser_from_source_str;
1313
use rustc_parse::parser::attr::InnerAttrPolicy;
1414
use rustc_resolve::rustdoc::span_of_fragments;
1515
use rustc_session::config::{self, CrateType, ErrorOutputType};
@@ -638,7 +638,7 @@ pub(crate) fn make_test(
638638
let mut found_extern_crate = crate_name.is_none();
639639
let mut found_macro = false;
640640

641-
let mut parser = match maybe_new_parser_from_source_str(&psess, filename, source) {
641+
let mut parser = match new_parser_from_source_str(&psess, filename, source) {
642642
Ok(p) => p,
643643
Err(errs) => {
644644
errs.into_iter().for_each(|err| err.cancel());
@@ -818,16 +818,15 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
818818

819819
let dcx = DiagCtxt::new(Box::new(emitter)).disable_warnings();
820820
let psess = ParseSess::with_dcx(dcx, sm);
821-
let mut parser =
822-
match maybe_new_parser_from_source_str(&psess, filename, source.to_owned()) {
823-
Ok(p) => p,
824-
Err(errs) => {
825-
errs.into_iter().for_each(|err| err.cancel());
826-
// If there is an unclosed delimiter, an error will be returned by the
827-
// tokentrees.
828-
return false;
829-
}
830-
};
821+
let mut parser = match new_parser_from_source_str(&psess, filename, source.to_owned()) {
822+
Ok(p) => p,
823+
Err(errs) => {
824+
errs.into_iter().for_each(|err| err.cancel());
825+
// If there is an unclosed delimiter, an error will be returned by the
826+
// tokentrees.
827+
return false;
828+
}
829+
};
831830
// If a parsing error happened, it's very likely that the attribute is incomplete.
832831
if let Err(e) = parser.parse_attribute(InnerAttrPolicy::Permitted) {
833832
e.cancel();

‎src/librustdoc/passes/lint/check_code_block_syntax.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{
55
translation::{to_fluent_args, Translate},
66
Applicability, DiagCtxt, DiagInner, LazyFallbackBundle,
77
};
8-
use rustc_parse::parse_stream_from_source_str;
8+
use rustc_parse::{source_str_to_stream, unwrap_or_emit_fatal};
99
use rustc_resolve::rustdoc::source_span_for_markdown_range;
1010
use rustc_session::parse::ParseSess;
1111
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, Transparency};
@@ -51,12 +51,12 @@ fn check_rust_syntax(
5151
let span = DUMMY_SP.apply_mark(expn_id.to_expn_id(), Transparency::Transparent);
5252

5353
let is_empty = rustc_driver::catch_fatal_errors(|| {
54-
parse_stream_from_source_str(
54+
unwrap_or_emit_fatal(source_str_to_stream(
55+
&psess,
5556
FileName::Custom(String::from("doctest")),
5657
source,
57-
&psess,
5858
Some(span),
59-
)
59+
))
6060
.is_empty()
6161
})
6262
.unwrap_or(false);

‎src/tools/clippy/clippy_lints/src/doc/needless_doctest_main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::sync::Lrc;
88
use rustc_errors::emitter::HumanEmitter;
99
use rustc_errors::{Diag, DiagCtxt};
1010
use rustc_lint::LateContext;
11-
use rustc_parse::maybe_new_parser_from_source_str;
11+
use rustc_parse::new_parser_from_source_str;
1212
use rustc_parse::parser::ForceCollect;
1313
use rustc_session::parse::ParseSess;
1414
use rustc_span::edition::Edition;
@@ -50,7 +50,7 @@ pub fn check(
5050
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
5151
let psess = ParseSess::with_dcx(dcx, sm);
5252

53-
let mut parser = match maybe_new_parser_from_source_str(&psess, filename, code) {
53+
let mut parser = match new_parser_from_source_str(&psess, filename, code) {
5454
Ok(p) => p,
5555
Err(errs) => {
5656
errs.into_iter().for_each(Diag::cancel);

‎src/tools/rustfmt/src/parse/macros/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::token::{Delimiter, NonterminalKind, TokenKind};
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{ast, ptr};
44
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
5-
use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS};
5+
use rustc_parse::MACRO_ARGUMENTS;
66
use rustc_session::parse::ParseSess;
77
use rustc_span::symbol::{self, kw};
88
use rustc_span::Symbol;
@@ -15,7 +15,7 @@ pub(crate) mod cfg_if;
1515
pub(crate) mod lazy_static;
1616

1717
fn build_stream_parser<'a>(psess: &'a ParseSess, tokens: TokenStream) -> Parser<'a> {
18-
stream_to_parser(psess, tokens, MACRO_ARGUMENTS).recovery(Recovery::Forbidden)
18+
Parser::new(psess, tokens, MACRO_ARGUMENTS).recovery(Recovery::Forbidden)
1919
}
2020

2121
fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser<'a> {

‎src/tools/rustfmt/src/parse/parser.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::path::{Path, PathBuf};
44
use rustc_ast::token::TokenKind;
55
use rustc_ast::{ast, attr, ptr};
66
use rustc_errors::Diag;
7-
use rustc_parse::{new_parser_from_file, parser::Parser as RawParser};
7+
use rustc_parse::parser::Parser as RawParser;
8+
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
89
use rustc_span::{sym, Span};
910
use thin_vec::ThinVec;
1011

@@ -50,12 +51,9 @@ impl<'a> ParserBuilder<'a> {
5051

5152
let parser = match Self::parser(psess.inner(), input) {
5253
Ok(p) => p,
53-
Err(db) => {
54-
if let Some(diagnostics) = db {
55-
psess.emit_diagnostics(diagnostics);
56-
return Err(ParserError::ParserCreationError);
57-
}
58-
return Err(ParserError::ParsePanicError);
54+
Err(diagnostics) => {
55+
psess.emit_diagnostics(diagnostics);
56+
return Err(ParserError::ParserCreationError);
5957
}
6058
};
6159

@@ -65,18 +63,14 @@ impl<'a> ParserBuilder<'a> {
6563
fn parser(
6664
psess: &'a rustc_session::parse::ParseSess,
6765
input: Input,
68-
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diag<'a>>>> {
66+
) -> Result<RawParser<'a>, Vec<Diag<'a>>> {
6967
match input {
70-
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
71-
new_parser_from_file(psess, file, None)
72-
}))
73-
.map_err(|_| None),
74-
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
68+
Input::File(ref file) => new_parser_from_file(psess, file, None),
69+
Input::Text(text) => new_parser_from_source_str(
7570
psess,
7671
rustc_span::FileName::Custom("stdin".to_owned()),
7772
text,
78-
)
79-
.map_err(Some),
73+
),
8074
}
8175
}
8276
}
@@ -111,7 +105,8 @@ impl<'a> Parser<'a> {
111105
span: Span,
112106
) -> Result<(ast::AttrVec, ThinVec<ptr::P<ast::Item>>, Span), ParserError> {
113107
let result = catch_unwind(AssertUnwindSafe(|| {
114-
let mut parser = new_parser_from_file(psess.inner(), path, Some(span));
108+
let mut parser =
109+
unwrap_or_emit_fatal(new_parser_from_file(psess.inner(), path, Some(span)));
115110
match parser.parse_mod(&TokenKind::Eof) {
116111
Ok((a, i, spans)) => Some((a, i, spans.inner_span)),
117112
Err(e) => {

‎tests/ui-fulldeps/mod_dir_path_canonicalized.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate rustc_span;
1616
#[allow(unused_extern_crates)]
1717
extern crate rustc_driver;
1818

19-
use rustc_parse::new_parser_from_file;
19+
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal};
2020
use rustc_session::parse::ParseSess;
2121
use std::path::Path;
2222

@@ -34,6 +34,6 @@ fn parse() {
3434

3535
let path = Path::new(file!());
3636
let path = path.canonicalize().unwrap();
37-
let mut parser = new_parser_from_file(&psess, &path, None);
37+
let mut parser = unwrap_or_emit_fatal(new_parser_from_file(&psess, &path, None));
3838
let _ = parser.parse_crate_mod();
3939
}

‎tests/ui-fulldeps/pprust-expr-roundtrip.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_ast::mut_visit::{visit_clobber, MutVisitor};
3636
use rustc_ast::ptr::P;
3737
use rustc_ast::*;
3838
use rustc_ast_pretty::pprust;
39-
use rustc_parse::new_parser_from_source_str;
39+
use rustc_parse::{new_parser_from_source_str, unwrap_or_emit_fatal};
4040
use rustc_session::parse::ParseSess;
4141
use rustc_span::source_map::Spanned;
4242
use rustc_span::symbol::Ident;
@@ -46,8 +46,9 @@ use thin_vec::{thin_vec, ThinVec};
4646
fn parse_expr(psess: &ParseSess, src: &str) -> Option<P<Expr>> {
4747
let src_as_string = src.to_string();
4848

49-
let mut p =
50-
new_parser_from_source_str(psess, FileName::Custom(src_as_string.clone()), src_as_string);
49+
let mut p = unwrap_or_emit_fatal(
50+
new_parser_from_source_str(psess, FileName::Custom(src_as_string.clone()), src_as_string)
51+
);
5152
p.parse_expr().map_err(|e| e.cancel()).ok()
5253
}
5354

0 commit comments

Comments
 (0)
This repository has been archived.