From 7ca25813c3c267148b4b0194332d4e0410a2213c Mon Sep 17 00:00:00 2001 From: Arvid Norlander Date: Wed, 17 Jul 2024 14:25:43 +0200 Subject: [PATCH] rune: Fix formatting panic for 0.13.x (#749) This ensures that we don't get an error on formatting a value without a debug protocol. Instead attempt to print *something* (fixes #747) --- crates/rune/src/runtime/value.rs | 21 +++++++++++++++++---- crates/rune/src/runtime/vm.rs | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/rune/src/runtime/value.rs b/crates/rune/src/runtime/value.rs index 461369c19..558bff3a1 100644 --- a/crates/rune/src/runtime/value.rs +++ b/crates/rune/src/runtime/value.rs @@ -495,11 +495,16 @@ impl Value { vm_write!(f, "{:?}", value); } value => { + // reborrow f to avoid moving it let result = - vm_try!(caller.call_protocol_fn(Protocol::STRING_DEBUG, value.clone(), (f,),)); + caller.call_protocol_fn(Protocol::STRING_DEBUG, self.clone(), (&mut *f,)); - vm_try!(<()>::from_value(result)); - return VmResult::Ok(()); + if let VmResult::Ok(result) = result { + vm_try!(<()>::from_value(result)); + } else { + let type_info = vm_try!(value.type_info()); + vm_write!(f, "<{} object at {:p}>", type_info, value); + } } }; @@ -2030,7 +2035,15 @@ impl fmt::Debug for Value { let mut o = Formatter::new(); if value.string_debug(&mut o).is_err() { - return Err(fmt::Error); + match value.type_info() { + VmResult::Ok(type_info) => { + write!(f, "<{} object at {:p}>", type_info, value)?; + } + VmResult::Err(e) => { + write!(f, "", value, e)?; + } + } + return Ok(()); } f.write_str(o.as_str())?; diff --git a/crates/rune/src/runtime/vm.rs b/crates/rune/src/runtime/vm.rs index a11fdebf5..ef3bc1e1e 100644 --- a/crates/rune/src/runtime/vm.rs +++ b/crates/rune/src/runtime/vm.rs @@ -2058,7 +2058,7 @@ impl Vm { } #[inline] - #[tracing::instrument(skip(self))] + #[tracing::instrument(skip(self, return_value))] fn op_return_internal( &mut self, return_value: Value,