Skip to content

Commit

Permalink
Fixed self-ref type detection. (#6455)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi committed Oct 7, 2024
1 parent 14b1d8c commit ef69e4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
5 changes: 4 additions & 1 deletion crates/cairo-lang-sierra-generator/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub trait SierraGenGroup: LoweringGroup + Upcast<dyn LoweringGroup> {

/// 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<bool>;

/// Returns the semantic type ids the type is directly dependent on.
Expand All @@ -89,6 +88,10 @@ pub trait SierraGenGroup: LoweringGroup + Upcast<dyn LoweringGroup> {
#[salsa::invoke(crate::types::type_dependencies)]
fn type_dependencies(&self, type_id: semantic::TypeId) -> Maybe<Arc<[semantic::TypeId]>>;

#[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<bool>;

/// Returns the [cairo_lang_sierra::program::FunctionSignature] object for the given function
/// id.
fn get_function_signature(
Expand Down
42 changes: 29 additions & 13 deletions crates/cairo-lang-sierra-generator/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
Ok(true)
}

/// See [SierraGenGroup::is_self_referential] for documentation.
pub fn is_self_referential(db: &dyn SierraGenGroup, type_id: semantic::TypeId) -> Maybe<bool> {
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.
Expand Down Expand Up @@ -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<bool> {
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<bool> {
Ok(false)
}
15 changes: 15 additions & 0 deletions tests/bug_samples/issue2249.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecursiveType>,
ExtraHop: Span<(felt252, RecursiveType)>,
}

#[inline(never)]
fn use_value(_value: RecursiveType) {}

#[test]
fn other_test() {
use_value(RecursiveType::Simple(12));
}

0 comments on commit ef69e4f

Please sign in to comment.