Skip to content

Commit

Permalink
Add context for more "strict undefined behavior" cases
Browse files Browse the repository at this point in the history
...and make it hard to forget to add context again
  • Loading branch information
fasterthanlime committed Oct 31, 2024
1 parent 61fd773 commit a7b0df9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions minijinja/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ impl<'env> Vm<'env> {
};
}

#[allow(unused_macros)]
macro_rules! ok {
($($tt:tt)*) => {
match $($tt)* {
Ok(rv) => rv,
Err(err) => {
compile_error!("Use the `ctx_ok!` macro instead of `ok!`");
},
}
}
}

macro_rules! assert_valid {
($expr:expr) => {{
let val = $expr;
Expand Down Expand Up @@ -306,7 +318,7 @@ impl<'env> Vm<'env> {
Instruction::EmitRaw(val) => {
// this only produces a format error, no need to attach
// location information.
ok!(out.write_str(val).map_err(Error::from));
ctx_ok!(out.write_str(val).map_err(Error::from));
}
Instruction::Emit => {
ctx_ok!(self.env.format(&stack.pop(), state, out));
Expand Down Expand Up @@ -488,21 +500,21 @@ impl<'env> Vm<'env> {
}
Instruction::JumpIfFalse(jump_target) => {
a = stack.pop();
if !ok!(undefined_behavior.is_true(&a)) {
if !ctx_ok!(undefined_behavior.is_true(&a)) {
pc = *jump_target;
continue;
}
}
Instruction::JumpIfFalseOrPop(jump_target) => {
if !ok!(undefined_behavior.is_true(stack.peek())) {
if !ctx_ok!(undefined_behavior.is_true(stack.peek())) {
pc = *jump_target;
continue;
} else {
stack.pop();
}
}
Instruction::JumpIfTrueOrPop(jump_target) => {
if ok!(undefined_behavior.is_true(stack.peek())) {
if ctx_ok!(undefined_behavior.is_true(stack.peek())) {
pc = *jump_target;
continue;
} else {
Expand Down

0 comments on commit a7b0df9

Please sign in to comment.