Skip to content

Commit 8e4ac98

Browse files
committed
Change cfg parsers to produce symbols instead of strings.
1 parent bfcff79 commit 8e4ac98

File tree

4 files changed

+16
-54
lines changed

4 files changed

+16
-54
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_session::Session;
2424
use rustc_session::{lint, EarlyErrorHandler};
2525
use rustc_span::source_map::{FileLoader, FileName};
2626
use rustc_span::symbol::sym;
27+
use rustc_span::Symbol;
2728
use std::path::PathBuf;
2829
use std::result;
2930
use std::sync::Arc;
@@ -64,7 +65,7 @@ impl Compiler {
6465
}
6566

6667
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
67-
pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<String> {
68+
pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<Symbol> {
6869
cfgs.into_iter()
6970
.map(|s| {
7071
let sess = ParseSess::with_silent_emitter(Some(format!(
@@ -94,10 +95,7 @@ pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<S
9495
}
9596
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
9697
let ident = meta_item.ident().expect("multi-segment cfg key");
97-
return (
98-
ident.name.to_string(),
99-
meta_item.value_str().map(|sym| sym.to_string()),
100-
);
98+
return (ident.name, meta_item.value_str());
10199
}
102100
}
103101
}
@@ -118,11 +116,11 @@ pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg<S
118116
error!(r#"expected `key` or `key="value"`"#);
119117
}
120118
})
121-
.collect::<Cfg<String>>()
119+
.collect::<Cfg<Symbol>>()
122120
}
123121

124122
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
125-
pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> CheckCfg<String> {
123+
pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> CheckCfg<Symbol> {
126124
// If any --check-cfg is passed then exhaustive_values and exhaustive_names
127125
// are enabled by default.
128126
let exhaustive_names = !specs.is_empty();
@@ -179,10 +177,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
179177
for arg in args {
180178
if arg.is_word() && arg.ident().is_some() {
181179
let ident = arg.ident().expect("multi-segment cfg key");
182-
check_cfg
183-
.expecteds
184-
.entry(ident.name.to_string())
185-
.or_insert(ExpectedValues::Any);
180+
check_cfg.expecteds.entry(ident.name).or_insert(ExpectedValues::Any);
186181
} else {
187182
error!("`names()` arguments must be simple identifiers");
188183
}
@@ -200,7 +195,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
200195
let ident = name.ident().expect("multi-segment cfg key");
201196
let expected_values = check_cfg
202197
.expecteds
203-
.entry(ident.name.to_string())
198+
.entry(ident.name)
204199
.and_modify(|expected_values| match expected_values {
205200
ExpectedValues::Some(_) => {}
206201
ExpectedValues::Any => {
@@ -217,7 +212,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
217212

218213
for val in values {
219214
if let Some(LitKind::Str(s, _)) = val.lit().map(|lit| &lit.kind) {
220-
expected_values.insert(Some(s.to_string()));
215+
expected_values.insert(Some(*s));
221216
} else {
222217
error!("`values()` arguments must be string literals");
223218
}
@@ -271,10 +266,8 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
271266
values_specified = true;
272267

273268
for arg in args {
274-
if let Some(LitKind::Str(s, _)) =
275-
arg.lit().map(|lit| &lit.kind)
276-
{
277-
values.insert(Some(s.to_string()));
269+
if let Some(LitKind::Str(s, _)) = arg.lit().map(|lit| &lit.kind) {
270+
values.insert(Some(*s));
278271
} else if arg.has_name(sym::any)
279272
&& let Some(args) = arg.meta_item_list()
280273
{
@@ -322,7 +315,7 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
322315
for name in names {
323316
check_cfg
324317
.expecteds
325-
.entry(name.to_string())
318+
.entry(name.name)
326319
.and_modify(|v| match v {
327320
ExpectedValues::Some(v) if !values_any_specified => {
328321
v.extend(values.clone())

compiler/rustc_interface/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use rustc_session::{build_session, getopts, Session};
2626
use rustc_session::{CompilerIO, EarlyErrorHandler};
2727
use rustc_span::edition::{Edition, DEFAULT_EDITION};
2828
use rustc_span::symbol::sym;
29-
use rustc_span::FileName;
3029
use rustc_span::SourceFileHashAlgorithm;
30+
use rustc_span::{FileName, Symbol};
3131
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
3232
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
3333
use std::collections::{BTreeMap, BTreeSet};
@@ -38,7 +38,7 @@ use std::sync::Arc;
3838
fn mk_session(
3939
handler: &mut EarlyErrorHandler,
4040
matches: getopts::Matches,
41-
) -> (Session, Cfg<String>) {
41+
) -> (Session, Cfg<Symbol>) {
4242
let registry = registry::Registry::new(&[]);
4343
let sessopts = build_session_options(handler, &matches);
4444
let cfg = parse_cfg(handler, matches.opt_strs("cfg"));

compiler/rustc_interface/src/util.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub fn add_configuration(
5959
pub fn create_session(
6060
handler: &EarlyErrorHandler,
6161
sopts: config::Options,
62-
cfg: Cfg<String>,
63-
check_cfg: CheckCfg<String>,
62+
cfg: Cfg<Symbol>,
63+
mut check_cfg: CheckCfg<Symbol>,
6464
locale_resources: &'static [&'static str],
6565
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
6666
io: CompilerIO,
@@ -123,7 +123,6 @@ pub fn create_session(
123123
let mut cfg = config::build_configuration(&sess, cfg);
124124
add_configuration(&mut cfg, &mut sess, &*codegen_backend);
125125

126-
let mut check_cfg = check_cfg.intern();
127126
check_cfg.fill_well_known(&sess.target);
128127

129128
// These configs use symbols, rather than strings.

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,30 +1386,6 @@ impl<T> Default for CheckCfg<T> {
13861386
}
13871387
}
13881388

1389-
impl CheckCfg<String> {
1390-
pub fn intern(self) -> CheckCfg<Symbol> {
1391-
CheckCfg {
1392-
exhaustive_names: self.exhaustive_names,
1393-
exhaustive_values: self.exhaustive_values,
1394-
expecteds: self
1395-
.expecteds
1396-
.into_iter()
1397-
.map(|(name, values)| {
1398-
(
1399-
Symbol::intern(&name),
1400-
match values {
1401-
ExpectedValues::Some(values) => ExpectedValues::Some(
1402-
values.into_iter().map(|b| b.map(|b| Symbol::intern(&b))).collect(),
1403-
),
1404-
ExpectedValues::Any => ExpectedValues::Any,
1405-
},
1406-
)
1407-
})
1408-
.collect(),
1409-
}
1410-
}
1411-
}
1412-
14131389
pub enum ExpectedValues<T> {
14141390
Some(FxHashSet<Option<T>>),
14151391
Any,
@@ -1582,13 +1558,7 @@ impl CheckCfg<Symbol> {
15821558
}
15831559
}
15841560

1585-
pub fn build_configuration(sess: &Session, user_cfg: Cfg<String>) -> Cfg<Symbol> {
1586-
// We can now intern these strings.
1587-
let mut user_cfg: Cfg<Symbol> = user_cfg
1588-
.into_iter()
1589-
.map(|(a, b)| (Symbol::intern(&a), b.map(|b| Symbol::intern(&b))))
1590-
.collect();
1591-
1561+
pub fn build_configuration(sess: &Session, mut user_cfg: Cfg<Symbol>) -> Cfg<Symbol> {
15921562
// Combine the configuration requested by the session (command line) with
15931563
// some default and generated configuration items.
15941564
let default_cfg = default_configuration(sess);

0 commit comments

Comments
 (0)