From 6467edf9bc19dd5d1e96dea9c1745f7cdc6a790a Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 24 Nov 2020 20:52:54 +0100 Subject: [PATCH] Escape braces in assert expressions. --- firmware/qemu/src/bin/assert-eq.rs | 2 ++ firmware/qemu/src/bin/assert-ne.rs | 2 ++ firmware/qemu/src/bin/assert.rs | 2 ++ macros/src/lib.rs | 7 ++++++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/firmware/qemu/src/bin/assert-eq.rs b/firmware/qemu/src/bin/assert-eq.rs index 0aa88bae..9159a42e 100644 --- a/firmware/qemu/src/bin/assert-eq.rs +++ b/firmware/qemu/src/bin/assert-eq.rs @@ -7,6 +7,8 @@ use defmt_semihosting as _; // global logger #[entry] fn main() -> ! { + defmt::assert_eq!({1 + 1}, { 2 }); + let x = 42; defmt::debug_assert_eq!(x - 1, x + 1, "dev"); defmt::assert_eq!(x - 1, x + 1, "release"); diff --git a/firmware/qemu/src/bin/assert-ne.rs b/firmware/qemu/src/bin/assert-ne.rs index 82cb4423..0b0bc99d 100644 --- a/firmware/qemu/src/bin/assert-ne.rs +++ b/firmware/qemu/src/bin/assert-ne.rs @@ -7,6 +7,8 @@ use defmt_semihosting as _; // global logger #[entry] fn main() -> ! { + defmt::assert_ne!({1 + 1}, { 3 }); + let x = 42; defmt::debug_assert_ne!(x, x, "dev"); defmt::assert_ne!(x, x, "release"); diff --git a/firmware/qemu/src/bin/assert.rs b/firmware/qemu/src/bin/assert.rs index 8664f1ee..9a8bba89 100644 --- a/firmware/qemu/src/bin/assert.rs +++ b/firmware/qemu/src/bin/assert.rs @@ -7,6 +7,8 @@ use defmt_semihosting as _; // global logger #[entry] fn main() -> ! { + defmt::assert!({1 + 1} == { 2 }); + let dev = false; let release = false; defmt::debug_assert!(dev); diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 9a6bf6c2..18a43d7c 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -594,7 +594,7 @@ pub fn assert_(ts: TokenStream) -> TokenStream { Level::Error, FormatArgs { litstr: LitStr::new( - &format!("panicked at 'assertion failed: {}'", quote!(#condition)), + &format!("panicked at 'assertion failed: {}'", escape_expr(&condition)), Span2::call_site(), ), rest: None, @@ -752,6 +752,11 @@ pub fn debug_assert_ne_(ts: TokenStream) -> TokenStream { .into() } +fn escape_expr(expr: &Expr) -> String { + let q = quote!(#expr); + q.to_string().replace("{", "{{").replace("}", "}}") +} + struct Assert { condition: Expr, args: Option,