From a16c91d3c5abdb85d7f5dcca54e8de456a7fdaad Mon Sep 17 00:00:00 2001 From: FractalFir Date: Mon, 2 Dec 2024 14:53:48 +0100 Subject: [PATCH 1/2] Fixes a bug with the `debug_printf` and `debug_printfln` macros not escaping '{' and '}' correctly. The `debug_printf` and `debug_printfln` passes its format string to the `asm!` macro, which uses `{` and `}` to separate its arguments. Passing `{` and `}` to `asm!` directly is incorrect, and leads to compilation issues: ``` error: invalid asm template string: expected `'}'`, found `'"'` --> examples/shaders/sky-shader/src/lib.rs:13016:2 | 13015 | unsafe{debug_printf!("Variant3{")}; | -------------------------- in this macro invocation 13016 | unsafe{debug_printf!("fld0:")}; | ----^ expected `'}'` in asm template string | | | because of this opening brace | = note: if you intended to print `{`, you can escape it using `{{` = note: this error originates in the macro `debug_printf` (in Nightly builds, run with -Z macro-backtrace for more info) ``` This commit escapes those characters using `{{` and `}}`, which removes this issue and produces correct behaviour. --- crates/spirv-std/macros/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/spirv-std/macros/src/lib.rs b/crates/spirv-std/macros/src/lib.rs index 9ecc7aef8b..3889d25ca9 100644 --- a/crates/spirv-std/macros/src/lib.rs +++ b/crates/spirv-std/macros/src/lib.rs @@ -613,7 +613,11 @@ fn debug_printf_inner(input: DebugPrintfInput) -> TokenStream { .into_iter() .collect::(); let op_loads = op_loads.into_iter().collect::(); - + // Escapes the '{' and '}' characters in the format string. + // Since the `asm!` macro expects '{' '}' to surround its arguments, we have to use '{{' and '}}' instead. + // The `asm!` macro will then later turn them back into '{' and '}'. + let format_string = format_string.replace('{',"{{").replace('}',"}}"); + let op_string = format!("%string = OpString {format_string:?}"); let output = quote::quote! { From e4a9c5b680c653fa029c26034609acbc8f83923c Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Mon, 2 Dec 2024 11:16:01 -0400 Subject: [PATCH 2/2] Fix formatting --- crates/spirv-std/macros/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/spirv-std/macros/src/lib.rs b/crates/spirv-std/macros/src/lib.rs index 3889d25ca9..c12da6ee69 100644 --- a/crates/spirv-std/macros/src/lib.rs +++ b/crates/spirv-std/macros/src/lib.rs @@ -613,11 +613,11 @@ fn debug_printf_inner(input: DebugPrintfInput) -> TokenStream { .into_iter() .collect::(); let op_loads = op_loads.into_iter().collect::(); - // Escapes the '{' and '}' characters in the format string. + // Escapes the '{' and '}' characters in the format string. // Since the `asm!` macro expects '{' '}' to surround its arguments, we have to use '{{' and '}}' instead. // The `asm!` macro will then later turn them back into '{' and '}'. - let format_string = format_string.replace('{',"{{").replace('}',"}}"); - + let format_string = format_string.replace('{', "{{").replace('}', "}}"); + let op_string = format!("%string = OpString {format_string:?}"); let output = quote::quote! {