Skip to content

Commit 8efe7d4

Browse files
authored
Merge pull request #321 from dtolnay/literal
Restore support for versions without FromStr for proc_macro::Literal
2 parents 58d5565 + 1a9b24d commit 8efe7d4

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/wrapper.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ macro_rules! unsuffixed_integers {
806806
impl Literal {
807807
pub unsafe fn from_str_unchecked(repr: &str) -> Self {
808808
if inside_proc_macro() {
809-
Literal::Compiler(repr.parse().expect("invalid literal"))
809+
Literal::Compiler(compiler_literal_from_str(repr).expect("invalid literal"))
810810
} else {
811811
Literal::Fallback(fallback::Literal::from_str_unchecked(repr))
812812
}
@@ -929,32 +929,32 @@ impl FromStr for Literal {
929929

930930
fn from_str(repr: &str) -> Result<Self, Self::Err> {
931931
if inside_proc_macro() {
932-
#[cfg(not(no_literal_from_str))]
933-
{
934-
proc_macro::Literal::from_str(repr)
935-
.map(Literal::Compiler)
936-
.map_err(LexError::Compiler)
937-
}
938-
#[cfg(no_literal_from_str)]
939-
{
940-
let tokens = proc_macro_parse(repr)?;
941-
let mut iter = tokens.into_iter();
942-
if let (Some(proc_macro::TokenTree::Literal(literal)), None) =
943-
(iter.next(), iter.next())
944-
{
945-
if literal.to_string().len() == repr.len() {
946-
return Ok(Literal::Compiler(literal));
947-
}
948-
}
949-
Err(LexError::call_site())
950-
}
932+
compiler_literal_from_str(repr).map(Literal::Compiler)
951933
} else {
952934
let literal = fallback::Literal::from_str(repr)?;
953935
Ok(Literal::Fallback(literal))
954936
}
955937
}
956938
}
957939

940+
fn compiler_literal_from_str(repr: &str) -> Result<proc_macro::Literal, LexError> {
941+
#[cfg(not(no_literal_from_str))]
942+
{
943+
proc_macro::Literal::from_str(repr).map_err(LexError::Compiler)
944+
}
945+
#[cfg(no_literal_from_str)]
946+
{
947+
let tokens = proc_macro_parse(repr)?;
948+
let mut iter = tokens.into_iter();
949+
if let (Some(proc_macro::TokenTree::Literal(literal)), None) = (iter.next(), iter.next()) {
950+
if literal.to_string().len() == repr.len() {
951+
return Ok(literal);
952+
}
953+
}
954+
Err(LexError::call_site())
955+
}
956+
}
957+
958958
impl Display for Literal {
959959
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
960960
match self {

0 commit comments

Comments
 (0)