Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make partial evaluation more aggressive #1098

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions cedar-policy-core/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,13 @@ impl<'e> Evaluator<'e> {
ExprKind::And { left, right } => {
match self.partial_interpret(left, slots)? {
// PE Case
PartialValue::Residual(e) => {
Ok(PartialValue::Residual(Expr::and(e, right.as_ref().clone())))
}
PartialValue::Residual(r) => Ok(PartialValue::Residual(Expr::and(
r,
match self.partial_interpret(right, slots) {
Ok(right) => Expr::from(right),
Err(_) => right.as_ref().clone(),
},
))),
// Full eval case
PartialValue::Value(v) => {
if v.get_as_bool()? {
Expand All @@ -325,9 +329,13 @@ impl<'e> Evaluator<'e> {
ExprKind::Or { left, right } => {
match self.partial_interpret(left, slots)? {
// PE cases
PartialValue::Residual(r) => {
Ok(PartialValue::Residual(Expr::or(r, right.as_ref().clone())))
}
PartialValue::Residual(r) => Ok(PartialValue::Residual(Expr::or(
r,
match self.partial_interpret(right, slots) {
Ok(right) => Expr::from(right),
Err(_) => right.as_ref().clone(),
},
))),
// Full eval case
PartialValue::Value(lhs) => {
if lhs.get_as_bool()? {
Expand Down Expand Up @@ -5120,7 +5128,7 @@ pub mod test {
assert_eq!(r, PartialValue::Value(Value::from(false)));
}

// res && true -> res && true
// res && (2 == 2) -> res && true
#[test]
fn partial_and_res_true() {
let lhs = Expr::get_attr(Expr::unknown(Unknown::new_untyped("test")), "field".into());
Expand All @@ -5131,7 +5139,7 @@ pub mod test {
let eval = Evaluator::new(empty_request(), &es, &exts);

let r = eval.partial_interpret(&e, &HashMap::new()).unwrap();
let expected = Expr::and(lhs, rhs);
let expected = Expr::and(lhs, Value::new(true, None).into());
assert_eq!(r, PartialValue::Residual(expected));
}

Expand All @@ -5145,7 +5153,7 @@ pub mod test {
let eval = Evaluator::new(empty_request(), &es, &exts);

let r = eval.partial_interpret(&e, &HashMap::new()).unwrap();
let expected = Expr::and(lhs, rhs);
let expected = Expr::and(lhs, Value::new(false, None).into());
assert_eq!(r, PartialValue::Residual(expected));
}

Expand Down Expand Up @@ -5230,7 +5238,7 @@ pub mod test {
let eval = Evaluator::new(empty_request(), &es, &exts);

let r = eval.partial_interpret(&e, &HashMap::new()).unwrap();
let expected = Expr::or(lhs, rhs);
let expected = Expr::or(lhs, Value::new(true, None).into());
assert_eq!(r, PartialValue::Residual(expected));
}

Expand All @@ -5244,7 +5252,7 @@ pub mod test {
let eval = Evaluator::new(empty_request(), &es, &exts);

let r = eval.partial_interpret(&e, &HashMap::new()).unwrap();
let expected = Expr::or(lhs, rhs);
let expected = Expr::or(lhs, Value::new(false, None).into());
assert_eq!(r, PartialValue::Residual(expected));
}

Expand Down