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

[move-compiler-v2] clean up a few remaining issues in lambda parser/front-end code #15365

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions third_party/move/move-compiler-v2/src/bytecode_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Parts of the project are originally copyright © Meta Platforms, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::{experiments::Experiment, Options};
use codespan_reporting::diagnostic::Severity;
use ethnum::U256;
use itertools::Itertools;
Expand Down Expand Up @@ -324,6 +325,14 @@ impl<'env> Generator<'env> {
let loc = env.get_node_loc(id);
env.diag(severity, &loc, msg.as_ref())
}

fn check_if_lambdas_enabled(&self) -> bool {
let options = self
.env()
.get_extension::<Options>()
.expect("Options is available");
options.experiment_on(Experiment::LAMBDA_VALUES)
}
}

// ======================================================================================
Expand Down Expand Up @@ -480,14 +489,23 @@ impl<'env> Generator<'env> {
self.emit_with(*id, |attr| Bytecode::SpecBlock(attr, spec));
},
// TODO(LAMBDA)
ExpData::Lambda(id, _, _, _, _) => self.error(
ExpData::Lambda(id, _, _, _, _) =>
self.error(
*id,
"Function-typed values not yet supported except as parameters to calls to inline functions",
if self.check_if_lambdas_enabled() {
"Function-typed values not yet implemented except as parameters to calls to inline functions"
} else {
"Function-typed values not yet supported except as parameters to calls to inline functions"
}
),
// TODO(LAMBDA)
ExpData::Invoke(id, _exp, _) => self.error(
*id,
"Calls to function values other than inline function parameters not yet supported",
if self.check_if_lambdas_enabled() {
"Calls to function values other than inline function parameters not yet implemented"
} else {
"Calls to function values other than inline function parameters not yet supported"
}
),
ExpData::Quant(id, _, _, _, _, _) => {
self.internal_error(*id, "unsupported specification construct")
Expand Down Expand Up @@ -564,10 +582,16 @@ impl<'env> Generator<'env> {
Constant::Bool(false)
}
},
// TODO(LAMBDA)
Value::Function(_mid, _fid) => {
self.error(
id,
"Function-typed values not yet supported except as parameters to calls to inline functions");
if self.check_if_lambdas_enabled() {
"Function-typed values not yet implemented except as parameters to calls to inline functions"
} else {
"Function-typed values not yet supported except as parameters to calls to inline functions"
}
);
Constant::Bool(false)
},
}
Expand Down Expand Up @@ -794,7 +818,11 @@ impl<'env> Generator<'env> {
// TODO(LAMBDA)
Operation::EarlyBind => self.error(
id,
"Function-typed values not yet supported except as parameters to calls to inline functions",
if self.check_if_lambdas_enabled() {
"Function-typed values not yet implemented except as parameters to calls to inline functions"
} else {
"Function-typed values not yet supported except as parameters to calls to inline functions"
},
),
Operation::TestVariants(mid, sid, variants) => {
self.gen_test_variants(targets, id, mid.qualified(*sid), variants, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ impl<'a> ExpRewriterFunctions for LambdaLifter<'a> {
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 is not yet supported."
"The body of a lambdas expression with `store` ability currently must be a simple call to an existing `public` function, with lambda params the same as the *final* arguments to the function call."
);
return None;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl ModuleGenerator {
ctx.error(
loc,
format!(
"Unexpected type: {}",
"Unimplemented type: {}",
ty.display(&ctx.env.get_type_display_ctx())
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

Diagnostics:
warning: Unused parameter `f`. Consider removing or prefixing with an underscore: `_f`
┌─ tests/checking/inlining/function_name_shadowing.move:20:28
20 │ public inline fun quux(f:|u64, u64|u64, g:|u64|u64, i:|u8|u8, a: u64, b: u64): u64 {
│ ^

warning: Unused parameter `g`. Consider removing or prefixing with an underscore: `_g`
┌─ tests/checking/inlining/function_name_shadowing.move:20:45
20 │ public inline fun quux(f:|u64, u64|u64, g:|u64|u64, i:|u8|u8, a: u64, b: u64): u64 {
│ ^

warning: Unused parameter `i`. Consider removing or prefixing with an underscore: `_i`
┌─ tests/checking/inlining/function_name_shadowing.move:20:57
20 │ public inline fun quux(f:|u64, u64|u64, g:|u64|u64, i:|u8|u8, a: u64, b: u64): u64 {
│ ^

// -- Model dump before bytecode pipeline
module 0x42::OtherModule {
public fun g(a: u64,b: u64): u64 {
Add<u64>(a, b)
}
public fun h(a: u64,b: u64): u64 {
Add<u64>(Mul<u64>(2, a), b)
}
} // end 0x42::OtherModule
module 0x42::Test {
use 0x42::OtherModule::{g}; // resolved as: 0x42::OtherModule
public fun f(a: u64,b: u64): u64 {
Mul<u64>(a, b)
}
public inline fun quux(f: |(u64, u64)|u64,g: |u64|u64,i: |u8|u8,a: u64,b: u64): u64 {
Mul<u64>(Mul<u64>(Test::f(a, b), OtherModule::g(a, b)), OtherModule::h(a, b))
}
public fun test_shadowing(): u64 {
Mul<u64>(Mul<u64>(Test::f(10, 2), OtherModule::g(10, 2)), OtherModule::h(10, 2))
}
} // end 0x42::Test

// -- Sourcified model before bytecode pipeline
module 0x42::OtherModule {
public fun g(a: u64, b: u64): u64 {
a + b
}
public fun h(a: u64, b: u64): u64 {
2 * a + b
}
}
module 0x42::Test {
use 0x42::OtherModule;
public fun f(a: u64, b: u64): u64 {
a * b
}
public inline fun quux(f: |(u64, u64)|u64, g: |u64|u64, i: |u8|u8, a: u64, b: u64): u64 {
f(a, b) * OtherModule::g(a, b) * OtherModule::h(a, b)
}
public fun test_shadowing(): u64 {
f(10, 2) * OtherModule::g(10, 2) * OtherModule::h(10, 2)
}
}


============ bytecode verification succeeded ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//# publish
module 0x42::OtherModule {
public fun g(a: u64, b: u64): u64 {
a + b
}

public fun h(a: u64, b: u64): u64 {
2 * a + b
}
}

//# publish
module 0x42::Test {
use 0x42::OtherModule::g;

public fun f(a: u64, b: u64): u64 {
a * b
}

public inline fun quux(f:|u64, u64|u64, g:|u64|u64, i:|u8|u8, a: u64, b: u64): u64 {
use 0x42::OtherModule::h;
f(a, b) * g(a, b) * h(a, b)
}

public fun test_shadowing(): u64 {
quux(|a, b| a - b, |a| a + 2, |b| 255u8-b, 10, 2)
}
}

//# run 0x42::Test::test_shadowing
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,3 @@ error: tuple type `()` is not allowed as a type argument (type was inferred)
│ ^
= required by instantiating type parameter `T` of function `foreach`

error: function type `|u64|u64` is not allowed as a field type
┌─ tests/checking/typing/lambda.move:81:12
81 │ f: |u64|u64, // expected lambda not allowed
│ ^^^^^^^^
= required by declaration of field `f`
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,3 @@ error: cannot pass `|&u64|u64 with copy+store` to a function which expects argum
73 │ foreach(&v, |e: &u64| { sum = sum + *e; *e }) // expected to have wrong result type of lambda
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: function type `|u64|u64` is not allowed as a field type
┌─ tests/checking/typing/lambda_typed.move:81:12
81 │ f: |u64|u64, // expected lambda not allowed
│ ^^^^^^^^
= required by declaration of field `f`
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,13 @@ module 0x8675309::M {


Diagnostics:
error: Calls to function values other than inline function parameters not yet supported
error: Calls to function values other than inline function parameters not yet implemented
┌─ tests/lambda/inline-parity/subtype_args.move:24:9
24 │ f(&mut 0, &mut 0);
│ ^^^^^^^^^^^^^^^^^

error: Calls to function values other than inline function parameters not yet supported
error: Calls to function values other than inline function parameters not yet implemented
┌─ tests/lambda/inline-parity/subtype_args.move:25:9
25 │ f(&0, &mut 0);
Expand Down
8 changes: 0 additions & 8 deletions third_party/move/move-compiler-v2/tests/lambda/lambda.exp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,3 @@ error: tuple type `()` is not allowed as a type argument (type was inferred)
│ ^
= required by instantiating type parameter `T` of function `foreach`

error: function type `|u64|u64` is not allowed as a field type
┌─ tests/lambda/lambda.move:81:12
81 │ f: |u64|u64, // expected lambda not allowed
│ ^^^^^^^^
= required by declaration of field `f`
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,3 @@ error: tuple type `()` is not allowed as a type argument (type was inferred)
│ ^
= required by instantiating type parameter `T` of function `foreach`

error: function type `|u64|u64` is not allowed as a field type
┌─ tests/lambda/lambda.move:81:12
81 │ f: |u64|u64, // expected lambda not allowed
│ ^^^^^^^^
= required by declaration of field `f`
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public fun M::fun_result_lambda_not_allowed(): |u64| {


Diagnostics:
error: Unexpected type: |u64|
error: Unimplemented type: |u64|
┌─ tests/lambda/lambda4.move:89:16
89 │ public fun fun_result_lambda_not_allowed(): |u64| { // expected lambda not allowed
Expand Down
Loading
Loading