-
Notifications
You must be signed in to change notification settings - Fork 248
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
fix(frontend)!: Restrict capturing mutable variable in lambdas #7488
Open
vezenovm
wants to merge
9
commits into
master
Choose a base branch
from
mv/restrict-mut-captures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
54e557a
do not allowing capturing mutable variable without a reference in lam…
vezenovm d0a07ba
specify mutable var in err msg
vezenovm ad988b5
fmt
vezenovm be06cc9
allow capturing mut variables used immutably
vezenovm f61e69e
one lvalue error info method
vezenovm 204cfd0
Update compiler/noirc_frontend/src/elaborator/mod.rs
vezenovm d63f272
merge conflicts after err location changes
vezenovm 12f78af
switch over test to use new strategy
vezenovm 636ddba
Merge branch 'master' into mv/restrict-mut-captures
vezenovm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -151,8 +151,24 @@ impl Elaborator<'_> { | |||||||||||||||||||||||||||||||||||||||
let (lvalue, lvalue_type, mutable) = self.elaborate_lvalue(assign.lvalue); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if !mutable { | ||||||||||||||||||||||||||||||||||||||||
let (name, location) = self.get_lvalue_name_and_location(&lvalue); | ||||||||||||||||||||||||||||||||||||||||
let (_, name, location) = self.get_lvalue_error_info(&lvalue); | ||||||||||||||||||||||||||||||||||||||||
self.push_err(TypeCheckError::VariableMustBeMutable { name, location }); | ||||||||||||||||||||||||||||||||||||||||
} else if let Some(lambda_context) = self.lambda_stack.last() { | ||||||||||||||||||||||||||||||||||||||||
// We must check whether the mutable variable we are attempting to assign | ||||||||||||||||||||||||||||||||||||||||
// comes from a lambda capture. All captures are immutable so we want to error | ||||||||||||||||||||||||||||||||||||||||
// if the user attempts to mutate a captured variable inside of a lambda without mutable references. | ||||||||||||||||||||||||||||||||||||||||
let capture_ids = | ||||||||||||||||||||||||||||||||||||||||
lambda_context.captures.iter().map(|var| var.ident.id).collect::<Vec<_>>(); | ||||||||||||||||||||||||||||||||||||||||
let (id, name, location) = self.get_lvalue_error_info(&lvalue); | ||||||||||||||||||||||||||||||||||||||||
let typ = self.interner.definition_type(id); | ||||||||||||||||||||||||||||||||||||||||
for capture_id in capture_ids { | ||||||||||||||||||||||||||||||||||||||||
if capture_id == id && !typ.is_mutable_ref() { | ||||||||||||||||||||||||||||||||||||||||
self.push_err(TypeCheckError::MutableCaptureWithoutRef { | ||||||||||||||||||||||||||||||||||||||||
name: name.clone(), | ||||||||||||||||||||||||||||||||||||||||
location, | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+160
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't try it but I thought of a way to avoid collecting the capture IDs into a Vec:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
self.unify_with_coercions(&expr_type, &lvalue_type, expression, expr_location, || { | ||||||||||||||||||||||||||||||||||||||||
|
@@ -331,20 +347,20 @@ impl Elaborator<'_> { | |||||||||||||||||||||||||||||||||||||||
(expr, self.interner.next_type_variable()) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
fn get_lvalue_name_and_location(&self, lvalue: &HirLValue) -> (String, Location) { | ||||||||||||||||||||||||||||||||||||||||
fn get_lvalue_error_info(&self, lvalue: &HirLValue) -> (DefinitionId, String, Location) { | ||||||||||||||||||||||||||||||||||||||||
match lvalue { | ||||||||||||||||||||||||||||||||||||||||
HirLValue::Ident(name, _) => { | ||||||||||||||||||||||||||||||||||||||||
let location = name.location; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if let Some(definition) = self.interner.try_definition(name.id) { | ||||||||||||||||||||||||||||||||||||||||
(definition.name.clone(), location) | ||||||||||||||||||||||||||||||||||||||||
(name.id, definition.name.clone(), location) | ||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||
("(undeclared variable)".into(), location) | ||||||||||||||||||||||||||||||||||||||||
(DefinitionId::dummy_id(), "(undeclared variable)".into(), location) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
HirLValue::MemberAccess { object, .. } => self.get_lvalue_name_and_location(object), | ||||||||||||||||||||||||||||||||||||||||
HirLValue::Index { array, .. } => self.get_lvalue_name_and_location(array), | ||||||||||||||||||||||||||||||||||||||||
HirLValue::Dereference { lvalue, .. } => self.get_lvalue_name_and_location(lvalue), | ||||||||||||||||||||||||||||||||||||||||
HirLValue::MemberAccess { object, .. } => self.get_lvalue_error_info(object), | ||||||||||||||||||||||||||||||||||||||||
HirLValue::Index { array, .. } => self.get_lvalue_error_info(array), | ||||||||||||||||||||||||||||||||||||||||
HirLValue::Dereference { lvalue, .. } => self.get_lvalue_error_info(lvalue), | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -446,8 +462,8 @@ impl Elaborator<'_> { | |||||||||||||||||||||||||||||||||||||||
Type::Slice(elem_type) => *elem_type, | ||||||||||||||||||||||||||||||||||||||||
Type::Error => Type::Error, | ||||||||||||||||||||||||||||||||||||||||
Type::String(_) => { | ||||||||||||||||||||||||||||||||||||||||
let (_lvalue_name, lvalue_location) = | ||||||||||||||||||||||||||||||||||||||||
self.get_lvalue_name_and_location(&lvalue); | ||||||||||||||||||||||||||||||||||||||||
let (_id, _lvalue_name, lvalue_location) = | ||||||||||||||||||||||||||||||||||||||||
self.get_lvalue_error_info(&lvalue); | ||||||||||||||||||||||||||||||||||||||||
self.push_err(TypeCheckError::StringIndexAssign { | ||||||||||||||||||||||||||||||||||||||||
location: lvalue_location, | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I was thinking instead of checking on assignment or mutable-ref creation or method calls we could go to the source of where a lambda capture is introduced into a lambda's scope and change that variable to be immutable. Is that possible?
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.
Ok, I looked over the lambda capture code some more and we don't create any new definition ids.
So instead of changing the definition (which we can't since it is not a copy and would change the original to be immutable), we should have a single function to verify whether something is mutable and have the check only in that function. I looked across the codebase and found that we actually have already accidentally duplicated this code.
&mut x
callscheck_can_mutate
whileobj.mutating_method()
callsverify_mutable_reference
. Assignment uses elaborate_lvalue` which returns a mutable boolean, although that method operates on an LValue rather than an ExprId so it is a bit different.We should unify these methods into one (probably keeping
elaborate_lvalue
) and add the mutable capture check on the Ident case for this shared method.elaborate_lvalue
should also call a new helper method to check the capture mutability on its Ident case as well.