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
pattern analysis: Use contiguous indices for enum variants #16979
Conversation
&Variant(id) => Some(id.into()), | ||
Variant(id) => { | ||
let hir_def::AdtId::EnumId(eid) = adt else { | ||
panic!("bad constructor {ctor:?} for adt {adt:?}") |
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.
Maybe stdx::never
and return None
here so we don't panic?
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.
If we return None
we panic only a tiny bit later because most of the calls of this function unwrap()
the output
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.
Want me to change that anyway?
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.
panicking early seems better then
0a84c37
to
216dab3
Compare
// Find the index of this variant in the list of variants. | ||
db.enum_data(eid) | ||
.variants | ||
.iter() | ||
.map(|(evid, _name)| *evid) | ||
.enumerate() | ||
.find(|(_, evid)| *evid == target_evid) | ||
.map(|(i, _)| EnumVariantContiguousIndex(i)) |
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 can be simplified to
// Find the index of this variant in the list of variants. | |
db.enum_data(eid) | |
.variants | |
.iter() | |
.map(|(evid, _name)| *evid) | |
.enumerate() | |
.find(|(_, evid)| *evid == target_evid) | |
.map(|(i, _)| EnumVariantContiguousIndex(i)) | |
target_evid.lookup(self.db.upcast()).index as usize |
(we store the index in the item tree to prevent a linear lookup here)
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.
Oh nicee
Thanks! |
✌️ @Nadrieril, you can now approve this pull request! If @Veykril told you to " |
216dab3
to
7e8f2d8
Compare
☀️ Test successful - checks-actions |
The main blocker to using the in-tree version of the
pattern_analysis
crate is that rustc requires enum indices to be contiguous because it usesIndexVec
/BitSet
for performance. Currently we swap these out forFxHashMap
/FxHashSet
when therustc
feature is off, but we can't do that if we use the in-tree crate.This PR solves the problem by using contiguous indices on the r-a side too.