Skip to content

Commit 6c10c85

Browse files
committed
Auto merge of rust-lang#17603 - Veykril:tt-symbols, r=Veykril
Switch token trees to use Symbols
2 parents a2c46b9 + 9ce066e commit 6c10c85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+713
-464
lines changed

Diff for: src/tools/rust-analyzer/Cargo.lock

+8-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ dependencies = [
146146
"arbitrary",
147147
"derive_arbitrary",
148148
"expect-test",
149+
"intern",
149150
"mbe",
150151
"oorandom",
151152
"rustc-hash",
@@ -1045,6 +1046,7 @@ version = "0.0.0"
10451046
dependencies = [
10461047
"arrayvec",
10471048
"cov-mark",
1049+
"intern",
10481050
"parser",
10491051
"rustc-hash",
10501052
"smallvec",
@@ -1324,8 +1326,8 @@ version = "0.0.0"
13241326
dependencies = [
13251327
"base-db",
13261328
"indexmap",
1329+
"intern",
13271330
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
1328-
"mbe",
13291331
"paths",
13301332
"rustc-hash",
13311333
"serde",
@@ -1343,6 +1345,7 @@ version = "0.0.0"
13431345
dependencies = [
13441346
"base-db",
13451347
"expect-test",
1348+
"intern",
13461349
"libloading",
13471350
"mbe",
13481351
"memmap2",
@@ -1413,6 +1416,7 @@ dependencies = [
14131416
"cargo_metadata",
14141417
"cfg",
14151418
"expect-test",
1419+
"intern",
14161420
"itertools",
14171421
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
14181422
"paths",
@@ -1653,6 +1657,7 @@ dependencies = [
16531657
"ide",
16541658
"ide-db",
16551659
"ide-ssr",
1660+
"intern",
16561661
"itertools",
16571662
"load-cargo",
16581663
"lsp-server 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1966,6 +1971,7 @@ dependencies = [
19661971
"base-db",
19671972
"cfg",
19681973
"hir-expand",
1974+
"intern",
19691975
"rustc-hash",
19701976
"span",
19711977
"stdx",
@@ -2218,8 +2224,8 @@ name = "tt"
22182224
version = "0.0.0"
22192225
dependencies = [
22202226
"arrayvec",
2227+
"intern",
22212228
"ra-ap-rustc_lexer",
2222-
"smol_str",
22232229
"stdx",
22242230
"text-size",
22252231
]

Diff for: src/tools/rust-analyzer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ debug = 2
5050
[workspace.dependencies]
5151
# local crates
5252
base-db = { path = "./crates/base-db", version = "0.0.0" }
53-
cfg = { path = "./crates/cfg", version = "0.0.0" }
53+
cfg = { path = "./crates/cfg", version = "0.0.0", features = ["tt"] }
5454
flycheck = { path = "./crates/flycheck", version = "0.0.0" }
5555
hir = { path = "./crates/hir", version = "0.0.0" }
5656
hir-def = { path = "./crates/hir-def", version = "0.0.0" }

Diff for: src/tools/rust-analyzer/crates/cfg/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ doctest = false
1515
rustc-hash.workspace = true
1616

1717
# locals deps
18-
tt.workspace = true
18+
tt = { workspace = true, optional = true }
19+
intern.workspace = true
1920

2021
[dev-dependencies]
2122
expect-test = "1.4.1"

Diff for: src/tools/rust-analyzer/crates/cfg/src/cfg_expr.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
//!
33
//! See: <https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation>
44
5-
use std::{fmt, slice::Iter as SliceIter};
5+
use std::fmt;
66

7-
use tt::SmolStr;
7+
use intern::Symbol;
88

99
/// A simple configuration value passed in from the outside.
10-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
10+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1111
pub enum CfgAtom {
1212
/// eg. `#[cfg(test)]`
13-
Flag(SmolStr),
13+
Flag(Symbol),
1414
/// eg. `#[cfg(target_os = "linux")]`
1515
///
1616
/// Note that a key can have multiple values that are all considered "active" at the same time.
1717
/// For example, `#[cfg(target_feature = "sse")]` and `#[cfg(target_feature = "sse2")]`.
18-
KeyValue { key: SmolStr, value: SmolStr },
18+
KeyValue { key: Symbol, value: Symbol },
1919
}
2020

2121
impl fmt::Display for CfgAtom {
@@ -44,6 +44,7 @@ impl From<CfgAtom> for CfgExpr {
4444
}
4545

4646
impl CfgExpr {
47+
#[cfg(feature = "tt")]
4748
pub fn parse<S>(tt: &tt::Subtree<S>) -> CfgExpr {
4849
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
4950
}
@@ -63,10 +64,14 @@ impl CfgExpr {
6364
}
6465
}
6566
}
66-
fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr> {
67+
68+
#[cfg(feature = "tt")]
69+
fn next_cfg_expr<S>(it: &mut std::slice::Iter<'_, tt::TokenTree<S>>) -> Option<CfgExpr> {
70+
use intern::sym;
71+
6772
let name = match it.next() {
6873
None => return None,
69-
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(),
74+
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
7075
Some(_) => return Some(CfgExpr::Invalid),
7176
};
7277

@@ -77,10 +82,7 @@ fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr>
7782
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
7883
it.next();
7984
it.next();
80-
// FIXME: escape? raw string?
81-
let value =
82-
SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"'));
83-
CfgAtom::KeyValue { key: name, value }.into()
85+
CfgAtom::KeyValue { key: name, value: literal.symbol.clone() }.into()
8486
}
8587
_ => return Some(CfgExpr::Invalid),
8688
}
@@ -89,10 +91,12 @@ fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr>
8991
it.next();
9092
let mut sub_it = subtree.token_trees.iter();
9193
let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect();
92-
match name.as_str() {
93-
"all" => CfgExpr::All(subs),
94-
"any" => CfgExpr::Any(subs),
95-
"not" => CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid))),
94+
match &name {
95+
s if *s == sym::all => CfgExpr::All(subs),
96+
s if *s == sym::any => CfgExpr::Any(subs),
97+
s if *s == sym::not => {
98+
CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid)))
99+
}
96100
_ => CfgExpr::Invalid,
97101
}
98102
}
@@ -112,11 +116,11 @@ fn next_cfg_expr<S>(it: &mut SliceIter<'_, tt::TokenTree<S>>) -> Option<CfgExpr>
112116
impl arbitrary::Arbitrary<'_> for CfgAtom {
113117
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
114118
if u.arbitrary()? {
115-
Ok(CfgAtom::Flag(String::arbitrary(u)?.into()))
119+
Ok(CfgAtom::Flag(Symbol::intern(<_>::arbitrary(u)?)))
116120
} else {
117121
Ok(CfgAtom::KeyValue {
118-
key: String::arbitrary(u)?.into(),
119-
value: String::arbitrary(u)?.into(),
122+
key: Symbol::intern(<_>::arbitrary(u)?),
123+
value: Symbol::intern(<_>::arbitrary(u)?),
120124
})
121125
}
122126
}

