From 36c0fb5a2dbb8b33407d814585e610d05d555030 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 23 Jan 2026 15:57:18 +0100 Subject: [PATCH 1/2] Add new `byte_value` and `char_value` methods to `proc_macro::Literal` --- library/proc_macro/src/lib.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index a005f743ddfa..2a75c4489095 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -54,7 +54,9 @@ use std::{error, fmt}; pub use diagnostic::{Diagnostic, Level, MultiSpan}; #[unstable(feature = "proc_macro_value", issue = "136652")] pub use rustc_literal_escaper::EscapeError; -use rustc_literal_escaper::{MixedUnit, unescape_byte_str, unescape_c_str, unescape_str}; +use rustc_literal_escaper::{ + MixedUnit, unescape_byte, unescape_byte_str, unescape_c_str, unescape_char, unescape_str, +}; #[unstable(feature = "proc_macro_totokens", issue = "130977")] pub use to_tokens::ToTokens; @@ -1443,6 +1445,28 @@ impl Literal { }) } + /// Returns the unescaped char value if the current literal is a char. + #[unstable(feature = "proc_macro_value", issue = "136652")] + pub fn byte_value(&self) -> Result { + self.0.symbol.with(|symbol| match self.0.kind { + bridge::LitKind::Char => { + unescape_byte(symbol).map_err(ConversionErrorKind::FailedToUnescape) + } + _ => Err(ConversionErrorKind::InvalidLiteralKind), + }) + } + + /// Returns the unescaped char value if the current literal is a char. + #[unstable(feature = "proc_macro_value", issue = "136652")] + pub fn char_value(&self) -> Result { + self.0.symbol.with(|symbol| match self.0.kind { + bridge::LitKind::Char => { + unescape_char(symbol).map_err(ConversionErrorKind::FailedToUnescape) + } + _ => Err(ConversionErrorKind::InvalidLiteralKind), + }) + } + /// Returns the unescaped string value if the current literal is a string or a string literal. #[unstable(feature = "proc_macro_value", issue = "136652")] pub fn str_value(&self) -> Result { From 55e3d2206a21aeb291cbaf6535579e2994d98d4a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 1 Feb 2026 15:05:57 +0100 Subject: [PATCH 2/2] Improve new proc-macro methods name --- library/proc_macro/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 2a75c4489095..18ab470f89d6 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -1445,9 +1445,9 @@ impl Literal { }) } - /// Returns the unescaped char value if the current literal is a char. + /// Returns the unescaped character value if the current literal is a byte character literal. #[unstable(feature = "proc_macro_value", issue = "136652")] - pub fn byte_value(&self) -> Result { + pub fn byte_character_value(&self) -> Result { self.0.symbol.with(|symbol| match self.0.kind { bridge::LitKind::Char => { unescape_byte(symbol).map_err(ConversionErrorKind::FailedToUnescape) @@ -1456,9 +1456,9 @@ impl Literal { }) } - /// Returns the unescaped char value if the current literal is a char. + /// Returns the unescaped character value if the current literal is a character literal. #[unstable(feature = "proc_macro_value", issue = "136652")] - pub fn char_value(&self) -> Result { + pub fn character_value(&self) -> Result { self.0.symbol.with(|symbol| match self.0.kind { bridge::LitKind::Char => { unescape_char(symbol).map_err(ConversionErrorKind::FailedToUnescape)