From 8a9652b8d2ad84641cc7dac205a6e31820200336 Mon Sep 17 00:00:00 2001 From: eric-therond Date: Thu, 29 Jun 2023 18:44:19 +0200 Subject: [PATCH] negation of an undefined value should return true (#21) Signed-off-by: eric-therond --- src/interpreter.rs | 5 +- tests/interpreter/cases/negation/tests.yaml | 66 +++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/interpreter/cases/negation/tests.yaml diff --git a/src/interpreter.rs b/src/interpreter.rs index 34eb9a0..95cb7c3 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -760,7 +760,10 @@ impl<'source> Interpreter<'source> { value, collection, } => self.eval_some_in(span, key, value, collection, stmts)?, - Literal::NotExpr { expr, .. } => matches!(self.eval_expr(expr)?, Value::Bool(false)), + Literal::NotExpr { expr, .. } => { + // https://github.com/open-policy-agent/opa/issues/1622#issuecomment-520547385 + matches!(self.eval_expr(expr)?, Value::Bool(false) | Value::Undefined) + } Literal::Every { span, key, diff --git a/tests/interpreter/cases/negation/tests.yaml b/tests/interpreter/cases/negation/tests.yaml new file mode 100644 index 0000000..3470bbf --- /dev/null +++ b/tests/interpreter/cases/negation/tests.yaml @@ -0,0 +1,66 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +cases: + - note: not-false + data: {} + modules: + - | + package test + + x[a] { + not 1 == 2 + a := "hello" + } + query: data.test + want_result: + x: + set!: [hello] + + - note: not-undefined + data: {} + modules: + - | + package test + + myequal(t) { + t == 2 + } + + x[a] { + not myequal(1) + a := "hello" + } + query: data.test + want_result: + x: + set!: [hello] + + # https://github.com/open-policy-agent/opa/issues/1877 + - note: not-more-undefined-OPA-INCOMPATIBLE + data: {} + modules: + - | + package test + + import future.keywords.if + + p if false # undefined value + + q1 { + not p # `not undefined` evaluates to true + } + + q2 = [p] # q2 = [undefined] = undefined + + q3 { + not [p] # `not undefined` evaluates to true + } + + q4 { + not q3 # `not true` making it undefined + } + query: data.test + want_result: + q1: true + q3: true