-
Notifications
You must be signed in to change notification settings - Fork 551
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
prevent capturing mutable variables and allow accessing copiable vari… #6277
prevent capturing mutable variables and allow accessing copiable vari… #6277
Conversation
86ae991
to
701a771
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 7 files at r1, all commit messages.
Reviewable status: 3 of 7 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @TomerStarkware)
crates/cairo-lang-lowering/src/lower/block_builder.rs
line 308 at r1 (raw file):
self.semantics.captured.insert(member.clone(), var_usage.var_id); } }
Suggestion:
.collect();
let var_usage = generators::StructConstruct { inputs, ty: expr.ty, location }
.add(ctx, &mut self.statements);
let closure_info = ClosureInfo { members, snapshot_types };
for (var_usage, member) in zip!(inputs, usage.usage.keys()) {
if ctx.variables[var_usage.var_id].copyable.is_ok() && !usage.changes.contains_key(member) {
self.semantics.copiable_captured.insert(member.clone(), var_usage.var_id);
} else {
self.semantics.captured.insert(member.clone(), var_usage.var_id);
}
}
crates/cairo-lang-lowering/src/lower/block_builder.rs
line 545 at r1 (raw file):
fn var(&self, var: VariableId) -> &Variable { &self.ctx.variables[var] }
this isn't used.
Code quote:
fn var(&self, var: VariableId) -> &Variable {
&self.ctx.variables[var]
}
crates/cairo-lang-semantic/src/diagnostic.rs
line 858 at r1 (raw file):
} SemanticDiagnosticKind::MutableCapturedVariable => { "Mutable variables cannot be captured by closures".into()
Suggestion:
"Mutable variables capture by closure is not supported".into()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 3 of 7 files reviewed, 4 unresolved discussions (waiting on @orizi and @TomerStarkware)
crates/cairo-lang-semantic/src/expr/compute.rs
line 1648 at r1 (raw file):
// TODO(TomerStarkware): Add support for capturing mutable variables when then we have borrow. for (captured_var, expr) in usage.usage.iter().chain(usage.snap_usage.iter()) {
what about usage. changes? do we support that?
Code quote:
for (captured_var, expr) in usage.usage.iter().chain(usage.snap_usage.iter()) {
Shouldn't we skip the update if it is a copiable capture? Code quote: self.update(ctx, path, new_var).unwrap(); |
8073748
to
e5bcb49
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 1 of 7 files reviewed, 5 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi)
crates/cairo-lang-lowering/src/lower/block_builder.rs
line 308 at r1 (raw file):
self.semantics.captured.insert(member.clone(), var_usage.var_id); } }
Done.
crates/cairo-lang-lowering/src/lower/block_builder.rs
line 545 at r1 (raw file):
Previously, orizi wrote…
this isn't used.
Done.
crates/cairo-lang-lowering/src/lower/refs.rs
line 98 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
Shouldn't we skip the update if it is a copiable capture?
yes
crates/cairo-lang-semantic/src/diagnostic.rs
line 858 at r1 (raw file):
} SemanticDiagnosticKind::MutableCapturedVariable => { "Mutable variables cannot be captured by closures".into()
Done.
crates/cairo-lang-semantic/src/expr/compute.rs
line 1648 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
what about usage. changes? do we support that?
no it has the same problem
Code snippet:
let mut x = 0;
let c = || {x=x+1;};
x=1
c();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r2, 3 of 5 files at r3, all commit messages.
Reviewable status: 5 of 7 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 of 7 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware)
fix comment. Code quote: this can only right now if we take a snapshot |
Suggestion: Capture of mutable variables in a closure is not supported |
Previously, TomerStarkware wrote…
so you should go over usage.chages as well, right? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 7 files at r1, 1 of 5 files at r3.
Reviewable status: 5 of 7 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)
e5bcb49
to
9b52b01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 2 of 7 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi)
crates/cairo-lang-lowering/src/lower/refs.rs
line 128 at r3 (raw file):
Previously, ilyalesokhin-starkware wrote…
fix comment.
Done.
crates/cairo-lang-semantic/src/diagnostic.rs
line 858 at r3 (raw file):
} SemanticDiagnosticKind::MutableCapturedVariable => { "Mutable variables capture by closure is not supported".into()
Done.
crates/cairo-lang-semantic/src/expr/compute.rs
line 1648 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
so you should go over usage.chages as well, right?
isn't changes contained in usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 2 of 7 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)
crates/cairo-lang-lowering/src/lower/refs.rs
line 128 at r3 (raw file):
Previously, TomerStarkware wrote…
Done.
I don't see it
Previously, TomerStarkware wrote…
no |
9b52b01
to
bc60528
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 1 of 7 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi)
crates/cairo-lang-lowering/src/lower/refs.rs
line 128 at r3 (raw file):
Previously, ilyalesokhin-starkware wrote…
I don't see it
Done.
crates/cairo-lang-semantic/src/expr/compute.rs
line 1648 at r1 (raw file):
Previously, ilyalesokhin-starkware wrote…
no
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 4 of 5 files at r4, 2 of 2 files at r5.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @TomerStarkware)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)
bc60528
to
e768971
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 2 files at r6, all commit messages.
Reviewable status: 6 of 7 files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @TomerStarkware)
crates/cairo-lang-semantic/src/expr/compute.rs
line 1667 at r6 (raw file):
ctx.diagnostics.report(expr.stable_ptr(), MutableCapturedVariable); } }
Suggestion:
let mut reported = UnorderedHashSet::<_>::default();
// TODO(TomerStarkware): Add support for capturing mutable variables when then we have borrow.
for (captured_var, expr) in chain!(&usage.usage, &usage.snap_usage, &usage.changes)
{
let Some(var) = ctx.semantic_defs.get(&captured_var.base_var()) else {
// if the variable is not found in the semantic defs, it is closure parameter.
continue;
};
if var.is_mut() && reported.insert(expr.stable_ptr()) {
ctx.diagnostics.report(expr.stable_ptr(), MutableCapturedVariable);
}
}
e768971
to
f552142
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 6 of 7 files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @orizi)
crates/cairo-lang-semantic/src/expr/compute.rs
line 1667 at r6 (raw file):
ctx.diagnostics.report(expr.stable_ptr(), MutableCapturedVariable); } }
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed all commit messages.
Reviewable status: 6 of 7 files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @TomerStarkware)
crates/cairo-lang-semantic/src/expr/compute.rs
line 1658 at r7 (raw file):
for (captured_var, expr) in chain!(usage.usage.iter(), usage.snap_usage.iter(), usage.changes.iter()) {
this doesn't work?
Suggestion:
for (captured_var, expr) in chain!(&usage.usage, &usage.snap_usage, &usage.changes) {
…ables post closure usage
f552142
to
173806e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 6 of 7 files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @orizi)
crates/cairo-lang-semantic/src/expr/compute.rs
line 1658 at r7 (raw file):
Previously, orizi wrote…
this doesn't work?
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r8, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)
…ables post closure usage
This change is