Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed closure type dependencies so it does not depend on the number o… #7258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/cairo-lang-lowering/src/borrow_check/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,45 @@ warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
//! > lowering_diagnostics

//! > lowering

//! > ==========================================================================

//! > capturing two destructables variables of the same type.

//! > test_runner_name
test_borrow_check

//! > function
fn foo(a: Destructable, b: Destructable) {
let c = || {
let Destructable { } = a;
let Destructable { } = b;
};
c.destruct();
}

//! > function_name
foo

//! > module_code
struct Destructable {}

impl MyDesruct of Destruct<Destructable> {
fn destruct(self: Destructable) nopanic {
let Destructable { } = self;
}
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters: v0: test::Destructable, v1: test::Destructable
blk0 (root):
Statements:
(v2: {[email protected]:9:13: 9:15}) <- struct_construct(v0{`a`}, v1{`b`})
(v3: ()) <- Generated `core::traits::Destruct::destruct` for {[email protected]:9:13: 9:15}(v2{`c`})
(v4: ()) <- struct_construct()
End:
Return(v4)
13 changes: 13 additions & 0 deletions crates/cairo-lang-lowering/src/lower/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,22 @@ impl BlockBuilder {
)
.collect();

let TypeLongId::Closure(closure_id) = expr.ty.lookup_intern(ctx.db) else {
unreachable!("Closure Expr should have a Closure type.");
};

// Assert that the closure type matches the input we pass to it.
assert!(
closure_id
.captured_types
.iter()
.eq(inputs.iter().map(|var_usage| &ctx.variables[var_usage.var_id].ty))
);

let var_usage =
generators::StructConstruct { inputs: inputs.clone(), ty: expr.ty, location }
.add(ctx, &mut self.statements);

let closure_info = ClosureInfo { members, snapshots };

for (var_usage, member) in izip!(inputs, usage.usage.keys()) {
Expand Down
3 changes: 0 additions & 3 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use cairo_lang_utils::{Intern, LookupIntern, OptionHelper, extract_matches, try_
use itertools::{Itertools, chain, zip_eq};
use num_bigint::BigInt;
use num_traits::ToPrimitive;
use salsa::InternKey;
use smol_str::SmolStr;

use super::inference::canonic::ResultNoErrEx;
Expand Down Expand Up @@ -1773,8 +1772,6 @@ fn compute_expr_closure_semantic(
usage.snap_usage.values().map(|item| wrap_in_snapshots(ctx.db, item.ty(), 1)),
chain!(usage.usage.values(), usage.changes.values()).map(|item| item.ty())
)
.sorted_by_key(|ty| ty.as_intern_id())
.dedup()
.collect_vec();

let ty = TypeLongId::Closure(ClosureTypeLongId {
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/items/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ pub fn find_closure_generated_candidate(
let mem_trait_generic_params = |trait_id, neg_impl_trait: Option<_>| {
let id = db.trait_generic_params(trait_id).unwrap().first().unwrap().id();
chain!(
closure_type_long.captured_types.iter().map(|ty| {
closure_type_long.captured_types.iter().unique().map(|ty| {
GenericParam::Impl(GenericParamImpl {
id,
concrete_trait: Maybe::Ok(db.intern_concrete_trait(ConcreteTraitLongId {
Expand Down
33 changes: 33 additions & 0 deletions tests/bug_samples/issue7234.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// A function which takes a closure as an argument and calls it.
// <F> denotes that F is a "Generic type parameter"
fn apply<F, +Drop<F>, impl func: core::ops::FnOnce<F, ()>, +Drop<func::Output>>(f: F) {
// ^ TODO: Try changing this to `Fn`.

f();
}
#[test]
fn main() {
// A non-copy type.
let greeting: ByteArray = "hello";
let farewell: ByteArray = "goodbye";

// Capture 2 variables: `greeting` by snapshot and
// `farewell` by value.
let diary = || {
// `greeting` is by snapshot: requires `Fn`.
println!("I said {}.", greeting);

// Using farewell by value requires `FnOnce`.
// Convert farewell to uppercase to demonstrate value capture through `into_iter`
let mut iter = farewell.into_iter();
let uppercase: ByteArray = iter.map(|c| if c >= 'a' {
c - 32
} else {
c
}).collect();
println!("Then I screamed {}!", uppercase);
};

// Call the function which applies the closure.
apply(diary);
}
1 change: 1 addition & 0 deletions tests/bug_samples/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod issue7083;
mod issue7095;
mod issue7155;
mod issue7233;
mod issue7234;
mod loop_break_in_match;
mod loop_only_change;
mod partial_param_local;
Expand Down
Loading