Skip to content

Commit

Permalink
Use IndexMap to keep track of pre-interned symbol indices
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 26, 2023
1 parent 34e14ef commit 6eae659
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4152,6 +4152,7 @@ dependencies = [
name = "rustc_macros"
version = "0.0.0"
dependencies = [
"indexmap 2.0.0",
"proc-macro2",
"quote",
"syn 2.0.29",
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"
proc-macro = true

[dependencies]
synstructure = "0.13.0"
syn = { version = "2.0.9", features = ["full"] }
indexmap = "2"
proc-macro2 = "1"
quote = "1"
syn = { version = "2.0.9", features = ["full"] }
synstructure = "0.13.0"
40 changes: 20 additions & 20 deletions compiler/rustc_macros/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
//! cargo expand > /tmp/rustc_span.rs # it's a big file
//! ```
use indexmap::IndexMap;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use std::collections::HashMap;
use syn::parse::{Parse, ParseStream, Result};
use syn::{braced, punctuated::Punctuated, Expr, Ident, Lit, LitStr, Macro, Token};

Expand Down Expand Up @@ -136,30 +136,29 @@ pub fn symbols(input: TokenStream) -> TokenStream {
output
}

struct Preinterned {
idx: u32,
span_of_name: Span,
}

struct Entries {
map: HashMap<String, Preinterned>,
map: IndexMap<String, Span>,
}

impl Entries {
fn with_capacity(capacity: usize) -> Self {
Entries { map: HashMap::with_capacity(capacity) }
Entries { map: IndexMap::with_capacity(capacity) }
}

fn insert(&mut self, span: Span, str: &str, errors: &mut Errors) -> u32 {
if let Some(prev) = self.map.get(str) {
errors.error(span, format!("Symbol `{str}` is duplicated"));
errors.error(prev.span_of_name, "location of previous definition".to_string());
prev.idx
} else {
let idx = self.len();
self.map.insert(str.to_string(), Preinterned { idx, span_of_name: span });
idx
}
let idx = match self.map.entry(str.to_string()) {
indexmap::map::Entry::Occupied(prev) => {
errors.error(span, format!("Symbol `{str}` is duplicated"));
errors.error(*prev.get(), "location of previous definition".to_string());
prev.index()
}
indexmap::map::Entry::Vacant(entry) => {
let idx = entry.index();
entry.insert(span);
idx
}
};
u32::try_from(idx).expect("way too many symbols")
}

fn len(&self) -> u32 {
Expand Down Expand Up @@ -273,8 +272,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
}
};

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

let symbol_digits_base = entries.map["0"].idx;
let symbol_digits_base =
u32::try_from(entries.map.get_index_of("0").unwrap()).expect("way too many symbols");
let preinterned_symbols_count = entries.len();
let output = quote! {
const SYMBOL_DIGITS_BASE: u32 = #symbol_digits_base;
Expand Down

0 comments on commit 6eae659

Please sign in to comment.