-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Streamline define_dep_nodes.
#152525
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
base: main
Are you sure you want to change the base?
Streamline define_dep_nodes.
#152525
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,56 +14,33 @@ macro_rules! define_dep_nodes { | |
| [$($modifiers:tt)*] fn $variant:ident($($K:tt)*) -> $V:ty, | ||
| )* | ||
| ) => { | ||
|
|
||
| #[macro_export] | ||
| macro_rules! make_dep_kind_array { | ||
| ($mod:ident) => {[ $($mod::$variant()),* ]}; | ||
| } | ||
|
|
||
| /// This enum serves as an index into arrays built by `make_dep_kind_array`. | ||
| // This enum has more than u8::MAX variants so we need some kind of multi-byte | ||
| // encoding. The derived Encodable/Decodable uses leb128 encoding which is | ||
| // dense when only considering this enum. But DepKind is encoded in a larger | ||
| // struct, and there we can take advantage of the unused bits in the u16. | ||
| #[allow(non_camel_case_types)] | ||
| #[repr(u16)] // Must be kept in sync with the inner type of `DepKind`. | ||
| enum DepKindDefs { | ||
| $( $( #[$attr] )* $variant),* | ||
| } | ||
|
|
||
| #[allow(non_upper_case_globals)] | ||
| pub mod dep_kinds { | ||
| use super::*; | ||
|
|
||
| $( | ||
| // The `as u16` cast must be kept in sync with the inner type of `DepKind`. | ||
| pub const $variant: DepKind = DepKind::new(DepKindDefs::$variant as u16); | ||
| pub const $variant: DepKind = DepKind::new(${index()}); | ||
| )* | ||
| } | ||
|
|
||
| // This checks that the discriminants of the variants have been assigned consecutively | ||
| // from 0 so that they can be used as a dense index. | ||
| pub(crate) const DEP_KIND_VARIANTS: u16 = { | ||
| let deps = &[$(dep_kinds::$variant,)*]; | ||
| let mut i = 0; | ||
| while i < deps.len() { | ||
| if i != deps[i].as_usize() { | ||
| panic!(); | ||
| } | ||
| i += 1; | ||
| } | ||
| deps.len() as u16 | ||
| }; | ||
| pub(crate) const NUM_DEP_KIND_VARIANTS: u16 = ${count($variant)}; | ||
|
|
||
| /// List containing the name of each dep kind as a static string, | ||
| /// indexable by `DepKind`. | ||
| pub(crate) const DEP_KIND_NAMES: &[&str] = &[ | ||
| $( self::label_strs::$variant, )* | ||
| $( stringify!($variant), )* | ||
| ]; | ||
|
|
||
| pub(super) fn dep_kind_from_label_string(label: &str) -> Result<DepKind, ()> { | ||
| match label { | ||
| $( self::label_strs::$variant => Ok(self::dep_kinds::$variant), )* | ||
| $( stringify!($variant) => Ok(self::dep_kinds::$variant), )* | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the time I was thinking that it would be good to reduce the amount of So with hindsight, I don't really object to changing it back.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me, mentally expanding |
||
| _ => Err(()), | ||
| } | ||
| } | ||
|
|
||
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.
Do we have any other check that the numeric values continue to be dense and ascending from 0?
That's currently still true in the new implementation, but I don't know if there's anything ensuring that it remains true.
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.
The numeric values come from the macro metavar
${index()}, which is "The current index of the inner-most repetition.". I.e. it's just a 0..n counter.AFAICT this sanity check was overkill for the enum case, because enums are (I think?) guaranteed to be numbered from 0..n but I can see why someone might be uncertain about that and want to check it. But checking that
${index()}actually implements 0..n seems paranoid, that's fully in "I don't even trust the compiler" territory.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.
This check seems awfully familiar, as if I wrote it. If I did, I was unsure at the time if it was justified, and the cost seemed acceptable at the time. Fine with me if it's now too clunky.
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.
My concern is not whether
${index()}implements 0..n, but rather that if someone tries to modify this code without knowing that the 0..n is load-bearing, then they'll end up with either mysterious crashes when running the compiler, or (worst-case) very subtle incremental-compilation bugs.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.
All that said, after #152516 has landed I question whether
DepKindneeds to be a u16 newtype at all.My understanding is that #115920 split off
DepKindfrom the enum, because the enum was inrustc_middlebutDepKindwanted to be upstream inrustc_query_system.If
DepKindis moved back torustc_middle, we should be able to just make it an enum again.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.