Skip to content

Commit 2581d80

Browse files
authored
Merge pull request #478 from dtolnay/fromstr
Spell out types in FromStr conversions
2 parents a720d9f + 8a3962e commit 2581d80

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/fallback.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,14 @@ impl Debug for TokenStream {
226226
#[cfg(feature = "proc-macro")]
227227
impl From<proc_macro::TokenStream> for TokenStream {
228228
fn from(inner: proc_macro::TokenStream) -> Self {
229-
inner
230-
.to_string()
231-
.parse()
232-
.expect("compiler token stream parse failed")
229+
TokenStream::from_str(&inner.to_string()).expect("compiler token stream parse failed")
233230
}
234231
}
235232

236233
#[cfg(feature = "proc-macro")]
237234
impl From<TokenStream> for proc_macro::TokenStream {
238235
fn from(inner: TokenStream) -> Self {
239-
inner
240-
.to_string()
241-
.parse()
236+
proc_macro::TokenStream::from_str(&inner.to_string())
242237
.expect("failed to parse to compiler tokens")
243238
}
244239
}

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl FromStr for TokenStream {
245245
type Err = LexError;
246246

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

13091309
fn from_str(repr: &str) -> Result<Self, LexError> {
1310-
repr.parse().map(Literal::_new).map_err(|inner| LexError {
1311-
inner,
1312-
_marker: MARKER,
1313-
})
1310+
imp::Literal::from_str(repr)
1311+
.map(Literal::_new)
1312+
.map_err(|inner| LexError {
1313+
inner,
1314+
_marker: MARKER,
1315+
})
13141316
}
13151317
}
13161318

src/wrapper.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ impl FromStr for TokenStream {
117117
proc_macro_parse(src)?,
118118
)))
119119
} else {
120-
Ok(TokenStream::Fallback(src.parse()?))
120+
Ok(TokenStream::Fallback(fallback::TokenStream::from_str(src)?))
121121
}
122122
}
123123
}
124124

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

@@ -147,7 +148,9 @@ impl From<TokenStream> for proc_macro::TokenStream {
147148
fn from(inner: TokenStream) -> Self {
148149
match inner {
149150
TokenStream::Compiler(inner) => inner.into_token_stream(),
150-
TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
151+
TokenStream::Fallback(inner) => {
152+
proc_macro::TokenStream::from_str(&inner.to_string()).unwrap()
153+
}
151154
}
152155
}
153156
}
@@ -897,7 +900,7 @@ impl Literal {
897900
#[cfg(no_literal_byte_character)]
898901
{
899902
let fallback = fallback::Literal::byte_character(byte);
900-
fallback.repr.parse::<proc_macro::Literal>().unwrap()
903+
proc_macro::Literal::from_str(&fallback.repr).unwrap()
901904
}
902905
})
903906
} else {
@@ -924,7 +927,7 @@ impl Literal {
924927
#[cfg(no_literal_c_string)]
925928
{
926929
let fallback = fallback::Literal::c_string(string);
927-
fallback.repr.parse::<proc_macro::Literal>().unwrap()
930+
proc_macro::Literal::from_str(&fallback.repr).unwrap()
928931
}
929932
})
930933
} else {

0 commit comments

Comments
 (0)