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..322f742c5d3 --- /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; +} 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": [] +}