Skip to content

Commit

Permalink
prevents closure parameters from being declared as refrences (#7078)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerStarkware authored Jan 15, 2025
1 parent 3cbf91b commit e635273
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/cairo-lang-semantic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,9 @@ impl DiagnosticEntry for SemanticDiagnostic {
SemanticDiagnosticKind::RefClosureArgument => {
"Arguments to closure functions cannot be references".into()
}
SemanticDiagnosticKind::RefClosureParam => {
"Closure parameters cannot be references".into()
}
SemanticDiagnosticKind::MutableCapturedVariable => {
"Capture of mutable variables in a closure is not supported".into()
}
Expand Down Expand Up @@ -1422,6 +1425,7 @@ pub enum SemanticDiagnosticKind {
shadowed_function_name: SmolStr,
},
RefClosureArgument,
RefClosureParam,
MutableCapturedVariable,
NonTraitTypeConstrained {
identifier: SmolStr,
Expand Down
4 changes: 4 additions & 0 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,10 @@ fn compute_expr_closure_semantic(
vec![]
};

params.iter().filter(|param| param.mutability == Mutability::Reference).for_each(|param| {
new_ctx.diagnostics.report(param.stable_ptr(ctx.db.upcast()), RefClosureParam);
});

new_ctx
.semantic_defs
.extend(new_ctx.environment.variables.iter().map(|(_, var)| (var.id(), var.clone())));
Expand Down
25 changes: 25 additions & 0 deletions crates/cairo-lang-semantic/src/expr/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,28 @@ error: Cannot assign to an immutable variable.
--> lib.cairo:3:9
a = a + 2
^^^^^^^^^

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

//! > Closures with ref arguments.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() {
let _ = |ref a| {
a = a + 2
};
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error: Closure parameters cannot be references
--> lib.cairo:2:14
let _ = |ref a| {
^^^^^

0 comments on commit e635273

Please sign in to comment.