From 5ce5e3c37425e370e61ec1e04ff1cfdbbe375c41 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 17 Apr 2024 17:41:16 -0400 Subject: [PATCH] track and assert struct ingredient indices We need a cheap way to compute field indices. --- components/salsa-2022-macros/src/tracked_struct.rs | 2 +- components/salsa-2022/src/routes.rs | 8 ++++++-- components/salsa-2022/src/tracked_struct.rs | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/components/salsa-2022-macros/src/tracked_struct.rs b/components/salsa-2022-macros/src/tracked_struct.rs index 0a4bb9b9..9d0ec4f5 100644 --- a/components/salsa-2022-macros/src/tracked_struct.rs +++ b/components/salsa-2022-macros/src/tracked_struct.rs @@ -257,7 +257,7 @@ impl TrackedStruct { where DB: salsa::DbWithJar + salsa::storage::JarFromJars, { - let struct_ingredient = { + let struct_ingredient = { let index = routes.push( |jars| { let jar = >::jar_from_jars(jars); diff --git a/components/salsa-2022/src/routes.rs b/components/salsa-2022/src/routes.rs index 1226bc0a..d8e663c1 100644 --- a/components/salsa-2022/src/routes.rs +++ b/components/salsa-2022/src/routes.rs @@ -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 } } diff --git a/components/salsa-2022/src/tracked_struct.rs b/components/salsa-2022/src/tracked_struct.rs index 0a548060..15f8d08e 100644 --- a/components/salsa-2022/src/tracked_struct.rs +++ b/components/salsa-2022/src/tracked_struct.rs @@ -121,6 +121,10 @@ pub struct TrackedStructValue 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 { + 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,