-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix move-compiler panic in case of inlined method use in parameter to…
… inlined method. (#8867) Includes test case illustrating original bug (#8516) plus a number of tests to check corner cases of multiple inlining and recursive inlining. (Several failed without this fix.) Also replace panic!("ICE expected function parameter to be a lambda") with a more useful diag output.
- Loading branch information
1 parent
b268ca4
commit d46d5a8
Showing
25 changed files
with
464 additions
and
10 deletions.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#[test_only] | ||
module aptos_std::math64_tests { | ||
use aptos_std::math64; | ||
|
||
#[test] | ||
fun test_nested_mul_div() { | ||
let a = math64::mul_div(1, 1, 1); | ||
assert!(math64::mul_div(1, a, 1) == 1, 0); | ||
} | ||
|
||
#[test] | ||
fun test_nested_mul_div2() { | ||
assert!(math64::mul_div(1, math64::mul_div(1, 1, 1),1) == 1, 0); | ||
} | ||
|
||
#[test] | ||
fun test_nested_mul_div3() { | ||
let a = math64::mul_div(1, math64::mul_div(1, 1, 1),1); | ||
assert!(a == 1, 0); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
third_party/move/move-compiler/tests/move_check/inlining/double_nesting.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,22 @@ | ||
module 0x42::mathtest { | ||
public inline fun fun1(a: u64, b: u64, c: u64): u64 { | ||
(((2 * (a as u128)) + (3 * (b as u128)) + (5 * (c as u128))) as u64) | ||
} | ||
} | ||
|
||
module 0x42::mathtest2 { | ||
public inline fun fun2(a: u64, b: u64, c: u64): u64 { | ||
(((7 * (a as u128)) + (11 * (b as u128)) + (13 * (c as u128))) as u64) | ||
} | ||
} | ||
|
||
module 0x42::test { | ||
use 0x42::mathtest; | ||
use 0x42::mathtest2; | ||
|
||
fun test_nested_fun1() { | ||
let a = mathtest::fun1(2, mathtest::fun1(3, mathtest2::fun2(4, 5, 6), 7), | ||
mathtest2::fun2(8, 9, mathtest::fun1(10, mathtest2::fun2(11, 12, 13), 14))); | ||
assert!(a == 81911, 0); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
third_party/move/move-compiler/tests/move_check/inlining/lambda.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,62 @@ | ||
module 0x42::LambdaTest1 { | ||
public inline fun inline_mul(a: u64, b: u64): u64 { | ||
a * b | ||
} | ||
|
||
public inline fun inline_apply1(f: |u64|u64, b: u64) : u64 { | ||
inline_mul(f(b) + 1, inline_mul(3, 4)) | ||
} | ||
|
||
public inline fun inline_apply(f: |u64|u64, b: u64) : u64 { | ||
f(b) | ||
} | ||
} | ||
|
||
module 0x42::LambdaTest2 { | ||
use 0x42::LambdaTest1; | ||
use std::vector; | ||
|
||
public inline fun foreach<T>(v: &vector<T>, action: |&T|) { // expected to be not implemented | ||
let i = 0; | ||
while (i < vector::length(v)) { | ||
action(vector::borrow(v, i)); | ||
i = i + 1; | ||
} | ||
} | ||
|
||
public fun test_inline_lambda() { | ||
let v = vector[1, 2, 3]; | ||
let product = 1; | ||
foreach(&v, |e| product = LambdaTest1::inline_mul(product, *e)); | ||
} | ||
|
||
public inline fun inline_apply2(g: |u64|u64, c: u64) : u64 { | ||
LambdaTest1::inline_apply1(|z|z, g(LambdaTest1::inline_mul(c, LambdaTest1::inline_apply(|x|x, 3)))) + 2 | ||
} | ||
|
||
public inline fun inline_apply3(g: |u64|u64, c: u64) : u64 { | ||
LambdaTest1::inline_apply1(g, | ||
LambdaTest1::inline_mul(c, LambdaTest1::inline_apply(|x| { | ||
LambdaTest1::inline_apply(|y|y, x) | ||
}, | ||
3))) + 4 | ||
} | ||
} | ||
|
||
module 0x42::LambdaTest { | ||
use 0x42::LambdaTest2; | ||
|
||
public inline fun inline_apply(f: |u64|u64, b: u64) : u64 { | ||
f(b) | ||
} | ||
|
||
public inline fun inline_apply_test() : u64 { | ||
LambdaTest2::inline_apply2(|x| x + 1, 3) + | ||
LambdaTest2::inline_apply2(|x| x * x, inline_apply(|y|y, 3)) | ||
} | ||
|
||
fun test_lambda() { | ||
let a = inline_apply_test(); | ||
assert!(a == 1, 0); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
third_party/move/move-compiler/tests/move_check/inlining/lambda_param.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,6 @@ | ||
error[E14003]: feature not supported in inlined functions | ||
┌─ tests/move_check/inlining/lambda_param.move:7:15 | ||
│ | ||
7 │ inline_apply(f, b) | ||
│ ^ Inlined function-typed parameter currently must be a literal lambda expression | ||
|
14 changes: 14 additions & 0 deletions
14
third_party/move/move-compiler/tests/move_check/inlining/lambda_param.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,14 @@ | ||
module 0x42::LambdaParam { | ||
public inline fun inline_apply(f: |u64|u64, b: u64) : u64 { | ||
f(b) | ||
} | ||
|
||
public inline fun inline_apply2(f: |u64|u64, b: u64) : u64 { | ||
inline_apply(f, b) | ||
} | ||
|
||
fun test_lambda_symbol_param() { | ||
let a = inline_apply2(|x| x, 3); | ||
assert!(a == 3, 0); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
third_party/move/move-compiler/tests/move_check/inlining/multiple_nesting.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,21 @@ | ||
error[E02004]: invalid 'module' declaration | ||
┌─ tests/move_check/inlining/multiple_nesting.move:4:9 | ||
│ | ||
4 │ mathtest2::mul_div2(c, a, b) | ||
│ ^^^^^^^^^^^^^^^^^^^ '0x42::mathtest2' uses '0x42::mathtest'. This 'use' relationship creates a dependency cycle. | ||
· | ||
11 │ mathtest::mul_div(b, a, c) | ||
│ ----------------- '0x42::mathtest' uses '0x42::mathtest2' | ||
|
||
error[E14001]: recursion during function inlining not allowed | ||
┌─ tests/move_check/inlining/multiple_nesting.move:4:20 | ||
│ | ||
4 │ mathtest2::mul_div2(c, a, b) | ||
│ ^^^^^^^^ cyclic inlining: -> mul_div -> mul_div2 | ||
|
||
error[E14001]: recursion during function inlining not allowed | ||
┌─ tests/move_check/inlining/multiple_nesting.move:11:19 | ||
│ | ||
11 │ mathtest::mul_div(b, a, c) | ||
│ ^^^^^^^ cyclic inlining: -> mul_div2 -> mul_div | ||
|
23 changes: 23 additions & 0 deletions
23
third_party/move/move-compiler/tests/move_check/inlining/multiple_nesting.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,23 @@ | ||
module 0x42::mathtest { | ||
use 0x42::mathtest2; | ||
public inline fun mul_div(a: u64, b: u64, c: u64): u64 { | ||
mathtest2::mul_div2(c, a, b) | ||
} | ||
} | ||
|
||
module 0x42::mathtest2 { | ||
use 0x42::mathtest; | ||
public inline fun mul_div2(a: u64, b: u64, c: u64): u64 { | ||
mathtest::mul_div(b, a, c) | ||
} | ||
} | ||
|
||
module 0x42::test { | ||
use 0x42::mathtest; | ||
use 0x42::mathtest2; | ||
fun test_nested_mul_div() { | ||
let a = mathtest::mul_div(1, mathtest::mul_div(1, 1, 1), | ||
mathtest2::mul_div2(1, 1, 1)); | ||
assert!(a == 1, 0); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
third_party/move/move-compiler/tests/move_check/inlining/nested_mul.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,13 @@ | ||
module 0x42::mathtest { | ||
public inline fun mul_div(a: u64, b: u64, c: u64): u64 { | ||
(((a as u128) * (b as u128) / (c as u128)) as u64) | ||
} | ||
} | ||
|
||
module 0x42::test { | ||
use 0x42::mathtest; | ||
fun test_nested_mul_div() { | ||
let a = mathtest::mul_div(1, mathtest::mul_div(1, 1, 1),1); | ||
assert!(a == 1, 0); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
third_party/move/move-compiler/tests/move_check/inlining/order_sensitive.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,48 @@ | ||
module 0x42::OrderSensitiveTest1 { | ||
public inline fun inline_fun1(a: u64, b: u64): u64 { | ||
a * b | ||
} | ||
|
||
public inline fun inline_fun2(a: u64, b: u64): u64 { | ||
inline_fun1(a, b) + 2 * inline_fun3(a, b) | ||
} | ||
|
||
public inline fun inline_fun3(a: u64, b: u64): u64 { | ||
a * b + 2 | ||
} | ||
} | ||
|
||
module 0x42::OrderSensitiveTest2 { | ||
use 0x42::OrderSensitiveTest1; | ||
|
||
public inline fun inline_fun1(a: u64, b: u64): u64 { | ||
a * b + 3 | ||
} | ||
|
||
public inline fun inline_fun2(a: u64, b: u64): u64 { | ||
OrderSensitiveTest1::inline_fun2(inline_fun1(a, b), inline_fun3(a, b)) | ||
+ 3 * inline_fun1(a, b) | ||
+ 5 * inline_fun3(a, b) | ||
} | ||
|
||
public inline fun inline_fun3(a: u64, b: u64): u64 { | ||
a * b + 4 | ||
} | ||
} | ||
|
||
module 0x42::OrderSensitiveTest3 { | ||
use 0x42::OrderSensitiveTest2; | ||
|
||
public inline fun fun1(a: u64, b: u64): u64 { | ||
a * b + 5 | ||
} | ||
|
||
public fun fun2(a: u64, b: u64): u64 { | ||
OrderSensitiveTest2::inline_fun2(7 * fun1(a, b), b) | ||
+ 9 * fun3(a, b) | ||
} | ||
|
||
public inline fun fun3(a: u64, b: u64): u64 { | ||
a * b + 6 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
third_party/move/move-compiler/tests/move_check/inlining/recursive_nesting.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,32 @@ | ||
module 0x42::mathtest { | ||
public inline fun mul_div(a: u64, b: u64, c: u64): u64 { | ||
(((a as u128) * (b as u128) / (c as u128)) as u64) | ||
} | ||
} | ||
|
||
module 0x42::mathtest2 { | ||
use 0x42::mathtest; | ||
public inline fun mul_div2(a: u64, b: u64, c: u64): u64 { | ||
mathtest::mul_div(b, a, c) | ||
} | ||
} | ||
|
||
module 0x42::mathtest3 { | ||
use 0x42::mathtest2; | ||
public inline fun mul_div3(a: u64, b: u64, c: u64): u64 { | ||
mathtest2::mul_div2(b, a, c) | ||
} | ||
} | ||
|
||
module 0x42::test { | ||
use 0x42::mathtest; | ||
use 0x42::mathtest2; | ||
use 0x42::mathtest3; | ||
fun test_nested_mul_div() { | ||
let a = mathtest::mul_div( | ||
mathtest3::mul_div3(1, 1, 1), | ||
mathtest::mul_div(1, 1, 1), | ||
mathtest2::mul_div2(1, 1, 1)); | ||
assert!(a == 1, 0); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
third_party/move/move-compiler/transactional-tests/tests/inlining/nested_lambda.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,4 @@ | ||
processed 2 tasks | ||
|
||
task 1 'run'. lines 13-13: | ||
return values: 3 |
13 changes: 13 additions & 0 deletions
13
third_party/move/move-compiler/transactional-tests/tests/inlining/nested_lambda.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,13 @@ | ||
//# publish | ||
module 0x42::Test { | ||
|
||
public inline fun apply(f: |u64, u64|u64, x: u64, y: u64): u64 { | ||
f(x, y) | ||
} | ||
|
||
public fun test(): u64 { | ||
apply(|x, y| x + y, 1, apply(|x, y| x * y, 2, 1)) | ||
} | ||
} | ||
|
||
//# run 0x42::Test::test |
4 changes: 4 additions & 0 deletions
4
third_party/move/move-compiler/transactional-tests/tests/inlining/nested_lambda_module.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,4 @@ | ||
processed 3 tasks | ||
|
||
task 2 'run'. lines 17-17: | ||
return values: 3 |
17 changes: 17 additions & 0 deletions
17
third_party/move/move-compiler/transactional-tests/tests/inlining/nested_lambda_module.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::Test1 { | ||
public inline fun apply(f: |u64, u64|u64, x: u64, y: u64): u64 { | ||
f(x, y) | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x42::Test { | ||
use 0x42::Test1; | ||
|
||
public fun test(): u64 { | ||
Test1::apply(|x, y| x + y, 1, Test1::apply(|x, y| x * y, 2, 1)) | ||
} | ||
} | ||
|
||
//# run 0x42::Test::test |
4 changes: 4 additions & 0 deletions
4
third_party/move/move-compiler/transactional-tests/tests/inlining/two_level_modules.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,4 @@ | ||
processed 3 tasks | ||
|
||
task 2 'run'. lines 21-21: | ||
return values: 18 |
21 changes: 21 additions & 0 deletions
21
third_party/move/move-compiler/transactional-tests/tests/inlining/two_level_modules.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,21 @@ | ||
//# publish | ||
module 0x42::TwoLevelTestModule { | ||
public inline fun f1(x: u64): u64 { | ||
x * 3 | ||
} | ||
|
||
public inline fun f2(x: u64): u64 { | ||
2 * x | ||
} | ||
} | ||
|
||
//# publish | ||
module 0x42::TwoLevelTestMain { | ||
use 0x42::TwoLevelTestModule; | ||
|
||
public fun test(): u64 { | ||
TwoLevelTestModule::f1(TwoLevelTestModule::f2(3)) | ||
} | ||
} | ||
|
||
//# run 0x42::TwoLevelTestMain::test |
4 changes: 4 additions & 0 deletions
4
..._party/move/move-compiler/transactional-tests/tests/inlining/two_level_modules_inline.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,4 @@ | ||
processed 3 tasks | ||
|
||
task 2 'run'. lines 25-25: | ||
return values: 18 |
Oops, something went wrong.