From 757162282983b98de93704a67fc945c358110eac Mon Sep 17 00:00:00 2001 From: orizi <104711814+orizi@users.noreply.github.com> Date: Wed, 25 Dec 2024 15:05:54 +0200 Subject: [PATCH] Made sure closure would have the same id on call and defintion. (#6923) --- crates/cairo-lang-lowering/src/ids.rs | 11 ++++++++++- tests/bug_samples/cairo_project.toml | 4 ++++ tests/bug_samples/issue6920.cairo | 11 +++++++++++ tests/bug_samples/lib.cairo | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/bug_samples/issue6920.cairo diff --git a/crates/cairo-lang-lowering/src/ids.rs b/crates/cairo-lang-lowering/src/ids.rs index 4e656fd3d52..e1ced62a241 100644 --- a/crates/cairo-lang-lowering/src/ids.rs +++ b/crates/cairo-lang-lowering/src/ids.rs @@ -359,7 +359,16 @@ pub trait SemanticFunctionIdEx { } impl SemanticFunctionIdEx for semantic::FunctionId { fn lowered(&self, db: &dyn LoweringGroup) -> FunctionId { - FunctionLongId::Semantic(*self).intern(db) + let ret = FunctionLongId::Semantic(*self).intern(db); + // If the function is generated, we need to check if it has a body, so we can return its + // generated function id. + // TODO(orizi): This is a hack, we should have a better way to do this. + if let Ok(Some(body)) = ret.body(db) { + if let Ok(id) = body.function_id(db) { + return id; + } + } + ret } } impl<'a> DebugWithDb for FunctionLongId { diff --git a/tests/bug_samples/cairo_project.toml b/tests/bug_samples/cairo_project.toml index eaf72a31dfd..b2ffc43638d 100644 --- a/tests/bug_samples/cairo_project.toml +++ b/tests/bug_samples/cairo_project.toml @@ -3,3 +3,7 @@ bug_samples = "." [config.global] edition = "2023_10" + +[config.global.experimental_features] +negative_impls = true +associated_item_constraints = true diff --git a/tests/bug_samples/issue6920.cairo b/tests/bug_samples/issue6920.cairo new file mode 100644 index 00000000000..38a5c0f21f2 --- /dev/null +++ b/tests/bug_samples/issue6920.cairo @@ -0,0 +1,11 @@ +#[test] +fn uninlined_closure_test() { + let uninlined_closure = + || { + let a: felt252 = 256; + format!( + "wow such amazing calcs {a} wow such amazing calcs {a} wow such amazing calcs {a}", + ); + }; + let _ = uninlined_closure(); +} diff --git a/tests/bug_samples/lib.cairo b/tests/bug_samples/lib.cairo index bfefab0a61b..84312857dd4 100644 --- a/tests/bug_samples/lib.cairo +++ b/tests/bug_samples/lib.cairo @@ -51,6 +51,7 @@ mod issue5967; mod issue6580; mod issue6623; mod issue6755; +mod issue6920; mod loop_break_in_match; mod loop_only_change; mod partial_param_local;