forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#117256 - dtolnay:currentversion, r=compiler…
…-errors Parse rustc version at compile time This PR eliminates a couple awkward codepaths where it was not clear how the compiler should proceed if its own version number is incomprehensible. https://github.com/rust-lang/rust/blob/dab715641e96a61a534587fda9de1128b75b34dc/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs#L385 https://github.com/rust-lang/rust/blob/dab715641e96a61a534587fda9de1128b75b34dc/compiler/rustc_attr/src/builtin.rs#L630 We can guarantee that every compiled rustc comes with a working version number, so the ICE codepaths above shouldn't need to be written.
- Loading branch information
Showing
9 changed files
with
124 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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) { | ||
Ok(RustcVersion { major, minor, patch }) => quote!( | ||
Self { major: #major, minor: #minor, patch: #patch } | ||
), | ||
Err(err) => syn::Error::new(Span::call_site(), err).into_compile_error(), | ||
}) | ||
} | ||
|
||
struct RustcVersion { | ||
major: u16, | ||
minor: u16, | ||
patch: u16, | ||
} | ||
|
||
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())?; | ||
Self::parse_str(&value) | ||
.ok_or_else(|| format!("failed to parse rustc version: {:?}", value).into()) | ||
} | ||
|
||
fn parse_str(value: &str) -> Option<Self> { | ||
// Ignore any suffixes such as "-dev" or "-nightly". | ||
let mut components = value.split('-').next().unwrap().splitn(3, '.'); | ||
let major = components.next()?.parse().ok()?; | ||
let minor = components.next()?.parse().ok()?; | ||
let patch = components.next().unwrap_or("0").parse().ok()?; | ||
Some(RustcVersion { major, minor, patch }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::fmt::{self, Display}; | ||
|
||
#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
#[derive(HashStable_Generic)] | ||
pub struct RustcVersion { | ||
pub major: u16, | ||
pub minor: u16, | ||
pub patch: u16, | ||
} | ||
|
||
impl RustcVersion { | ||
pub const CURRENT: Self = current_rustc_version!(env!("CFG_RELEASE")); | ||
} | ||
|
||
impl Display for RustcVersion { | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters