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

Escape braces in assert expressions. #272

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions firmware/qemu/src/bin/assert-eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions firmware/qemu/src/bin/assert-ne.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions firmware/qemu/src/bin/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<FormatArgs>,
Expand Down