diff --git a/codegen/src/debug.rs b/codegen/src/debug.rs index 91938812..25092fb5 100644 --- a/codegen/src/debug.rs +++ b/codegen/src/debug.rs @@ -157,15 +157,23 @@ fn expand_impl_body(defs: &Definitions, node: &Node, name: &str) -> TokenStream } } } else if fields.len() == 1 { - let ty = &fields[0]; let val = quote!(_val); - let format = format_field(&val, ty).map(|format| { - quote! { - formatter.write_str("(")?; - Debug::fmt(#format, formatter)?; - formatter.write_str(")")?; - } - }); + let format = if variant == "Verbatim" { + Some(quote! { + formatter.write_str("(`")?; + Display::fmt(#val, formatter)?; + formatter.write_str("`)")?; + }) + } else { + let ty = &fields[0]; + format_field(&val, ty).map(|format| { + quote! { + formatter.write_str("(")?; + Debug::fmt(#format, formatter)?; + formatter.write_str(")")?; + } + }) + }; quote! { syn::#ident::#variant(_val) => { formatter.write_str(#v)?; @@ -298,7 +306,7 @@ pub fn generate(defs: &Definitions) -> Result<()> { DEBUG_SRC, quote! { use super::{Lite, RefCast}; - use std::fmt::{self, Debug}; + use std::fmt::{self, Debug, Display}; #impls }, diff --git a/tests/debug/gen.rs b/tests/debug/gen.rs index 8450c09e..85a1a390 100644 --- a/tests/debug/gen.rs +++ b/tests/debug/gen.rs @@ -2,7 +2,7 @@ // It is not intended for manual editing. use super::{Lite, RefCast}; -use std::fmt::{self, Debug}; +use std::fmt::{self, Debug, Display}; impl Debug for Lite { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { let _val = &self.value; @@ -1039,9 +1039,9 @@ impl Debug for Lite { } syn::Expr::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } syn::Expr::While(_val) => { @@ -2116,9 +2116,9 @@ impl Debug for Lite { } syn::ForeignItem::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } _ => unreachable!(), @@ -2432,9 +2432,9 @@ impl Debug for Lite { } syn::ImplItem::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } _ => unreachable!(), @@ -2940,9 +2940,9 @@ impl Debug for Lite { } syn::Item::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } _ => unreachable!(), @@ -3437,9 +3437,9 @@ impl Debug for Lite { } syn::Lit::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } } @@ -3878,9 +3878,9 @@ impl Debug for Lite { } syn::Pat::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } syn::Pat::Wild(_val) => { @@ -4674,9 +4674,9 @@ impl Debug for Lite { } syn::TraitItem::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } _ => unreachable!(), @@ -5040,9 +5040,9 @@ impl Debug for Lite { } syn::Type::Verbatim(_val) => { formatter.write_str("Verbatim")?; - formatter.write_str("(")?; - Debug::fmt(Lite(_val), formatter)?; - formatter.write_str(")")?; + formatter.write_str("(`")?; + Display::fmt(_val, formatter)?; + formatter.write_str("`)")?; Ok(()) } _ => unreachable!(), diff --git a/tests/test_stmt.rs b/tests/test_stmt.rs index 1e6f0099..d68b47fd 100644 --- a/tests/test_stmt.rs +++ b/tests/test_stmt.rs @@ -10,7 +10,7 @@ fn test_raw_operator() { snapshot!(stmt, @r###" Local(Local { pat: Pat::Wild, - init: Some(Verbatim(TokenStream(`& raw const x`))), + init: Some(Verbatim(`& raw const x`)), }) "###); }