Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main interner is running. They each use a short-lived interner and convert all interned symbols to strings in their output data structures. Once the main interner starts up, these data structures get converted into new data structures that are identical except with the strings converted to symbols. All is not obvious from the current code, which is a mess, particularly with inconsistent naming that obscures the parallel string/symbol data structures. This commit clean things up a lot. - The existing `CheckCfg` type is generic, allowing both `CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really useful, but it defaults to `String`. The commit removes the default so we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly, which makes things clearer. - Introduces `Cfg`, which is generic over `String` and `Symbol`, similar to `CheckCfg`. - Renames some things. - `parse_cfgspecs` -> `parse_cfg` - `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather than the underlying `FxHashSet` type. - `CrateConfig` -> `Cfg<Symbol>`. - `CrateCheckConfig` -> `CheckCfg<Symbol>` - Adds some comments explaining the string-to-symbol conversions. - `to_crate_check_config`, which converts `CheckCfg<String>` to `CheckCfg<Symbol>`, is inlined and removed and combined with the overly-general `CheckCfg::map_data` to produce `CheckCfg::<String>::intern`. - `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>` conversion, so callers don't need to, which removes the need for `to_crate_config`. The diff for two of the fields in `Config` is a good example of the improved clarity: ``` - pub crate_cfg: FxHashSet<(String, Option<String>)>, - pub crate_check_cfg: CheckCfg, + pub crate_cfg: Cfg<String>, + pub crate_check_cfg: CheckCfg<String>, ``` Compare that with the diff for the corresponding fields in `ParseSess`, and the relationship to `Config` is much clearer than before: ``` - pub config: CrateConfig, - pub check_config: CrateCheckConfig, + pub config: Cfg<Symbol>, + pub check_config: CheckCfg<Symbol>, ```
This commit is contained in:
parent
75e415ba86
commit
5e54997157
8 changed files with 65 additions and 71 deletions
|
@ -15,7 +15,9 @@ use rustc_middle::{bug, ty};
|
|||
use rustc_parse::maybe_new_parser_from_source_str;
|
||||
use rustc_query_impl::QueryCtxt;
|
||||
use rustc_query_system::query::print_query_stack;
|
||||
use rustc_session::config::{self, CheckCfg, ExpectedValues, Input, OutFileName, OutputFilenames};
|
||||
use rustc_session::config::{
|
||||
self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName, OutputFilenames,
|
||||
};
|
||||
use rustc_session::parse::ParseSess;
|
||||
use rustc_session::CompilerIO;
|
||||
use rustc_session::Session;
|
||||
|
@ -61,14 +63,13 @@ impl Compiler {
|
|||
}
|
||||
}
|
||||
|
||||
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
|
||||
pub fn parse_cfgspecs(
|
||||
handler: &EarlyErrorHandler,
|
||||
cfgspecs: Vec<String>,
|
||||
) -> FxHashSet<(String, Option<String>)> {
|
||||
/// 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 |_| {
|
||||
cfgspecs
|
||||
.into_iter()
|
||||
cfgs.into_iter()
|
||||
.map(|s| {
|
||||
let sess = ParseSess::with_silent_emitter(Some(format!(
|
||||
"this error occurred on the command line: `--cfg={s}`"
|
||||
|
@ -121,12 +122,14 @@ pub fn parse_cfgspecs(
|
|||
error!(r#"expected `key` or `key="value"`"#);
|
||||
}
|
||||
})
|
||||
.collect::<FxHashSet<_>>()
|
||||
.collect::<Cfg<String>>()
|
||||
})
|
||||
}
|
||||
|
||||
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
|
||||
pub fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> 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.
|
||||
|
@ -374,8 +377,8 @@ pub struct Config {
|
|||
pub opts: config::Options,
|
||||
|
||||
/// cfg! configuration in addition to the default ones
|
||||
pub crate_cfg: FxHashSet<(String, Option<String>)>,
|
||||
pub crate_check_cfg: CheckCfg,
|
||||
pub crate_cfg: Cfg<String>,
|
||||
pub crate_check_cfg: CheckCfg<String>,
|
||||
|
||||
pub input: Input,
|
||||
pub output_dir: Option<PathBuf>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue