Skip to content

Commit 9a39fca

Browse files
authored
Merge pull request #296 from dtolnay/literalfromstr
Use real Literal::from_str from libproc_macro
2 parents 81dbf4c + fb3504c commit 9a39fca

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ fn main() {
7878
println!("cargo:rustc-cfg=hygiene");
7979
}
8080

81+
if version.minor >= 54 {
82+
println!("cargo:rustc-cfg=literal_from_str");
83+
}
84+
8185
let target = env::var("TARGET").unwrap();
8286
if !enable_use_proc_macro(&target) {
8387
return;

src/wrapper.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -921,18 +921,25 @@ impl FromStr for Literal {
921921

922922
fn from_str(repr: &str) -> Result<Self, Self::Err> {
923923
if inside_proc_macro() {
924-
// TODO: use libproc_macro's FromStr impl once it is available in
925-
// rustc. https://github.com/rust-lang/rust/pull/84717
926-
let tokens = proc_macro_parse(repr)?;
927-
let mut iter = tokens.into_iter();
928-
if let (Some(proc_macro::TokenTree::Literal(literal)), None) =
929-
(iter.next(), iter.next())
924+
#[cfg(literal_from_str)]
930925
{
931-
if literal.to_string().len() == repr.len() {
932-
return Ok(Literal::Compiler(literal));
926+
proc_macro::Literal::from_str(repr)
927+
.map(Literal::Compiler)
928+
.map_err(LexError::Compiler)
929+
}
930+
#[cfg(not(literal_from_str))]
931+
{
932+
let tokens = proc_macro_parse(repr)?;
933+
let mut iter = tokens.into_iter();
934+
if let (Some(proc_macro::TokenTree::Literal(literal)), None) =
935+
(iter.next(), iter.next())
936+
{
937+
if literal.to_string().len() == repr.len() {
938+
return Ok(Literal::Compiler(literal));
939+
}
933940
}
941+
Err(LexError::call_site())
934942
}
935-
Err(LexError::call_site())
936943
} else {
937944
let literal = fallback::Literal::from_str(repr)?;
938945
Ok(Literal::Fallback(literal))

0 commit comments

Comments
 (0)