Skip to content

Commit

Permalink
Merge pull request #478 from dtolnay/fromstr
Browse files Browse the repository at this point in the history
Spell out types in FromStr conversions
  • Loading branch information
dtolnay authored Nov 21, 2024
2 parents a720d9f + 8a3962e commit 2581d80
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,14 @@ impl Debug for TokenStream {
#[cfg(feature = "proc-macro")]
impl From<proc_macro::TokenStream> for TokenStream {
fn from(inner: proc_macro::TokenStream) -> Self {
inner
.to_string()
.parse()
.expect("compiler token stream parse failed")
TokenStream::from_str(&inner.to_string()).expect("compiler token stream parse failed")
}
}

#[cfg(feature = "proc-macro")]
impl From<TokenStream> for proc_macro::TokenStream {
fn from(inner: TokenStream) -> Self {
inner
.to_string()
.parse()
proc_macro::TokenStream::from_str(&inner.to_string())
.expect("failed to parse to compiler tokens")
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl FromStr for TokenStream {
type Err = LexError;

fn from_str(src: &str) -> Result<TokenStream, LexError> {
let e = src.parse().map_err(|e| LexError {
let e = imp::TokenStream::from_str(src).map_err(|e| LexError {
inner: e,
_marker: MARKER,
})?;
Expand Down Expand Up @@ -1307,10 +1307,12 @@ impl FromStr for Literal {
type Err = LexError;

fn from_str(repr: &str) -> Result<Self, LexError> {
repr.parse().map(Literal::_new).map_err(|inner| LexError {
inner,
_marker: MARKER,
})
imp::Literal::from_str(repr)
.map(Literal::_new)
.map_err(|inner| LexError {
inner,
_marker: MARKER,
})
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ impl FromStr for TokenStream {
proc_macro_parse(src)?,
)))
} else {
Ok(TokenStream::Fallback(src.parse()?))
Ok(TokenStream::Fallback(fallback::TokenStream::from_str(src)?))
}
}
}

// Work around https://github.com/rust-lang/rust/issues/58736.
fn proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError> {
let result = panic::catch_unwind(|| src.parse().map_err(LexError::Compiler));
let result =
panic::catch_unwind(|| proc_macro::TokenStream::from_str(src).map_err(LexError::Compiler));
result.unwrap_or_else(|_| Err(LexError::CompilerPanic))
}

Expand All @@ -147,7 +148,9 @@ impl From<TokenStream> for proc_macro::TokenStream {
fn from(inner: TokenStream) -> Self {
match inner {
TokenStream::Compiler(inner) => inner.into_token_stream(),
TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
TokenStream::Fallback(inner) => {
proc_macro::TokenStream::from_str(&inner.to_string()).unwrap()
}
}
}
}
Expand Down Expand Up @@ -897,7 +900,7 @@ impl Literal {
#[cfg(no_literal_byte_character)]
{
let fallback = fallback::Literal::byte_character(byte);
fallback.repr.parse::<proc_macro::Literal>().unwrap()
proc_macro::Literal::from_str(&fallback.repr).unwrap()
}
})
} else {
Expand All @@ -924,7 +927,7 @@ impl Literal {
#[cfg(no_literal_c_string)]
{
let fallback = fallback::Literal::c_string(string);
fallback.repr.parse::<proc_macro::Literal>().unwrap()
proc_macro::Literal::from_str(&fallback.repr).unwrap()
}
})
} else {
Expand Down

0 comments on commit 2581d80

Please sign in to comment.