From 7e2b618ad68ae0e12a1fda28589e7f0825ebe4e0 Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Tue, 6 Aug 2024 10:24:46 +0100 Subject: [PATCH] Fixes assert in type_check_encode_buffer_empty. assert in type_check_encode_buffer_empty was causing the compiler to crash. We now throw a CompileError to avoid crashing. Fixes #6338. --- .../ast_node/expression/intrinsic_function.rs | 11 +++++++++-- .../encode_buffer_empty_with_args/Forc.lock | 8 ++++++++ .../encode_buffer_empty_with_args/Forc.toml | 8 ++++++++ .../json_abi_oracle.json | 1 + .../encode_buffer_empty_with_args/src/main.sw | 13 +++++++++++++ .../encode_buffer_empty_with_args/test.toml | 4 ++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/test.toml diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 9c750722f07..1d43acd2c02 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -93,7 +93,7 @@ impl ty::TyIntrinsicFunctionKind { type_check_contract_ret(handler, ctx, kind, arguments, type_arguments, span) } Intrinsic::EncodeBufferEmpty => { - type_check_encode_buffer_empty(ctx, kind, arguments, type_arguments, span) + type_check_encode_buffer_empty(handler, ctx, kind, arguments, type_arguments, span) } Intrinsic::EncodeBufferAppend => { type_check_encode_append(handler, ctx, kind, arguments, type_arguments, span) @@ -156,13 +156,20 @@ fn new_encoding_buffer_tuple( } fn type_check_encode_buffer_empty( + handler: &Handler, ctx: TypeCheckContext, kind: sway_ast::Intrinsic, arguments: &[Expression], _type_arguments: &[TypeArgument], span: Span, ) -> Result<(ty::TyIntrinsicFunctionKind, TypeId), ErrorEmitted> { - assert!(arguments.is_empty()); + if !arguments.is_empty() { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 0, + span, + })); + } let type_engine = ctx.engines.te(); let engines = ctx.engines(); diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.lock new file mode 100644 index 00000000000..671b855a7cb --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = "core" +source = "path+from-root-BABDDF1B461AF6DA" + +[[package]] +name = "encode_buffer_empty_with_args" +source = "member" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.toml new file mode 100644 index 00000000000..377ee70d0ae --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "encode_buffer_empty_with_args" +entry = "main.sw" + +[dependencies] +core = { path = "../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/json_abi_oracle.json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/json_abi_oracle.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/src/main.sw new file mode 100644 index 00000000000..c54e57c0de2 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/src/main.sw @@ -0,0 +1,13 @@ +library; + +struct Buffer { + buffer: (raw_ptr, u64, u64), +} + +impl Buffer { + pub fn new() -> Self { + Buffer { + buffer: __encode_buffer_empty(self), + } + } +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/test.toml new file mode 100644 index 00000000000..e51812b07ed --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/encode_buffer_empty_with_args/test.toml @@ -0,0 +1,4 @@ +category = "fail" + +# check: $()buffer: __encode_buffer_empty(self), +# nextln: $()Call to "encode_buffer_empty" expects 0 arguments