Repository: mitsuhiko/minijinja
Pull request #78 (2022-08-28 15:09:35)

Change the debug format for instructions to actual rust format
Manually maintaining this list is not a lot of fun.


Patch:
diff --git a/minijinja/src/instructions.rs b/minijinja/src/instructions.rs
index f6fca20..cd69e6a 100644
--- a/minijinja/src/instructions.rs
+++ b/minijinja/src/instructions.rs
@@ -14,6 +14,7 @@ pub const LOOP_FLAG_RECURSIVE: u8 = 2;
 
 /// Represents an instruction for the VM.
 #[derive(Clone, PartialEq, Eq)]
+#[cfg_attr(feature = "internal_debug", derive(Debug))]
 pub enum Instruction<'source> {
     /// Emits raw source
     EmitRaw(&'source str),
@@ -185,79 +186,6 @@ pub enum Instruction<'source> {
     Nop,
 }
 
-#[cfg(feature = "internal_debug")]
-impl<'source> fmt::Debug for Instruction<'source> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            Instruction::EmitRaw(s) => write!(f, "EMIT_RAW (string {:?})", s),
-            Instruction::StoreLocal(n) => write!(f, "STORE_LOCAL (var {:?})", n),
-            Instruction::Lookup(n) => write!(f, "LOOKUP (var {:?})", n),
-            Instruction::GetAttr(n) => write!(f, "GETATTR (key {:?})", n),
-            Instruction::GetItem => write!(f, "GETITEM"),
-            Instruction::LoadConst(ref v) => write!(f, "LOAD_CONST (value {:?})", v),
-            Instruction::BuildMap(n) => write!(f, "BUILD_MAP ({:?} pairs)", n),
-            Instruction::BuildList(n) => write!(f, "BUILD_LIST ({:?} items)", n),
-            Instruction::UnpackList(n) => write!(f, "UNPACK_LIST ({:?} items)", n),
-            Instruction::ListAppend => write!(f, "LIST_APPEND"),
-            Instruction::Add => write!(f, "ADD"),
-            Instruction::Sub => write!(f, "SUB"),
-            Instruction::Mul => write!(f, "MUL"),
-            Instruction::Div => write!(f, "DIV"),
-            Instruction::IntDiv => write!(f, "INT_DIV"),
-            Instruction::Rem => write!(f, "REM"),
-            Instruction::Pow => write!(f, "Pow"),
-            Instruction::Neg => write!(f, "NEG"),
-            Instruction::Eq => write!(f, "EQ"),
-            Instruction::Ne => write!(f, "NE"),
-            Instruction::Gt => write!(f, "GT"),
-            Instruction::Gte => write!(f, "GTE"),
-            Instruction::Lt => write!(f, "LT"),
-            Instruction::Lte => write!(f, "LTE"),
-            Instruction::Not => write!(f, "NOT"),
-            Instruction::StringConcat => write!(f, "STRING_CONCAT"),
-            Instruction::In => write!(f, "IN"),
-            Instruction::ApplyFilter(n) => {
-                write!(f, "APPLY_FILTER (name {:?})", n)
-            }
-            Instruction::PerformTest(n) => {
-                write!(f, "PERFORM_TEST (name {:?})", n)
-            }
-            Instruction::Emit => write!(f, "EMIT"),
-            Instruction::PushLoop(flags) => {
-                let recursive = flags & LOOP_FLAG_RECURSIVE != 0;
-                let loop_var = flags & LOOP_FLAG_WITH_LOOP_VAR != 0;
-                write!(
-                    f,
-                    "PUSH_LOOP (loop var: {:?}, recursive: {:?})",
-                    loop_var, recursive
-                )
-            }
-            Instruction::PushWith => write!(f, "PUSH_WITH"),
-            Instruction::Iterate(t) => write!(f, "ITERATE (exit to {:>05x})", t),
-            Instruction::PopFrame => write!(f, "POP_FRAME"),
-            Instruction::Jump(t) => write!(f, "JUMP (to {:>05x})", t),
-            Instruction::JumpIfFalse(t) => write!(f, "JUMP_IF_FALSE (to {:>05x})", t),
-            Instruction::JumpIfFalseOrPop(t) => write!(f, "JUMP_IF_FALSE_OR_POP (to {:>05x})", t),
-            Instruction::JumpIfTrueOrPop(t) => write!(f, "JUMP_IF_TRUE_OR_POP (to {:>05x})", t),
-            Instruction::CallBlock(n) => write!(f, "CALL_BLOCK (name {:?})", n),
-            Instruction::LoadBlocks => write!(f, "LOAD_BLOCKS"),
-            Instruction::Include(b) => write!(f, "INCLUDE (ignore missing {:?})", b),
-            Instruction::PushAutoEscape => write!(f, "PUSH_AUTO_ESCAPE"),
-            Instruction::PopAutoEscape => write!(f, "POP_AUTO_ESCAPE"),
-            Instruction::BeginCapture => write!(f, "BEGIN_CAPTURE"),
-            Instruction::EndCapture => write!(f, "END_CAPTURE"),
-            Instruction::CallFunction(n) => write!(f, "CALL_FUNCTION (name {:?})", n),
-            Instruction::CallMethod(n) => write!(f, "CALL_METHOD (name {:?})", n),
-            Instruction::CallObject => write!(f, "CALL_OBJECT"),
-            Instruction::DupTop => write!(f, "DUP_TOP"),
-            Instruction::DiscardTop => write!(f, "DISCARD_TOP"),
-            Instruction::FastSuper => write!(f, "FAST_SUPER"),
-            Instruction::FastRecurse => write!(f, "FAST_RECURSE"),
-            Instruction::Nop => write!(f, "NOP"),
-        }
-    }
-}
-
 #[derive(Copy, Clone)]
 struct Loc {
     first_instruction: u32,


Test patch:
diff --git a/minijinja/tests/snapshots/test_compiler__bool_ops.snap b/minijinja/tests/snapshots/test_compiler__bool_ops.snap
index 84e55ab..ee9857f 100644
--- a/minijinja/tests/snapshots/test_compiler__bool_ops.snap
+++ b/minijinja/tests/snapshots/test_compiler__bool_ops.snap
@@ -4,11 +4,11 @@ expression: "&c"
 ---
 Compiler {
     instructions: [
-        00000 | LOOKUP (var "first")  [line 0],
-        00001 | JUMP_IF_FALSE_OR_POP (to 00005),
-        00002 | LOOKUP (var "second"),
-        00003 | JUMP_IF_TRUE_OR_POP (to 00005),
-        00004 | LOOKUP (var "third"),
+        00000 | Lookup("first")  [line 0],
+        00001 | JumpIfFalseOrPop(5),
+        00002 | Lookup("second"),
+        00003 | JumpIfTrueOrPop(5),
+        00004 | Lookup("third"),
     ],
     blocks: {},
     pending_block: [],
diff --git a/minijinja/tests/snapshots/test_compiler__const.snap b/minijinja/tests/snapshots/test_compiler__const.snap
index a6ac40d..d2f3e75 100644
--- a/minijinja/tests/snapshots/test_compiler__const.snap
+++ b/minijinja/tests/snapshots/test_compiler__const.snap
@@ -4,9 +4,9 @@ expression: "&c"
 ---
 Compiler {
     instructions: [
-        00000 | LOAD_CONST (value "a")  [line 0],
-        00001 | LOAD_CONST (value 42),
-        00002 | STRING_CONCAT,
+        00000 | LoadConst("a")  [line 0],
+        00001 | LoadConst(42),
+        00002 | StringConcat,
     ],
     blocks: {},
     pending_block: [],
diff --git a/minijinja/tests/snapshots/test_compiler__for_loop.snap b/minijinja/tests/snapshots/test_compiler__for_loop.snap
index 62e3c36..05109af 100644
--- a/minijinja/tests/snapshots/test_compiler__for_loop.snap
+++ b/minijinja/tests/snapshots/test_compiler__for_loop.snap
@@ -4,13 +4,13 @@ expression: "&c"
 ---
 Compiler {
     instructions: [
-        00000 | LOOKUP (var "items")  [line 0],
-        00001 | PUSH_LOOP (loop var: true, recursive: false),
-        00002 | ITERATE (exit to 00005),
-        00003 | EMIT,
-        00004 | JUMP (to 00002),
-        00005 | POP_FRAME,
-        00006 | EMIT_RAW (string "!"),
+        00000 | Lookup("items")  [line 0],
+        00001 | PushLoop(1),
+        00002 | Iterate(5),
+        00003 | Emit,
+        00004 | Jump(2),
+        00005 | PopFrame,
+        00006 | EmitRaw("!"),
     ],
     blocks: {},
     pending_block: [],
diff --git a/minijinja/tests/snapshots/test_compiler__if_branches.snap b/minijinja/tests/snapshots/test_compiler__if_branches.snap
index b8b93a6..fece9ad 100644
--- a/minijinja/tests/snapshots/test_compiler__if_branches.snap
+++ b/minijinja/tests/snapshots/test_compiler__if_branches.snap
@@ -4,15 +4,15 @@ expression: "&c"
 ---
 Compiler {
     instructions: [
-        00000 | LOOKUP (var "false")  [line 0],
-        00001 | JUMP_IF_FALSE (to 00004),
-        00002 | EMIT_RAW (string "nope1"),
-        00003 | JUMP (to 00009),
-        00004 | LOOKUP (var "nil"),
-        00005 | JUMP_IF_FALSE (to 00008),
-        00006 | EMIT_RAW (string "nope1"),
-        00007 | JUMP (to 00009),
-        00008 | EMIT_RAW (string "yes"),
+        00000 | Lookup("false")  [line 0],
+        00001 | JumpIfFalse(4),
+        00002 | EmitRaw("nope1"),
+        00003 | Jump(9),
+        00004 | Lookup("nil"),
+        00005 | JumpIfFalse(8),
+        00006 | EmitRaw("nope1"),
+        00007 | Jump(9),
+        00008 | EmitRaw("yes"),
     ],
     blocks: {},
     pending_block: [],