Diff for: src/tools/rust-analyzer/crates/cfg/src/dnf.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl DnfExpr {
6666
}
6767
}
6868

69-
res.enabled.sort_unstable();
69+
res.enabled.sort_unstable_by(compare);
7070
res.enabled.dedup();
71-
res.disabled.sort_unstable();
71+
res.disabled.sort_unstable_by(compare);
7272
res.disabled.dedup();
7373
Some(res)
7474
}
@@ -114,14 +114,25 @@ impl DnfExpr {
114114
};
115115

116116
// Undo the FxHashMap randomization for consistent output.
117-
diff.enable.sort_unstable();
118-
diff.disable.sort_unstable();
117+
diff.enable.sort_unstable_by(compare);
118+
diff.disable.sort_unstable_by(compare);
119119

120120
Some(diff)
121121
})
122122
}
123123
}
124124

125+
fn compare(a: &CfgAtom, b: &CfgAtom) -> std::cmp::Ordering {
126+
match (a, b) {
127+
(CfgAtom::Flag(a), CfgAtom::Flag(b)) => a.as_str().cmp(b.as_str()),
128+
(CfgAtom::Flag(_), CfgAtom::KeyValue { .. }) => std::cmp::Ordering::Less,
129+
(CfgAtom::KeyValue { .. }, CfgAtom::Flag(_)) => std::cmp::Ordering::Greater,
130+
(CfgAtom::KeyValue { key, value }, CfgAtom::KeyValue { key: key2, value: value2 }) => {
131+
key.as_str().cmp(key2.as_str()).then(value.as_str().cmp(value2.as_str()))
132+
}
133+
}
134+
}
135+
125136
impl fmt::Display for DnfExpr {
126137
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127138
if self.conjunctions.len() != 1 {

Diff for: src/tools/rust-analyzer/crates/cfg/src/lib.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ mod tests;
88
use std::fmt;
99

1010
use rustc_hash::FxHashSet;
11-
use tt::SmolStr;
11+
12+
use intern::Symbol;
1213

1314
pub use cfg_expr::{CfgAtom, CfgExpr};
1415
pub use dnf::DnfExpr;
@@ -48,11 +49,11 @@ impl CfgOptions {
4849
cfg.fold(&|atom| self.enabled.contains(atom))
4950
}
5051

51-
pub fn insert_atom(&mut self, key: SmolStr) {
52+
pub fn insert_atom(&mut self, key: Symbol) {
5253
self.enabled.insert(CfgAtom::Flag(key));
5354
}
5455

55-
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
56+
pub fn insert_key_value(&mut self, key: Symbol, value: Symbol) {
5657
self.enabled.insert(CfgAtom::KeyValue { key, value });
5758
}
5859

@@ -66,19 +67,16 @@ impl CfgOptions {
6667
}
6768
}
6869

69-
pub fn get_cfg_keys(&self) -> impl Iterator<Item = &SmolStr> {
70+
pub fn get_cfg_keys(&self) -> impl Iterator<Item = &Symbol> {
7071
self.enabled.iter().map(|it| match it {
7172
CfgAtom::Flag(key) => key,
7273
CfgAtom::KeyValue { key, .. } => key,
7374
})
7475
}
7576

76-
pub fn get_cfg_values<'a>(
77-
&'a self,
78-
cfg_key: &'a str,
79-
) -> impl Iterator<Item = &'a SmolStr> + 'a {
77+
pub fn get_cfg_values<'a>(&'a self, cfg_key: &'a str) -> impl Iterator<Item = &'a Symbol> + 'a {
8078
self.enabled.iter().filter_map(move |it| match it {
81-
CfgAtom::KeyValue { key, value } if cfg_key == key => Some(value),
79+
CfgAtom::KeyValue { key, value } if cfg_key == key.as_str() => Some(value),
8280
_ => None,
8381
})
8482
}

Diff for: src/tools/rust-analyzer/crates/cfg/src/tests.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
3+
use intern::Symbol;
34
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode, DummyTestSpanMap, DUMMY};
45
use syntax::{ast, AstNode, Edition};
56

@@ -65,22 +66,25 @@ fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
6566

6667
#[test]
6768
fn test_cfg_expr_parser() {
68-
assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into());
69-
assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into());
69+
assert_parse_result("#![cfg(foo)]", CfgAtom::Flag(Symbol::intern("foo")).into());
70+
assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag(Symbol::intern("foo")).into());
7071
assert_parse_result(
7172
"#![cfg(not(foo))]",
72-
CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())),
73+
CfgExpr::Not(Box::new(CfgAtom::Flag(Symbol::intern("foo")).into())),
7374
);
7475
assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid);
7576

