track and assert struct ingredient indices

We need a cheap way to compute field indices.
This commit is contained in:
Niko Matsakis 2024-04-17 17:41:16 -04:00
parent 79d24e0ad7
commit 5ce5e3c374
3 changed files with 21 additions and 3 deletions

View file

@ -257,7 +257,7 @@ impl TrackedStruct {
where
DB: salsa::DbWithJar<Self::Jar> + salsa::storage::JarFromJars<Self::Jar>,
{
let struct_ingredient = {
let struct_ingredient = {
let index = routes.push(
|jars| {
let jar = <DB as salsa::storage::JarFromJars<Self::Jar>>::jar_from_jars(jars);

View file

@ -10,13 +10,17 @@ pub struct IngredientIndex(u32);
impl IngredientIndex {
/// Create an ingredient index from a usize.
fn from(v: usize) -> Self {
pub(crate) fn from(v: usize) -> Self {
assert!(v < (std::u32::MAX as usize));
Self(v as u32)
}
pub(crate) fn as_u32(self) -> u32 {
self.0
}
/// Convert the ingredient index back into a usize.
fn as_usize(self) -> usize {
pub(crate) fn as_usize(self) -> usize {
self.0 as usize
}
}

View file

@ -121,6 +121,10 @@ pub struct TrackedStructValue<C>
where
C: Configuration,
{
/// Index of the struct ingredient.
#[allow(dead_code)]
struct_ingredient_index: IngredientIndex,
/// The id of this struct in the ingredient.
id: C::Id,
@ -166,12 +170,21 @@ where
}
}
fn struct_ingredient_index(&self) -> IngredientIndex {
self.interned.ingredient_index()
}
pub fn new_field_ingredient(
&self,
field_ingredient_index: IngredientIndex,
field_index: u32,
field_debug_name: &'static str,
) -> TrackedFieldIngredient<C> {
assert_eq!(
field_ingredient_index.as_u32() - self.struct_ingredient_index().as_u32() - 1,
field_index,
);
TrackedFieldIngredient {
ingredient_index: field_ingredient_index,
field_index,
@ -215,6 +228,7 @@ where
runtime,
TrackedStructValue {
id,
struct_ingredient_index: self.struct_ingredient_index(),
created_at: current_revision,
durability: current_deps.durability,
fields,