-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[move-compiler-v2] clean up a few remaining issues in lambda parser/front-end code #15365
Open
brmataptos
wants to merge
1
commit into
09-27-add_parser_code_for_lambda_types
Choose a base branch
from
11-21-lambda-compiler-completion
base: 09-27-add_parser_code_for_lambda_types
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.
+6,417
−651
Open
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
//! not modify free variables. | ||
//! | ||
//! Lambda lifting rewrites lambda expressions into construction | ||
//! of *closures*. A closure refers to a function and contains a partial list | ||
//! of arguments for that function, essentially currying it. We use the | ||
//! of *closures* using the `EarlyBind` operation. A closure refers to a function and contains a list | ||
//! of "early bound" leading arguments for that function, essentially currying it. We use the | ||
//! `EarlyBind` operation to construct a closure from a function and set of arguemnts, | ||
//! which must be the first `k` arguments to the function argument list. | ||
//! | ||
|
@@ -204,7 +204,9 @@ impl<'a> LambdaLifter<'a> { | |
/// - `closure_args` = corresponding expressions to provide as actual arg for each param | ||
/// - `param_index_mapping` = for each free var which is a Parameter from the enclosing function, | ||
/// a mapping from index there to index in the params list | ||
fn get_params_for_freevars(&mut self) -> (Vec<Parameter>, Vec<Exp>, BTreeMap<usize, usize>) { | ||
fn get_params_for_freevars( | ||
&mut self, | ||
) -> Option<(Vec<Parameter>, Vec<Exp>, BTreeMap<usize, usize>)> { | ||
let env = self.fun_env.module_env.env; | ||
let mut closure_args = vec![]; | ||
|
||
|
@@ -213,6 +215,9 @@ impl<'a> LambdaLifter<'a> { | |
// functions (courtesy of #12317) | ||
let mut param_index_mapping = BTreeMap::new(); | ||
let mut params = vec![]; | ||
let ty_params = self.fun_env.get_type_parameters_ref(); | ||
let ability_inferer = AbilityInferer::new(env, ty_params); | ||
let mut saw_error = false; | ||
|
||
for (used_param_count, (param, var_info)) in | ||
mem::take(&mut self.free_params).into_iter().enumerate() | ||
|
@@ -229,6 +234,18 @@ impl<'a> LambdaLifter<'a> { | |
name.display(env.symbol_pool()) | ||
), | ||
); | ||
saw_error = true; | ||
} | ||
let param_abilities = ability_inferer.infer_abilities(&ty).1; | ||
if !param_abilities.has_copy() { | ||
env.error( | ||
&loc, | ||
&format!( | ||
"captured variable `{}` must have a value with `copy` ability", // TODO(LAMBDA) | ||
name.display(env.symbol_pool()) | ||
), | ||
); | ||
saw_error = true; | ||
} | ||
params.push(Parameter(name, ty.clone(), loc.clone())); | ||
let new_id = env.new_node(loc, ty); | ||
|
@@ -252,6 +269,7 @@ impl<'a> LambdaLifter<'a> { | |
name.display(env.symbol_pool()) | ||
), | ||
); | ||
saw_error = true; | ||
} | ||
params.push(Parameter(name, ty.clone(), loc.clone())); | ||
let new_id = env.new_node(loc, ty); | ||
|
@@ -261,7 +279,11 @@ impl<'a> LambdaLifter<'a> { | |
closure_args.push(ExpData::LocalVar(new_id, name).into_exp()) | ||
} | ||
|
||
(params, closure_args, param_index_mapping) | ||
if !saw_error { | ||
Some((params, closure_args, param_index_mapping)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn get_arg_if_simple(arg: &Exp) -> Option<&Exp> { | ||
|
@@ -606,10 +628,13 @@ impl<'a> ExpRewriterFunctions for LambdaLifter<'a> { | |
|
||
fn rewrite_assign(&mut self, _node_id: NodeId, lhs: &Pattern, _rhs: &Exp) -> Option<Exp> { | ||
for (node_id, name) in lhs.vars() { | ||
self.free_locals.insert(name, VarInfo { | ||
node_id, | ||
modified: true, | ||
}); | ||
self.free_locals.insert( | ||
name, | ||
VarInfo { | ||
node_id, | ||
modified: true, | ||
}, | ||
); | ||
} | ||
None | ||
} | ||
|
@@ -618,16 +643,22 @@ impl<'a> ExpRewriterFunctions for LambdaLifter<'a> { | |
if matches!(oper, Operation::Borrow(ReferenceKind::Mutable)) { | ||
match args[0].as_ref() { | ||
ExpData::LocalVar(node_id, name) => { | ||
self.free_locals.insert(*name, VarInfo { | ||
node_id: *node_id, | ||
modified: true, | ||
}); | ||
self.free_locals.insert( | ||
*name, | ||
VarInfo { | ||
node_id: *node_id, | ||
modified: true, | ||
}, | ||
); | ||
}, | ||
ExpData::Temporary(node_id, param) => { | ||
self.free_params.insert(*param, VarInfo { | ||
node_id: *node_id, | ||
modified: true, | ||
}); | ||
self.free_params.insert( | ||
*param, | ||
VarInfo { | ||
node_id: *node_id, | ||
modified: true, | ||
}, | ||
); | ||
}, | ||
_ => {}, | ||
} | ||
|
@@ -669,7 +700,11 @@ impl<'a> ExpRewriterFunctions for LambdaLifter<'a> { | |
// param_index_mapping = for each free var which is a Parameter from the enclosing function, | ||
// a mapping from index there to index in the params list; other free vars are | ||
// substituted automatically by using the same symbol for the param | ||
let (mut params, mut closure_args, param_index_mapping) = self.get_params_for_freevars(); | ||
let Some((mut params, mut closure_args, param_index_mapping)) = | ||
self.get_params_for_freevars() | ||
else { | ||
return None; | ||
}; | ||
|
||
// Some(ExpData::Invalid(env.clone_node(id)).into_exp()); | ||
// Add lambda args. For dealing with patterns in lambdas (`|S{..}|e`) we need | ||
|
@@ -732,6 +767,15 @@ impl<'a> ExpRewriterFunctions for LambdaLifter<'a> { | |
let body = ExpRewriter::new(env, &mut replacer).rewrite_exp(body.clone()); | ||
let fun_id = FunId::new(fun_name); | ||
let params_types = params.iter().map(|param| param.get_type()).collect(); | ||
if abilities.has_store() { | ||
let loc = env.get_node_loc(id); | ||
env.error( | ||
&loc, | ||
// TODO(LAMBDA) | ||
"Lambdas expressions with `store` ability currently may only be a simple call to an existing `public` function. This lambda expression requires defining a `public` helper function, which might affect module upgradeability and is not yet supported." | ||
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. This error message looks a little bit confusing for users (e.g what is a |
||
); | ||
return None; | ||
}; | ||
self.lifted.push(ClosureFunction { | ||
loc: lambda_loc.clone(), | ||
fun_id, | ||
|
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
36 changes: 36 additions & 0 deletions
36
third_party/move/move-compiler-v2/tests/checking/inlining/function_name_shadowing.exp
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
Diagnostics: | ||
warning: Unused parameter `f`. Consider removing or prefixing with an underscore: `_f` | ||
┌─ tests/checking/inlining/function_name_shadowing.move:8:28 | ||
│ | ||
8 │ public inline fun quux(f:|u64, u64|u64, a: u64, b: u64): u64 { | ||
│ ^ | ||
|
||
// -- Model dump before bytecode pipeline | ||
module 0x42::Test { | ||
public fun f(a: u64,b: u64): u64 { | ||
Mul<u64>(a, b) | ||
} | ||
public inline fun quux(f: |(u64, u64)|u64,a: u64,b: u64): u64 { | ||
Test::f(a, b) | ||
} | ||
public fun test_shadowing(): u64 { | ||
Test::f(10, 2) | ||
} | ||
} // end 0x42::Test | ||
|
||
// -- Sourcified model before bytecode pipeline | ||
module 0x42::Test { | ||
public fun f(a: u64, b: u64): u64 { | ||
a * b | ||
} | ||
public inline fun quux(f: |(u64, u64)|u64, a: u64, b: u64): u64 { | ||
f(a, b) | ||
} | ||
public fun test_shadowing(): u64 { | ||
f(10, 2) | ||
} | ||
} | ||
|
||
|
||
============ bytecode verification succeeded ======== |
17 changes: 17 additions & 0 deletions
17
third_party/move/move-compiler-v2/tests/checking/inlining/function_name_shadowing.move
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//# publish | ||
module 0x42::Test { | ||
|
||
public fun f(a: u64, b: u64): u64 { | ||
a * b | ||
} | ||
|
||
public inline fun quux(f:|u64, u64|u64, a: u64, b: u64): u64 { | ||
f(a, b) | ||
} | ||
|
||
public fun test_shadowing(): u64 { | ||
quux(|a, b| a - b, 10, 2) | ||
} | ||
} | ||
|
||
//# run 0x42::Test::test_shadowing |
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.
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.
Is there a test case for this error?