Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More rustc_interface cleanups #117376

Merged
merged 11 commits into from
Oct 30, 2023
Next Next commit
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`.
nnethercote committed Oct 30, 2023

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
commit 678e01a3fc1d21eb8ea4b281b3905c6d3340ce54
6 changes: 2 additions & 4 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Original file line Diff line number Diff line change
@@ -317,13 +317,11 @@ fn run_compiler(
return Ok(()); 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 (odir, ofile) = make_output(&matches);
let mut config = interface::Config { let mut config = interface::Config {
opts: sopts, opts: sopts,
crate_cfg: cfg, crate_cfg: matches.opt_strs("cfg"),
crate_check_cfg: check_cfg, crate_check_cfg: matches.opt_strs("check-cfg"),
input: Input::File(PathBuf::new()), input: Input::File(PathBuf::new()),
output_file: ofile, output_file: ofile,
output_dir: odir, output_dir: odir,
19 changes: 5 additions & 14 deletions compiler/rustc_interface/src/interface.rs
Original file line number Original file line Diff line number Diff line change
@@ -65,10 +65,6 @@ impl Compiler {


/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`. /// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
pub fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<String> { 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() cfgs.into_iter()
.map(|s| { .map(|s| {
let sess = ParseSess::with_silent_emitter(Some(format!( 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>>() .collect::<Cfg<String>>()
})
} }


/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`. /// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
pub fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> CheckCfg<String> { 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 // If any --check-cfg is passed then exhaustive_values and exhaustive_names
// are enabled by default. // are enabled by default.
let exhaustive_names = !specs.is_empty(); let exhaustive_names = !specs.is_empty();
@@ -353,17 +345,16 @@ pub fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> Check
} }


check_cfg check_cfg
})
} }


/// The compiler configuration /// The compiler configuration
pub struct Config { pub struct Config {
/// Command line options /// Command line options
pub opts: config::Options, pub opts: config::Options,


/// cfg! configuration in addition to the default ones /// Unparsed cfg! configuration in addition to the default ones.
pub crate_cfg: Cfg<String>, pub crate_cfg: Vec<String>,
pub crate_check_cfg: CheckCfg<String>, pub crate_check_cfg: Vec<String>,


pub input: Input, pub input: Input,
pub output_dir: Option<PathBuf>, 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( let (mut sess, codegen_backend) = util::create_session(
&handler, &handler,
config.opts, config.opts,
config.crate_cfg, parse_cfg(&handler, config.crate_cfg),
config.crate_check_cfg, parse_check_cfg(&handler, config.crate_check_cfg),
config.locale_resources, config.locale_resources,
config.file_loader, config.file_loader,
CompilerIO { CompilerIO {
7 changes: 3 additions & 4 deletions src/librustdoc/core.rs
Original file line number Original file line Diff line number Diff line change
@@ -14,8 +14,8 @@ use rustc_lint::{late_lint_mod, MissingDoc};
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt}; use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks}; use rustc_session::config::{self, CrateType, ErrorOutputType, ResolveDocLinks};
use rustc_session::lint;
use rustc_session::Session; use rustc_session::Session;
use rustc_session::{lint, EarlyErrorHandler};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::{source_map, Span}; use rustc_span::{source_map, Span};


@@ -175,7 +175,6 @@ pub(crate) fn new_handler(


/// Parse, resolve, and typecheck the given crate. /// Parse, resolve, and typecheck the given crate.
pub(crate) fn create_config( pub(crate) fn create_config(
handler: &EarlyErrorHandler,
RustdocOptions { RustdocOptions {
input, input,
crate_name, crate_name,
@@ -255,8 +254,8 @@ pub(crate) fn create_config(


interface::Config { interface::Config {
opts: sessopts, opts: sessopts,
crate_cfg: interface::parse_cfg(handler, cfgs), crate_cfg: cfgs,
crate_check_cfg: interface::parse_check_cfg(handler, check_cfgs), crate_check_cfg: check_cfgs,
input, input,
output_file: None, output_file: None,
output_dir: None, output_dir: None,
11 changes: 3 additions & 8 deletions src/librustdoc/doctest.rs
Original file line number Original file line Diff line number Diff line change
@@ -13,7 +13,7 @@ use rustc_parse::parser::attr::InnerAttrPolicy;
use rustc_resolve::rustdoc::span_of_fragments; use rustc_resolve::rustdoc::span_of_fragments;
use rustc_session::config::{self, CrateType, ErrorOutputType}; use rustc_session::config::{self, CrateType, ErrorOutputType};
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_session::{lint, EarlyErrorHandler, Session}; use rustc_session::{lint, Session};
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@@ -85,18 +85,13 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
..config::Options::default() ..config::Options::default()
}; };


let early_error_handler = EarlyErrorHandler::new(ErrorOutputType::default());

let mut cfgs = options.cfgs.clone(); let mut cfgs = options.cfgs.clone();
cfgs.push("doc".to_owned()); cfgs.push("doc".to_owned());
cfgs.push("doctest".to_owned()); cfgs.push("doctest".to_owned());
let config = interface::Config { let config = interface::Config {
opts: sessopts, opts: sessopts,
crate_cfg: interface::parse_cfg(&early_error_handler, cfgs), crate_cfg: cfgs,
crate_check_cfg: interface::parse_check_cfg( crate_check_cfg: options.check_cfgs.clone(),
&early_error_handler,
options.check_cfgs.clone(),
),
input, input,
output_file: None, output_file: None,
output_dir: None, output_dir: None,
5 changes: 2 additions & 3 deletions src/librustdoc/lib.rs
Original file line number Original file line Diff line number Diff line change
@@ -757,8 +757,7 @@ fn main_args(
(false, true) => { (false, true) => {
let input = options.input.clone(); let input = options.input.clone();
let edition = options.edition; let edition = options.edition;
let config = let config = core::create_config(options, &render_options, using_internal_features);
core::create_config(handler, options, &render_options, using_internal_features);


// `markdown::render` can invoke `doctest::make_test`, which // `markdown::render` can invoke `doctest::make_test`, which
// requires session globals and a thread pool, so we use // 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 scrape_examples_options = options.scrape_examples_options.clone();
let bin_crate = options.bin_crate; 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| { interface::run_compiler(config, |compiler| {
let sess = compiler.session(); let sess = compiler.session();