Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
//! Proc macro which builds the Symbol table
|
|
|
|
//!
|
|
|
|
//! # Debugging
|
|
|
|
//!
|
|
|
|
//! Since this proc-macro does some non-trivial work, debugging it is important.
|
|
|
|
//! This proc-macro can be invoked as an ordinary unit test, like so:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! cd compiler/rustc_macros
|
|
|
|
//! cargo test symbols::test_symbols -- --nocapture
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This unit test finds the `symbols!` invocation in `compiler/rustc_span/src/symbol.rs`
|
|
|
|
//! and runs it. It verifies that the output token stream can be parsed as valid module
|
|
|
|
//! items and that no errors were produced.
|
|
|
|
//!
|
|
|
|
//! You can also view the generated code by using `cargo expand`:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! cargo install cargo-expand # this is necessary only once
|
|
|
|
//! cd compiler/rustc_span
|
|
|
|
//! cargo expand > /tmp/rustc_span.rs # it's a big file
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
use proc_macro2::{Span, TokenStream};
|
2019-04-03 00:43:49 +00:00
|
|
|
use quote::quote;
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
use std::collections::HashMap;
|
2019-04-03 00:43:49 +00:00
|
|
|
use syn::parse::{Parse, ParseStream, Result};
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
use syn::{braced, punctuated::Punctuated, Ident, LitStr, Token};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2019-04-03 00:43:49 +00:00
|
|
|
|
|
|
|
mod kw {
|
|
|
|
syn::custom_keyword!(Keywords);
|
2019-05-06 23:55:12 +00:00
|
|
|
syn::custom_keyword!(Symbols);
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Keyword {
|
|
|
|
name: Ident,
|
|
|
|
value: LitStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Keyword {
|
|
|
|
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
|
|
|
let name = input.parse()?;
|
2019-04-09 07:39:48 +00:00
|
|
|
input.parse::<Token![:]>()?;
|
2019-04-03 00:43:49 +00:00
|
|
|
let value = input.parse()?;
|
|
|
|
|
|
|
|
Ok(Keyword { name, value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:55:12 +00:00
|
|
|
struct Symbol {
|
|
|
|
name: Ident,
|
|
|
|
value: Option<LitStr>,
|
|
|
|
}
|
2019-04-03 00:43:49 +00:00
|
|
|
|
|
|
|
impl Parse for Symbol {
|
|
|
|
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
2019-05-06 23:55:12 +00:00
|
|
|
let name = input.parse()?;
|
|
|
|
let value = match input.parse::<Token![:]>() {
|
|
|
|
Ok(_) => Some(input.parse()?),
|
|
|
|
Err(_) => None,
|
|
|
|
};
|
2019-04-03 00:43:49 +00:00
|
|
|
|
2019-05-06 23:55:12 +00:00
|
|
|
Ok(Symbol { name, value })
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Input {
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
keywords: Punctuated<Keyword, Token![,]>,
|
|
|
|
symbols: Punctuated<Symbol, Token![,]>,
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Input {
|
|
|
|
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
|
|
|
input.parse::<kw::Keywords>()?;
|
|
|
|
let content;
|
|
|
|
braced!(content in input);
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let keywords = Punctuated::parse_terminated(&content)?;
|
2019-04-03 00:43:49 +00:00
|
|
|
|
2019-05-06 23:55:12 +00:00
|
|
|
input.parse::<kw::Symbols>()?;
|
2019-04-03 00:43:49 +00:00
|
|
|
let content;
|
|
|
|
braced!(content in input);
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let symbols = Punctuated::parse_terminated(&content)?;
|
2019-04-03 00:43:49 +00:00
|
|
|
|
|
|
|
Ok(Input { keywords, symbols })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Errors {
|
|
|
|
list: Vec<syn::Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Errors {
|
|
|
|
fn error(&mut self, span: Span, message: String) {
|
|
|
|
self.list.push(syn::Error::new(span, message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 00:43:49 +00:00
|
|
|
pub fn symbols(input: TokenStream) -> TokenStream {
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let (mut output, errors) = symbols_with_errors(input);
|
|
|
|
|
|
|
|
// If we generated any errors, then report them as compiler_error!() macro calls.
|
|
|
|
// This lets the errors point back to the most relevant span. It also allows us
|
|
|
|
// to report as many errors as we can during a single run.
|
|
|
|
output.extend(errors.into_iter().map(|e| e.to_compile_error()));
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
|
|
|
|
let mut errors = Errors::default();
|
|
|
|
|
|
|
|
let input: Input = match syn::parse2(input) {
|
|
|
|
Ok(input) => input,
|
|
|
|
Err(e) => {
|
|
|
|
// This allows us to display errors at the proper span, while minimizing
|
|
|
|
// unrelated errors caused by bailing out (and not generating code).
|
|
|
|
errors.list.push(e);
|
|
|
|
Input { keywords: Default::default(), symbols: Default::default() }
|
|
|
|
}
|
|
|
|
};
|
2019-04-03 00:43:49 +00:00
|
|
|
|
|
|
|
let mut keyword_stream = quote! {};
|
|
|
|
let mut symbols_stream = quote! {};
|
|
|
|
let mut prefill_stream = quote! {};
|
|
|
|
let mut counter = 0u32;
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let mut keys =
|
|
|
|
HashMap::<String, Span>::with_capacity(input.keywords.len() + input.symbols.len() + 10);
|
|
|
|
let mut prev_key: Option<(Span, String)> = None;
|
|
|
|
|
|
|
|
let mut check_dup = |span: Span, str: &str, errors: &mut Errors| {
|
|
|
|
if let Some(prev_span) = keys.get(str) {
|
2022-12-19 09:31:55 +00:00
|
|
|
errors.error(span, format!("Symbol `{str}` is duplicated"));
|
2021-07-21 20:43:19 +00:00
|
|
|
errors.error(*prev_span, "location of previous definition".to_string());
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
} else {
|
|
|
|
keys.insert(str.to_string(), span);
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let mut check_order = |span: Span, str: &str, errors: &mut Errors| {
|
|
|
|
if let Some((prev_span, ref prev_str)) = prev_key {
|
2020-07-09 23:46:38 +00:00
|
|
|
if str < prev_str {
|
2022-12-19 09:31:55 +00:00
|
|
|
errors.error(span, format!("Symbol `{str}` must precede `{prev_str}`"));
|
|
|
|
errors.error(prev_span, format!("location of previous symbol `{prev_str}`"));
|
2020-07-09 23:46:38 +00:00
|
|
|
}
|
|
|
|
}
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
prev_key = Some((span, str.to_string()));
|
2020-07-09 23:46:38 +00:00
|
|
|
};
|
|
|
|
|
2019-05-22 09:25:39 +00:00
|
|
|
// Generate the listed keywords.
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
for keyword in input.keywords.iter() {
|
2019-04-03 00:43:49 +00:00
|
|
|
let name = &keyword.name;
|
|
|
|
let value = &keyword.value;
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let value_string = value.value();
|
|
|
|
check_dup(keyword.name.span(), &value_string, &mut errors);
|
2019-04-03 00:43:49 +00:00
|
|
|
prefill_stream.extend(quote! {
|
|
|
|
#value,
|
|
|
|
});
|
|
|
|
keyword_stream.extend(quote! {
|
2019-05-11 14:41:37 +00:00
|
|
|
pub const #name: Symbol = Symbol::new(#counter);
|
2019-04-03 00:43:49 +00:00
|
|
|
});
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:25:39 +00:00
|
|
|
// Generate the listed symbols.
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
for symbol in input.symbols.iter() {
|
2019-05-06 23:55:12 +00:00
|
|
|
let name = &symbol.name;
|
|
|
|
let value = match &symbol.value {
|
|
|
|
Some(value) => value.value(),
|
|
|
|
None => name.to_string(),
|
|
|
|
};
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
check_dup(symbol.name.span(), &value, &mut errors);
|
|
|
|
check_order(symbol.name.span(), &name.to_string(), &mut errors);
|
|
|
|
|
2019-04-03 00:43:49 +00:00
|
|
|
prefill_stream.extend(quote! {
|
2019-05-06 23:55:12 +00:00
|
|
|
#value,
|
2019-04-03 00:43:49 +00:00
|
|
|
});
|
|
|
|
symbols_stream.extend(quote! {
|
2019-05-06 23:55:12 +00:00
|
|
|
pub const #name: Symbol = Symbol::new(#counter);
|
2019-04-03 00:43:49 +00:00
|
|
|
});
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:25:39 +00:00
|
|
|
// Generate symbols for the strings "0", "1", ..., "9".
|
2020-12-14 19:34:55 +00:00
|
|
|
let digits_base = counter;
|
|
|
|
counter += 10;
|
2019-05-22 09:25:39 +00:00
|
|
|
for n in 0..10 {
|
|
|
|
let n = n.to_string();
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
check_dup(Span::call_site(), &n, &mut errors);
|
2019-05-22 09:25:39 +00:00
|
|
|
prefill_stream.extend(quote! {
|
|
|
|
#n,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
let output = quote! {
|
2020-12-14 19:34:55 +00:00
|
|
|
const SYMBOL_DIGITS_BASE: u32 = #digits_base;
|
2022-08-20 12:13:41 +00:00
|
|
|
const PREINTERNED_SYMBOLS_COUNT: u32 = #counter;
|
2019-04-03 00:43:49 +00:00
|
|
|
|
2020-12-14 19:34:55 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
mod kw_generated {
|
|
|
|
use super::Symbol;
|
|
|
|
#keyword_stream
|
|
|
|
}
|
2019-05-22 09:25:39 +00:00
|
|
|
|
2020-12-14 19:34:55 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod sym_generated {
|
|
|
|
use super::Symbol;
|
|
|
|
#symbols_stream
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Interner {
|
2021-08-08 15:24:30 +00:00
|
|
|
pub(crate) fn fresh() -> Self {
|
2019-04-03 00:43:49 +00:00
|
|
|
Interner::prefill(&[
|
|
|
|
#prefill_stream
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
};
|
2019-05-06 23:55:12 +00:00
|
|
|
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
(output, errors.list)
|
2019-05-06 23:55:12 +00:00
|
|
|
|
Improve error handling in `symbols` proc-macro
This improves how the `symbols` proc-macro handles errors.
If it finds an error in its input, the macro does not panic.
Instead, it still produces an output token stream. That token
stream will contain `compile_error!(...)` macro invocations.
This will still cause compilation to fail (which is what we want),
but it will prevent meaningless errors caused by the output not
containing symbols that the macro normally generates.
This solves a small (but annoying) problem. When you're editing
rustc_span/src/symbol.rs, and you get something wrong (dup
symbol name, misordered symbol), you want to get only the errors
that are relevant, not a burst of errors that are irrelevant.
This change also uses the correct Span when reporting errors,
so you get errors that point to the correct place in
rustc_span/src/symbol.rs where something is wrong.
This also adds several unit tests which test the `symbols` proc-macro.
This commit also makes it easy to run the `symbols` proc-macro
as an ordinary Cargo test. Just run `cargo test`. This makes it
easier to do development on the macro itself, such as running it
under a debugger.
This commit also uses the `Punctuated` type in `syn` for parsing
comma-separated lists, rather than doing it manually.
The output of the macro is not changed at all by this commit,
so rustc should be completely unchanged. This just improves
quality of life during development.
2020-12-11 19:32:48 +00:00
|
|
|
// To see the generated code, use the "cargo expand" command.
|
|
|
|
// Do this once to install:
|
|
|
|
// cargo install cargo-expand
|
|
|
|
//
|
|
|
|
// Then, cd to rustc_span and run:
|
|
|
|
// cargo expand > /tmp/rustc_span_expanded.rs
|
|
|
|
//
|
|
|
|
// and read that file.
|
2019-04-03 00:43:49 +00:00
|
|
|
}
|