From 937a8b9698231cbbb33db123c69bb0ab283dc55a Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 5 Apr 2024 13:23:08 +0800 Subject: [PATCH] core/magic_methods: Fix type of unary ops with primitive types --- nac3core/src/typecheck/magic_methods.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/nac3core/src/typecheck/magic_methods.rs b/nac3core/src/typecheck/magic_methods.rs index d785e92a3..59067b8a6 100644 --- a/nac3core/src/typecheck/magic_methods.rs +++ b/nac3core/src/typecheck/magic_methods.rs @@ -476,10 +476,24 @@ pub fn typeof_unaryop( return Err("The truth value of an array with more than one element is ambiguous".to_string()) } - Ok(if operand.obj_id(unifier).is_some_and(|id| PRIMITIVE_DEF_IDS.iter().any(|prim_id| id == prim_id)) { - Some(operand) - } else { - None + Ok(match *op { + Unaryop::Not => { + match operand.obj_id(unifier) { + Some(v) if v == PRIMITIVE_DEF_IDS.ndarray => Some(operand), + Some(_) => Some(primitives.bool), + _ => None + } + } + + Unaryop::Invert + | Unaryop::UAdd + | Unaryop::USub => { + if operand.obj_id(unifier).is_some_and(|id| PRIMITIVE_DEF_IDS.iter().any(|prim_id| id == prim_id)) { + Some(operand) + } else { + None + } + } }) }