Delay parsing of --cfg and --check-cfg options.

By storing the unparsed values in `Config` and then parsing them within
`run_compiler`, the parsing functions can use the main symbol interner,
and not create their own short-lived interners.

This change also eliminates the need for one `EarlyErrorHandler` in
rustdoc, because parsing errors can be reported by another, slightly
later `EarlyErrorHandler`.
This commit is contained in:
Nicholas Nethercote 2023-10-30 13:37:34 +11:00
parent ec2b311914
commit 678e01a3fc
5 changed files with 271 additions and 289 deletions

View file

@ -317,13 +317,11 @@ fn run_compiler(
return Ok(());
}
let cfg = interface::parse_cfg(&early_error_handler, matches.opt_strs("cfg"));
let check_cfg = interface::parse_check_cfg(&early_error_handler, matches.opt_strs("check-cfg"));
let (odir, ofile) = make_output(&matches);
let mut config = interface::Config {
opts: sopts,
crate_cfg: cfg,
crate_check_cfg: check_cfg,
crate_cfg: matches.opt_strs("cfg"),
crate_check_cfg: matches.opt_strs("check-cfg"),
input: Input::File(PathBuf::new()),
output_file: ofile,
output_dir: odir,

View file

@ -65,10 +65,6 @@ impl Compiler {
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
pub fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<String> {
// This creates a short-lived `SessionGlobals`, containing an interner. The
// parsed values are converted from symbols to strings before exiting
// because the symbols are meaningless once the interner is gone.
rustc_span::create_default_session_if_not_set_then(move |_| {
cfgs.into_iter()
.map(|s| {
let sess = ParseSess::with_silent_emitter(Some(format!(
@ -123,14 +119,10 @@ pub fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<String>
}
})
.collect::<Cfg<String>>()
})
}
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
pub fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> CheckCfg<String> {
// The comment about `SessionGlobals` and symbols in `parse_cfg` above
// applies here too.
rustc_span::create_default_session_if_not_set_then(move |_| {
// If any --check-cfg is passed then exhaustive_values and exhaustive_names
// are enabled by default.
let exhaustive_names = !specs.is_empty();
@ -353,7 +345,6 @@ pub fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> Check
}
check_cfg
})
}
/// The compiler configuration
@ -361,9 +352,9 @@ pub struct Config {
/// Command line options
pub opts: config::Options,
/// cfg! configuration in addition to the default ones
pub crate_cfg: Cfg<String>,
pub crate_check_cfg: CheckCfg<String>,
/// Unparsed cfg! configuration in addition to the default ones.
pub crate_cfg: Vec<String>,
pub crate_check_cfg: Vec<String>,
pub input: Input,
pub output_dir: Option<PathBuf>,
@ -436,8 +427,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
let (mut sess, codegen_backend) = util::create_session(
&handler,
config.opts,
config.crate_cfg,
config.crate_check_cfg,
parse_cfg(&handler, config.crate_cfg),
parse_check_cfg(&handler, config.crate_check_cfg),
config.locale_resources,
config.file_loader,
CompilerIO {

View file

@ -14,8 +14,8 @@ use rustc_lint::{late_lint_mod, MissingDoc};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks};
use rustc_session::lint;
use rustc_session::Session;
use rustc_session::{lint, EarlyErrorHandler};
use rustc_span::symbol::sym;
use rustc_span::{source_map, Span};
@ -175,7 +175,6 @@ pub(crate) fn new_handler(
/// Parse, resolve, and typecheck the given crate.
pub(crate) fn create_config(
handler: &EarlyErrorHandler,
RustdocOptions {
input,
crate_name,
@ -255,8 +254,8 @@ pub(crate) fn create_config(
interface::Config {
opts: sessopts,
crate_cfg: interface::parse_cfg(handler, cfgs),
crate_check_cfg: interface::parse_check_cfg(handler, check_cfgs),
crate_cfg: cfgs,
crate_check_cfg: check_cfgs,
input,
output_file: None,
output_dir: None,

View file

@ -13,7 +13,7 @@ use rustc_parse::parser::attr::InnerAttrPolicy;
use rustc_resolve::rustdoc::span_of_fragments;
use rustc_session::config::{self, CrateType, ErrorOutputType};
use rustc_session::parse::ParseSess;
use rustc_session::{lint, EarlyErrorHandler, Session};
use rustc_session::{lint, Session};
use rustc_span::edition::Edition;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::sym;
@ -85,18 +85,13 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
..config::Options::default()
};
let early_error_handler = EarlyErrorHandler::new(ErrorOutputType::default());
let mut cfgs = options.cfgs.clone();
cfgs.push("doc".to_owned());
cfgs.push("doctest".to_owned());
let config = interface::Config {
opts: sessopts,
crate_cfg: interface::parse_cfg(&early_error_handler, cfgs),
crate_check_cfg: interface::parse_check_cfg(
&early_error_handler,
options.check_cfgs.clone(),
),
crate_cfg: cfgs,
crate_check_cfg: options.check_cfgs.clone(),
input,
output_file: None,
output_dir: None,

View file

@ -757,8 +757,7 @@ fn main_args(
(false, true) => {
let input = options.input.clone();
let edition = options.edition;
let config =
core::create_config(handler, options, &render_options, using_internal_features);
let config = core::create_config(options, &render_options, using_internal_features);
// `markdown::render` can invoke `doctest::make_test`, which
// requires session globals and a thread pool, so we use
@ -791,7 +790,7 @@ fn main_args(
let scrape_examples_options = options.scrape_examples_options.clone();
let bin_crate = options.bin_crate;
let config = core::create_config(handler, options, &render_options, using_internal_features);
let config = core::create_config(options, &render_options, using_internal_features);
interface::run_compiler(config, |compiler| {
let sess = compiler.session();