From 00d1b9be9b4872985bcac3cdfc53373119484e0a Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 12 Apr 2024 15:35:36 +0800 Subject: [PATCH] core: Fix __inv__ for i8-based boolean operands --- nac3core/src/codegen/expr.rs | 9 ++++++++- nac3standalone/demo/src/ndarray.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index bec094c9..f360c788 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -1308,7 +1308,14 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>( let val = val.into_int_value(); match op { ast::Unaryop::Invert | ast::Unaryop::Not => { - ctx.builder.build_not(val, "not").map(Into::into).unwrap() + let not = ctx.builder.build_not(val, "not").unwrap(); + let not_bool = ctx.builder.build_and( + not, + not.get_type().const_int(1, false), + "", + ).unwrap(); + + not_bool.into() } _ => val.into(), } diff --git a/nac3standalone/demo/src/ndarray.py b/nac3standalone/demo/src/ndarray.py index 6dcc7ac2..49fdd209 100644 --- a/nac3standalone/demo/src/ndarray.py +++ b/nac3standalone/demo/src/ndarray.py @@ -455,6 +455,12 @@ def test_ndarray_inv(): output_ndarray_int32_2(x_int32) output_ndarray_int32_2(y_int32) + x_bool = np_full([2, 2], True) + y_bool = ~x_bool + + output_ndarray_bool_2(x_bool) + output_ndarray_bool_2(y_bool) + def test_ndarray_eq(): x = np_identity(2) y = x == np_full([2, 2], 0.0)