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

chore: fix unmutable environment #5016

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

michaeljklein
Copy link
Contributor

Description

Problem*

Resolves #4795

Summary*

This PR simply adds a check on mutable captures: when not &mut, a type checking error is thrown.

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

…ype checking for mutable capture that's not a reference, add two unit tests
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

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

Instead of adding a type check on a lambda's environment, can we avoid making the captured variables mutable in the first place? When the lookup is performed in name resolution there should be a check to look up a variable from an outer function. We should ensure there that the variable is introduced only as immutable if possible.

Comment on lines 319 to 326
let captured_vars =
match self.ensure_mutable_captures_have_ref(expr_id, lambda.captures) {
Err(err) => {
self.errors.push(err);
return Type::Error;
}
Ok(captured_vars) => captured_vars,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let captured_vars =
match self.ensure_mutable_captures_have_ref(expr_id, lambda.captures) {
Err(err) => {
self.errors.push(err);
return Type::Error;
}
Ok(captured_vars) => captured_vars,
};
let Ok(captured_vars) = self.ensure_mutable_captures_have_ref(expr_id, lambda.captures) else {
self.errors.push(err);
return Type::Error;
};

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.

Unmutable environment
2 participants