Skip to content

Commit a16c91d

Browse files
authored
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.
1 parent d3f9af7 commit a16c91d

File tree

1 file changed

+5
-1
lines changed
  • crates/spirv-std/macros/src

1 file changed

+5
-1
lines changed

crates/spirv-std/macros/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,11 @@ fn debug_printf_inner(input: DebugPrintfInput) -> TokenStream {
613613
.into_iter()
614614
.collect::<proc_macro2::TokenStream>();
615615
let op_loads = op_loads.into_iter().collect::<proc_macro2::TokenStream>();
616-
616+
// Escapes the '{' and '}' characters in the format string.
617+
// Since the `asm!` macro expects '{' '}' to surround its arguments, we have to use '{{' and '}}' instead.
618+
// The `asm!` macro will then later turn them back into '{' and '}'.
619+
let format_string = format_string.replace('{',"{{").replace('}',"}}");
620+
617621
let op_string = format!("%string = OpString {format_string:?}");
618622

619623
let output = quote::quote! {

0 commit comments

Comments
 (0)