-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Parse rustc version at compile time #117256
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 ⬆️
I don't fully understand how this works, but without this change, the PR does not compile. Hopefully it is okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because of the
HashStable_Generic
derive onRustcVersion
inrustc_session
. If you really wanted to avoid this, you'd need to move that data structure fromrustc_session
torustc_ast
or derive it manually.Deriving it manually may be easier since it's just a few u16s....?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, this handwritten impl gets the job done: 0fc378a
But in the absence of any known downside of changing
rustc_attr::HashStableContext
, I think I would go for keeping the derived impl. I was not able to find any evidence ofdyn HashStableContext
in use anywhere, so I don't see any way that this change could bloat vtables, for example.