Skip to content

Commit

Permalink
Merge pull request #321 from dtolnay/literal
Browse files Browse the repository at this point in the history
Restore support for versions without FromStr for proc_macro::Literal
  • Loading branch information
dtolnay authored Dec 27, 2021
2 parents 58d5565 + 1a9b24d commit 8efe7d4
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ macro_rules! unsuffixed_integers {
impl Literal {
pub unsafe fn from_str_unchecked(repr: &str) -> Self {
if inside_proc_macro() {
Literal::Compiler(repr.parse().expect("invalid literal"))
Literal::Compiler(compiler_literal_from_str(repr).expect("invalid literal"))
} else {
Literal::Fallback(fallback::Literal::from_str_unchecked(repr))
}
Expand Down Expand Up @@ -929,32 +929,32 @@ impl FromStr for Literal {

fn from_str(repr: &str) -> Result<Self, Self::Err> {
if inside_proc_macro() {
#[cfg(not(no_literal_from_str))]
{
proc_macro::Literal::from_str(repr)
.map(Literal::Compiler)
.map_err(LexError::Compiler)
}
#[cfg(no_literal_from_str)]
{
let tokens = proc_macro_parse(repr)?;
let mut iter = tokens.into_iter();
if let (Some(proc_macro::TokenTree::Literal(literal)), None) =
(iter.next(), iter.next())
{
if literal.to_string().len() == repr.len() {
return Ok(Literal::Compiler(literal));
}
}
Err(LexError::call_site())
}
compiler_literal_from_str(repr).map(Literal::Compiler)
} else {
let literal = fallback::Literal::from_str(repr)?;
Ok(Literal::Fallback(literal))
}
}
}

fn compiler_literal_from_str(repr: &str) -> Result<proc_macro::Literal, LexError> {
#[cfg(not(no_literal_from_str))]
{
proc_macro::Literal::from_str(repr).map_err(LexError::Compiler)
}
#[cfg(no_literal_from_str)]
{
let tokens = proc_macro_parse(repr)?;
let mut iter = tokens.into_iter();
if let (Some(proc_macro::TokenTree::Literal(literal)), None) = (iter.next(), iter.next()) {
if literal.to_string().len() == repr.len() {
return Ok(literal);
}
}
Err(LexError::call_site())
}
}

impl Display for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down

0 comments on commit 8efe7d4

Please sign in to comment.