From ef69e4f33a5efaac40fed22c603d7b5024e25f3a Mon Sep 17 00:00:00 2001 From: orizi <104711814+orizi@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:05:44 +0300 Subject: [PATCH] Fixed self-ref type detection. (#6455) --- crates/cairo-lang-sierra-generator/src/db.rs | 5 ++- .../cairo-lang-sierra-generator/src/types.rs | 42 +++++++++++++------ tests/bug_samples/issue2249.cairo | 15 +++++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/crates/cairo-lang-sierra-generator/src/db.rs b/crates/cairo-lang-sierra-generator/src/db.rs index 79078cd7c41..e1f3a607486 100644 --- a/crates/cairo-lang-sierra-generator/src/db.rs +++ b/crates/cairo-lang-sierra-generator/src/db.rs @@ -79,7 +79,6 @@ pub trait SierraGenGroup: LoweringGroup + Upcast { /// Returns if the semantic id has a circular definition. #[salsa::invoke(crate::types::is_self_referential)] - #[salsa::cycle(crate::types::is_self_referential_cycle)] fn is_self_referential(&self, type_id: semantic::TypeId) -> Maybe; /// Returns the semantic type ids the type is directly dependent on. @@ -89,6 +88,10 @@ pub trait SierraGenGroup: LoweringGroup + Upcast { #[salsa::invoke(crate::types::type_dependencies)] fn type_dependencies(&self, type_id: semantic::TypeId) -> Maybe>; + #[salsa::invoke(crate::types::has_in_deps)] + #[salsa::cycle(crate::types::has_in_deps_cycle)] + fn has_in_deps(&self, type_id: semantic::TypeId, needle: semantic::TypeId) -> Maybe; + /// Returns the [cairo_lang_sierra::program::FunctionSignature] object for the given function /// id. fn get_function_signature( diff --git a/crates/cairo-lang-sierra-generator/src/types.rs b/crates/cairo-lang-sierra-generator/src/types.rs index 82ac2221ee5..cb80e47af0c 100644 --- a/crates/cairo-lang-sierra-generator/src/types.rs +++ b/crates/cairo-lang-sierra-generator/src/types.rs @@ -161,21 +161,9 @@ pub fn get_concrete_long_type_id( }) } -/// See [SierraGenGroup::is_self_referential] for documentation. -pub fn is_self_referential_cycle( - _db: &dyn SierraGenGroup, - _cycle: &salsa::Cycle, - _type_id: &semantic::TypeId, -) -> Maybe { - Ok(true) -} - /// See [SierraGenGroup::is_self_referential] for documentation. pub fn is_self_referential(db: &dyn SierraGenGroup, type_id: semantic::TypeId) -> Maybe { - for ty in db.type_dependencies(type_id)?.iter() { - db.is_self_referential(*ty)?; - } - Ok(false) + db.has_in_deps(type_id, type_id) } /// See [SierraGenGroup::type_dependencies] for documentation. @@ -225,3 +213,31 @@ pub fn type_dependencies( } .into()) } + +/// See [SierraGenGroup::has_in_deps] for documentation. +pub fn has_in_deps( + db: &dyn SierraGenGroup, + type_id: semantic::TypeId, + needle: semantic::TypeId, +) -> Maybe { + let deps = type_dependencies(db, type_id)?; + if deps.contains(&needle) { + return Ok(true); + } + for dep in deps.iter() { + if db.has_in_deps(*dep, needle)? { + return Ok(true); + } + } + Ok(false) +} + +/// See [SierraGenGroup::has_in_deps] for documentation. +pub fn has_in_deps_cycle( + _db: &dyn SierraGenGroup, + _cycle: &salsa::Cycle, + _type_id: &semantic::TypeId, + _needle: &semantic::TypeId, +) -> Maybe { + Ok(false) +} diff --git a/tests/bug_samples/issue2249.cairo b/tests/bug_samples/issue2249.cairo index e6f1ef04bf7..47d5d8eb867 100644 --- a/tests/bug_samples/issue2249.cairo +++ b/tests/bug_samples/issue2249.cairo @@ -11,3 +11,18 @@ fn simple_test() { assert_eq!(bst.value, 12); assert!(bst.left.is_none(), "left should be none"); } + +#[derive(Copy, Drop)] +pub enum RecursiveType { + Simple: felt252, + Direct: Span, + ExtraHop: Span<(felt252, RecursiveType)>, +} + +#[inline(never)] +fn use_value(_value: RecursiveType) {} + +#[test] +fn other_test() { + use_value(RecursiveType::Simple(12)); +}