From 00eaf97897bba777fb29887ee8a04a3ce09a3de5 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 15:52:10 -0300 Subject: [PATCH 1/2] fix: cast signed to u1 --- .../src/ssa/ssa_gen/context.rs | 5 +++- .../cast_signed_to_u1/Nargo.toml | 7 +++++ .../cast_signed_to_u1/src/main.nr | 4 +++ .../execute__tests__expanded.snap | 8 ++++++ ..._tests__force_brillig_false_inliner_0.snap | 26 +++++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test_programs/compile_success_empty/cast_signed_to_u1/Nargo.toml create mode 100644 test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__expanded.snap create mode 100644 tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__force_brillig_false_inliner_0.snap diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs index 16f18ed2d21..2de174e0936 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs @@ -724,7 +724,10 @@ impl<'a> FunctionContext<'a> { Type::Numeric(NumericType::Signed { bit_size: incoming_type_size }), NumericType::Unsigned { bit_size: target_type_size }, ) => { - if *incoming_type_size != target_type_size { + // If the target type size is 1 it means it's a cast to u1. In that case + // there's no need to cast to i1 first (which actually isn't a valid type), + // because the result will be the least significant bit anyway. + if *incoming_type_size != target_type_size && target_type_size != 1 { value = self.insert_safe_cast( value, NumericType::signed(target_type_size), diff --git a/test_programs/compile_success_empty/cast_signed_to_u1/Nargo.toml b/test_programs/compile_success_empty/cast_signed_to_u1/Nargo.toml new file mode 100644 index 00000000000..da669cfa642 --- /dev/null +++ b/test_programs/compile_success_empty/cast_signed_to_u1/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "cast_signed_to_u1" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr b/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr new file mode 100644 index 00000000000..3d28d91d17f --- /dev/null +++ b/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr @@ -0,0 +1,4 @@ +fn main() { + let x: i32 = 1; + let _ = x as u1; +} \ No newline at end of file diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__expanded.snap new file mode 100644 index 00000000000..211c13cfb20 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__expanded.snap @@ -0,0 +1,8 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +fn main() { + let x: i32 = 1; + let _: u1 = x as u1; +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__force_brillig_false_inliner_0.snap b/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__force_brillig_false_inliner_0.snap new file mode 100644 index 00000000000..7de940827c8 --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_empty/cast_signed_to_u1/execute__tests__force_brillig_false_inliner_0.snap @@ -0,0 +1,26 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: artifact +--- +{ + "noir_version": "[noir_version]", + "hash": "[hash]", + "abi": { + "parameters": [], + "return_type": null, + "error_types": {} + }, + "bytecode": [ + "func 0", + "current witness index : _0", + "private parameters indices : []", + "public parameters indices : []", + "return value indices : []" + ], + "debug_symbols": "XY5BCsQwCEXv4rqLWfcqw1BsaosgJtikMITefWyYQOlK/3/6tcJCc9km1jXuML4rzMYivE0SA2aO6m49B+hyykbkFty4byU00gyjFpEBDpTShvaE2mpGc/oagHTx6oErC13d+XGBge158UBjnIX+ci0abjR/Uyf942Qx0FKMrqTGPPsH", + "file_map": {}, + "names": [ + "main" + ], + "brillig_names": [] +} From 6c1f42c802f329e30e4dc6e9868900492e68772a Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 29 May 2025 16:05:23 -0300 Subject: [PATCH 2/2] Format --- .../compile_success_empty/cast_signed_to_u1/src/main.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr b/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr index 3d28d91d17f..322f742c5d3 100644 --- a/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr +++ b/test_programs/compile_success_empty/cast_signed_to_u1/src/main.nr @@ -1,4 +1,4 @@ fn main() { let x: i32 = 1; let _ = x as u1; -} \ No newline at end of file +}