mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
Simplify the current_rustc_version
macro.
It currently has the syntax `current_rustc_version!(env!("CFG_RELEASE"))` where the `env!("CFG_RELEASE")` part looks like a normal expression but it is actually parsed and processed by the `current_rustc_version` macro. The documented rationale for this is that you'll find it if you grep for `env!("CFG_RELEASE")`. But I think that's of very little use -- I would personally grep for just "CFG_RELEASE" -- and it complicates the macro, requiring the use of `syn`. This commit simplifies the macro.
This commit is contained in:
parent
fdaaaf9f92
commit
49908b4d90
@ -1,37 +1,16 @@
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote;
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::{parenthesized, parse_macro_input, LitStr, Token};
|
||||
|
||||
pub struct Input {
|
||||
variable: LitStr,
|
||||
}
|
||||
|
||||
mod kw {
|
||||
syn::custom_keyword!(env);
|
||||
}
|
||||
|
||||
impl Parse for Input {
|
||||
// Input syntax is `env!("CFG_RELEASE")` to facilitate grepping.
|
||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
let paren;
|
||||
input.parse::<kw::env>()?;
|
||||
input.parse::<Token![!]>()?;
|
||||
parenthesized!(paren in input);
|
||||
let variable: LitStr = paren.parse()?;
|
||||
Ok(Input { variable })
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn current_version(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as Input);
|
||||
|
||||
TokenStream::from(match RustcVersion::parse_env_var(&input.variable) {
|
||||
pub(crate) fn current_version(_input: TokenStream) -> TokenStream {
|
||||
let env_var = "CFG_RELEASE";
|
||||
TokenStream::from(match RustcVersion::parse_cfg_release(env_var) {
|
||||
Ok(RustcVersion { major, minor, patch }) => quote!(
|
||||
// The produced literal has type `rustc_session::RustcVersion`.
|
||||
Self { major: #major, minor: #minor, patch: #patch }
|
||||
),
|
||||
Err(err) => syn::Error::new(Span::call_site(), err).into_compile_error(),
|
||||
Err(err) => syn::Error::new(Span::call_site(), format!("{env_var} env var: {err}"))
|
||||
.into_compile_error(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -42,8 +21,8 @@ struct RustcVersion {
|
||||
}
|
||||
|
||||
impl RustcVersion {
|
||||
fn parse_env_var(env_var: &LitStr) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let value = proc_macro::tracked_env::var(env_var.value())?;
|
||||
fn parse_cfg_release(env_var: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let value = proc_macro::tracked_env::var(env_var)?;
|
||||
Self::parse_str(&value)
|
||||
.ok_or_else(|| format!("failed to parse rustc version: {:?}", value).into())
|
||||
}
|
||||
|
@ -26,6 +26,9 @@ mod symbols;
|
||||
mod type_foldable;
|
||||
mod type_visitable;
|
||||
|
||||
// Reads the rust version (e.g. "1.75.0") from the CFG_RELEASE env var and
|
||||
// produces a `RustcVersion` literal containing that version (e.g.
|
||||
// `RustcVersion { major: 1, minor: 75, patch: 0 }`).
|
||||
#[proc_macro]
|
||||
pub fn current_rustc_version(input: TokenStream) -> TokenStream {
|
||||
current_version::current_version(input)
|
||||
|
@ -9,7 +9,7 @@ pub struct RustcVersion {
|
||||
}
|
||||
|
||||
impl RustcVersion {
|
||||
pub const CURRENT: Self = current_rustc_version!(env!("CFG_RELEASE"));
|
||||
pub const CURRENT: Self = current_rustc_version!();
|
||||
}
|
||||
|
||||
impl Display for RustcVersion {
|
||||
|
Loading…
Reference in New Issue
Block a user