Inline and remove parse_crate{,_attrs}_from_{file,source_str}.

All four functions are simple and have a single call site.

This requires making `Parser::parse_inner_attributes` public, which is
no big deal.
This commit is contained in:
Nicholas Nethercote 2024-05-31 13:56:44 +10:00
parent 8964106e44
commit 25972aec67
4 changed files with 16 additions and 40 deletions

View File

@ -32,6 +32,7 @@
use rustc_lint::unerased_lint_store; use rustc_lint::unerased_lint_store;
use rustc_metadata::creader::MetadataLoader; use rustc_metadata::creader::MetadataLoader;
use rustc_metadata::locator; use rustc_metadata::locator;
use rustc_parse::{new_parser_from_file, new_parser_from_source_str};
use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS}; use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType}; use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType};
use rustc_session::getopts::{self, Matches}; use rustc_session::getopts::{self, Matches};
@ -1264,12 +1265,13 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
} }
fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> { fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
match &sess.io.input { let mut parser = match &sess.io.input {
Input::File(ifile) => rustc_parse::parse_crate_attrs_from_file(ifile, &sess.psess), Input::File(file) => new_parser_from_file(&sess.psess, file, None),
Input::Str { name, input } => { Input::Str { name, input } => {
rustc_parse::parse_crate_attrs_from_source_str(name.clone(), input.clone(), &sess.psess) new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
} }
} };
parser.parse_inner_attributes()
} }
/// Runs a closure and catches unwinds triggered by fatal errors. /// Runs a closure and catches unwinds triggered by fatal errors.

View File

@ -19,7 +19,7 @@
use rustc_middle::dep_graph::DepGraph; use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt}; use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_middle::util::Providers; use rustc_middle::util::Providers;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr}; use rustc_parse::{new_parser_from_file, new_parser_from_source_str, validate_attr};
use rustc_passes::{abi_test, hir_stats, layout_test}; use rustc_passes::{abi_test, hir_stats, layout_test};
use rustc_resolve::Resolver; use rustc_resolve::Resolver;
use rustc_session::code_stats::VTableSizeInfo; use rustc_session::code_stats::VTableSizeInfo;
@ -42,11 +42,14 @@
use tracing::{info, instrument}; use tracing::{info, instrument};
pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> { pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> {
let krate = sess.time("parse_crate", || match &sess.io.input { let krate = sess.time("parse_crate", || {
Input::File(file) => parse_crate_from_file(file, &sess.psess), let mut parser = match &sess.io.input {
Input::Str { input, name } => { Input::File(file) => new_parser_from_file(&sess.psess, file, None),
parse_crate_from_source_str(name.clone(), input.clone(), &sess.psess) Input::Str { input, name } => {
} new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
}
};
parser.parse_crate_mod()
})?; })?;
if sess.opts.unstable_opts.input_stats { if sess.opts.unstable_opts.input_stats {

View File

@ -47,35 +47,6 @@ fn unwrap_or_emit_fatal<T>(expr: Result<T, Vec<Diag<'_>>>) -> T {
} }
} }
pub fn parse_crate_from_file<'a>(input: &Path, psess: &'a ParseSess) -> PResult<'a, ast::Crate> {
let mut parser = new_parser_from_file(psess, input, None);
parser.parse_crate_mod()
}
pub fn parse_crate_attrs_from_file<'a>(
input: &Path,
psess: &'a ParseSess,
) -> PResult<'a, ast::AttrVec> {
let mut parser = new_parser_from_file(psess, input, None);
parser.parse_inner_attributes()
}
pub fn parse_crate_from_source_str(
name: FileName,
source: String,
psess: &ParseSess,
) -> PResult<'_, ast::Crate> {
new_parser_from_source_str(psess, name, source).parse_crate_mod()
}
pub fn parse_crate_attrs_from_source_str(
name: FileName,
source: String,
psess: &ParseSess,
) -> PResult<'_, ast::AttrVec> {
new_parser_from_source_str(psess, name, source).parse_inner_attributes()
}
/// Creates a new parser from a source string. /// Creates a new parser from a source string.
pub fn new_parser_from_source_str(psess: &ParseSess, name: FileName, source: String) -> Parser<'_> { pub fn new_parser_from_source_str(psess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
unwrap_or_emit_fatal(maybe_new_parser_from_source_str(psess, name, source)) unwrap_or_emit_fatal(maybe_new_parser_from_source_str(psess, name, source))

View File

@ -265,7 +265,7 @@ pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::Attr
/// terminated by a semicolon. /// terminated by a semicolon.
/// ///
/// Matches `inner_attrs*`. /// Matches `inner_attrs*`.
pub(crate) fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> { pub fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
let mut attrs = ast::AttrVec::new(); let mut attrs = ast::AttrVec::new();
loop { loop {
let start_pos: u32 = self.num_bump_calls.try_into().unwrap(); let start_pos: u32 = self.num_bump_calls.try_into().unwrap();