Skip to content

Commit 6eae659

Browse files
committed
Use IndexMap to keep track of pre-interned symbol indices
1 parent 34e14ef commit 6eae659

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4152,6 +4152,7 @@ dependencies = [
41524152
name = "rustc_macros"
41534153
version = "0.0.0"
41544154
dependencies = [
4155+
"indexmap 2.0.0",
41554156
"proc-macro2",
41564157
"quote",
41574158
"syn 2.0.29",

compiler/rustc_macros/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ edition = "2021"
77
proc-macro = true
88

99
[dependencies]
10-
synstructure = "0.13.0"
11-
syn = { version = "2.0.9", features = ["full"] }
10+
indexmap = "2"
1211
proc-macro2 = "1"
1312
quote = "1"
13+
syn = { version = "2.0.9", features = ["full"] }
14+
synstructure = "0.13.0"

compiler/rustc_macros/src/symbols.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
//! cargo expand > /tmp/rustc_span.rs # it's a big file
2323
//! ```
2424
25+
use indexmap::IndexMap;
2526
use proc_macro2::{Span, TokenStream};
2627
use quote::quote;
27-
use std::collections::HashMap;
2828
use syn::parse::{Parse, ParseStream, Result};
2929
use syn::{braced, punctuated::Punctuated, Expr, Ident, Lit, LitStr, Macro, Token};
3030

@@ -136,30 +136,29 @@ pub fn symbols(input: TokenStream) -> TokenStream {
136136
output
137137
}
138138

139-
struct Preinterned {
140-
idx: u32,
141-
span_of_name: Span,
142-
}
143-
144139
struct Entries {
145-
map: HashMap<String, Preinterned>,
140+
map: IndexMap<String, Span>,
146141
}
147142

148143
impl Entries {
149144
fn with_capacity(capacity: usize) -> Self {
150-
Entries { map: HashMap::with_capacity(capacity) }
145+
Entries { map: IndexMap::with_capacity(capacity) }
151146
}
152147

153148
fn insert(&mut self, span: Span, str: &str, errors: &mut Errors) -> u32 {
154-
if let Some(prev) = self.map.get(str) {
155-
errors.error(span, format!("Symbol `{str}` is duplicated"));
156-
errors.error(prev.span_of_name, "location of previous definition".to_string());
157-
prev.idx
158-
} else {
159-
let idx = self.len();
160-
self.map.insert(str.to_string(), Preinterned { idx, span_of_name: span });
161-
idx
162-
}
149+
let idx = match self.map.entry(str.to_string()) {
150+
indexmap::map::Entry::Occupied(prev) => {
151+
errors.error(span, format!("Symbol `{str}` is duplicated"));
152+
errors.error(*prev.get(), "location of previous definition".to_string());
153+
prev.index()
154+
}
155+
indexmap::map::Entry::Vacant(entry) => {
156+
let idx = entry.index();
157+
entry.insert(span);
158+
idx
159+
}
160+
};
161+
u32::try_from(idx).expect("way too many symbols")
163162
}
164163

165164
fn len(&self) -> u32 {
@@ -273,8 +272,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
273272
}
274273
};
275274

276-
let idx = if let Some(prev) = entries.map.get(&value) {
277-
prev.idx
275+
let idx = if let Some(prev) = entries.map.get_index_of(&value) {
276+
u32::try_from(prev).expect("way too many symbols")
278277
} else {
279278
prefill_stream.extend(quote! {
280279
#value,
@@ -288,7 +287,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
288287
});
289288
}
290289

291-
let symbol_digits_base = entries.map["0"].idx;
290+
let symbol_digits_base =
291+
u32::try_from(entries.map.get_index_of("0").unwrap()).expect("way too many symbols");
292292
let preinterned_symbols_count = entries.len();
293293
let output = quote! {
294294
const SYMBOL_DIGITS_BASE: u32 = #symbol_digits_base;

0 commit comments

Comments
 (0)