7677
// Only take the first
77-
assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into());
78+
assert_parse_result(
79+
r#"#![cfg(foo, bar = "baz")]"#,
80+
CfgAtom::Flag(Symbol::intern("foo")).into(),
81+
);
7882

7983
assert_parse_result(
8084
r#"#![cfg(all(foo, bar = "baz"))]"#,
8185
CfgExpr::All(vec![
82-
CfgAtom::Flag("foo".into()).into(),
83-
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
86+
CfgAtom::Flag(Symbol::intern("foo")).into(),
87+
CfgAtom::KeyValue { key: Symbol::intern("bar"), value: Symbol::intern("baz") }.into(),
8488
]),
8589
);
8690

@@ -90,7 +94,7 @@ fn test_cfg_expr_parser() {
9094
CfgExpr::Not(Box::new(CfgExpr::Invalid)),
9195
CfgExpr::All(vec![]),
9296
CfgExpr::Invalid,
93-
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
97+
CfgAtom::KeyValue { key: Symbol::intern("bar"), value: Symbol::intern("baz") }.into(),
9498
]),
9599
);
96100
}
@@ -167,7 +171,7 @@ fn hints() {
167171

168172
check_enable_hints("#![cfg(all(a, b))]", &opts, &["enable a and b"]);
169173

170-
opts.insert_atom("test".into());
174+
opts.insert_atom(Symbol::intern("test"));
171175

172176
check_enable_hints("#![cfg(test)]", &opts, &[]);
173177
check_enable_hints("#![cfg(not(test))]", &opts, &["disable test"]);
@@ -180,16 +184,16 @@ fn hints_impossible() {
180184

181185
check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]);
182186

183-
opts.insert_atom("test".into());
187+
opts.insert_atom(Symbol::intern("test"));
184188

185189
check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]);
186190
}
187191

188192
#[test]
189193
fn why_inactive() {
190194
let mut opts = CfgOptions::default();
191-
opts.insert_atom("test".into());
192-
opts.insert_atom("test2".into());
195+
opts.insert_atom(Symbol::intern("test"));
196+
opts.insert_atom(Symbol::intern("test2"));
193197

194198
check_why_inactive("#![cfg(a)]", &opts, expect![["a is disabled"]]);
195199
check_why_inactive("#![cfg(not(test))]", &opts, expect![["test is enabled"]]);

0 commit comments

Comments
 (0)