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

prevent capturing mutable variables and allow accessing copiable vari… #6277

Merged

Conversation

TomerStarkware
Copy link
Collaborator

@TomerStarkware TomerStarkware commented Aug 25, 2024

…ables post closure usage


This change is Reviewable

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch 2 times, most recently from 86ae991 to 701a771 Compare August 25, 2024 11:57
Copy link
Collaborator

@orizi orizi left a 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()

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a 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()) {

@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-lowering/src/lower/refs.rs line 98 at r1 (raw file):

            }
        }
    }

Shouldn't we skip the update if it is a copiable capture?

Code quote:

     self.update(ctx, path, new_var).unwrap();

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch 3 times, most recently from 8073748 to e5bcb49 Compare August 25, 2024 14:21
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a 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();

Copy link
Collaborator

@orizi orizi left a 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)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 5 of 7 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware)

@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-lowering/src/lower/refs.rs line 128 at r3 (raw file):

    ) -> Option<()> {
        if self.copiable_captured.get(path).is_some() {
            // Note, this can only right now if we take a snapshot of the variable (as the snapshot

fix comment.

Code quote:

this can only right now if we take a snapshot

@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-semantic/src/diagnostic.rs line 858 at r3 (raw file):

            }
            SemanticDiagnosticKind::MutableCapturedVariable => {
                "Mutable variables capture by closure is not supported".into()

Suggestion:

Capture of mutable variables in a closure is not supported

@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-semantic/src/expr/compute.rs line 1648 at r1 (raw file):

Previously, TomerStarkware wrote…

no it has the same problem

so you should go over usage.chages as well, right?

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a 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)

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch from e5bcb49 to 9b52b01 Compare August 26, 2024 20:28
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a 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?

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a 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

@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-semantic/src/expr/compute.rs line 1648 at r1 (raw file):

Previously, TomerStarkware wrote…

isn't changes contained in usage?

no

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch from 9b52b01 to bc60528 Compare August 27, 2024 09:02
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a 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.

Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

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)

Copy link
Collaborator

@orizi orizi left a 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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch from bc60528 to e768971 Compare September 3, 2024 14:14
Copy link
Collaborator

@orizi orizi left a 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);
        }
    }

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch from e768971 to f552142 Compare September 3, 2024 14:53
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a 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.

Copy link
Collaborator

@orizi orizi left a 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) {

@TomerStarkware TomerStarkware force-pushed the tomer/remove_closure_invalidation_on_copiable_access branch from f552142 to 173806e Compare September 3, 2024 14:57
Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a 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.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 1 files at r8, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

@TomerStarkware TomerStarkware added this pull request to the merge queue Sep 4, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 4, 2024
@TomerStarkware TomerStarkware added this pull request to the merge queue Sep 4, 2024
Merged via the queue into main with commit d3628c3 Sep 4, 2024
44 checks passed
@orizi orizi deleted the tomer/remove_closure_invalidation_on_copiable_access branch September 5, 2024 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants