Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 6 pull requests #117249

Merged
merged 24 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6c97f13
Invalid `?` suggestion on mismatched `Ok(T)`
eopb Oct 20, 2023
24cdb27
let_chainify `suggest_coercing_result_via_try_operator`
eopb Oct 22, 2023
8716890
Enable cg_clif tests for riscv64gc
bjorn3 Oct 21, 2023
c1bfd46
When expecting closure argument but finding block provide suggestion
estebank Oct 23, 2023
f0a2635
Redo `stringify.rs` test.
nnethercote Oct 19, 2023
2e2e780
Augment `stringify.rs` test.
nnethercote Oct 19, 2023
c1800fb
Augment `stringify.rs` test some more.
nnethercote Oct 24, 2023
173dcb2
Touch up syn parsing in symbols macro
dtolnay Oct 25, 2023
ba17934
Represent symbol value as enum to prepare for supporting env vars
dtolnay Oct 25, 2023
95742ff
Add a Parse impl for symbol Value
dtolnay Oct 25, 2023
8dea49a
Delete counter from symbols proc macro in favor of hashmap as source …
dtolnay Oct 25, 2023
726a43c
Move symbols macro map into a struct
dtolnay Oct 25, 2023
65f0253
Support environment variable for interned Symbol value
dtolnay Oct 25, 2023
1078250
Continue generating other symbols if an expr is not supported
dtolnay Oct 25, 2023
5563a9b
Improve span of env-related errors
dtolnay Oct 25, 2023
ac4fa3f
Pre-intern a symbol for env!("CFG_RELEASE")
dtolnay Oct 25, 2023
c1552df
Fix symbols::tests::test_symbols
dtolnay Oct 25, 2023
94ecabf
Add comment to mem::replace to explain why it's not implemented via m…
chfogelman Oct 26, 2023
934cbe4
Rollup merge of #116968 - eopb:116967, r=petrochenkov
matthiaskrgr Oct 26, 2023
596369f
Rollup merge of #117032 - bjorn3:riscv64_enable_cg_clif_tests, r=petr…
matthiaskrgr Oct 26, 2023
d09c988
Rollup merge of #117106 - estebank:issue-27300, r=petrochenkov
matthiaskrgr Oct 26, 2023
a8f7acd
Rollup merge of #117114 - nnethercote:improve-stringify-test, r=petro…
matthiaskrgr Oct 26, 2023
2656c98
Rollup merge of #117188 - dtolnay:symbolenv, r=cjgillot
matthiaskrgr Oct 26, 2023
c0b1c1a
Rollup merge of #117243 - chfogelman:replace-not-swap-comment, r=thomcc
matthiaskrgr Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move symbols macro map into a struct
  • Loading branch information
dtolnay committed Oct 26, 2023
commit 726a43c1de78eb21a483896c052b353939531cdb
52 changes: 32 additions & 20 deletions compiler/rustc_macros/src/symbols.rs
Original file line number Diff line number Diff line change
@@ -126,6 +126,32 @@ struct Preinterned {
span_of_name: Span,
}

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

impl Entries {
fn with_capacity(capacity: usize) -> Self {
Entries { map: HashMap::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
}
}

fn len(&self) -> u32 {
u32::try_from(self.map.len()).expect("way too many symbols")
}
}

fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let mut errors = Errors::default();

@@ -142,23 +168,9 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let mut keyword_stream = quote! {};
let mut symbols_stream = quote! {};
let mut prefill_stream = quote! {};
let mut entries = HashMap::<String, Preinterned>::with_capacity(
input.keywords.len() + input.symbols.len() + 10,
);
let mut entries = Entries::with_capacity(input.keywords.len() + input.symbols.len() + 10);
let mut prev_key: Option<(Span, String)> = None;

let mut insert = |span: Span, str: &str, errors: &mut Errors| -> u32 {
if let Some(prev) = entries.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 = u32::try_from(entries.len()).expect("way too many symbols");
entries.insert(str.to_string(), Preinterned { idx, span_of_name: span });
idx
}
};

let mut check_order = |span: Span, str: &str, errors: &mut Errors| {
if let Some((prev_span, ref prev_str)) = prev_key {
if str < prev_str {
@@ -174,7 +186,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let name = &keyword.name;
let value = &keyword.value;
let value_string = value.value();
let idx = insert(keyword.name.span(), &value_string, &mut errors);
let idx = entries.insert(keyword.name.span(), &value_string, &mut errors);
prefill_stream.extend(quote! {
#value,
});
@@ -190,7 +202,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
Value::SameAsName => name.to_string(),
Value::String(lit) => lit.value(),
};
let idx = insert(symbol.name.span(), &value, &mut errors);
let idx = entries.insert(symbol.name.span(), &value, &mut errors);
check_order(symbol.name.span(), &name.to_string(), &mut errors);

prefill_stream.extend(quote! {
@@ -204,14 +216,14 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
// Generate symbols for the strings "0", "1", ..., "9".
for n in 0..10 {
let n = n.to_string();
insert(Span::call_site(), &n, &mut errors);
entries.insert(Span::call_site(), &n, &mut errors);
prefill_stream.extend(quote! {
#n,
});
}

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