Skip to content

Commit 80b67bb

Browse files
committed
replace TokenStream::from_str panics with LexErrors
1 parent e100792 commit 80b67bb

File tree

8 files changed

+28
-40
lines changed

8 files changed

+28
-40
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,17 @@ impl server::TokenStream for Rustc<'_, '_> {
562562
stream.is_empty()
563563
}
564564

565-
fn from_str(&mut self, src: &str) -> Self::TokenStream {
566-
unwrap_or_emit_fatal(source_str_to_stream(
565+
fn from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
566+
source_str_to_stream(
567567
self.psess(),
568568
FileName::proc_macro_source_code(src),
569569
src.to_string(),
570570
Some(self.call_site),
571-
))
571+
)
572+
.map_err(|diags| {
573+
diags.into_iter().for_each(Diag::cancel);
574+
"cannot parse string into token stream".to_string()
575+
})
572576
}
573577

574578
fn to_string(&mut self, stream: &Self::TokenStream) -> String {

library/proc_macro/src/bridge/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ macro_rules! with_api {
6161
fn clone($self: &$S::TokenStream) -> $S::TokenStream;
6262
fn is_empty($self: &$S::TokenStream) -> bool;
6363
fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
64-
fn from_str(src: &str) -> $S::TokenStream;
64+
fn from_str(src: &str) -> Result<$S::TokenStream, String>;
6565
fn to_string($self: &$S::TokenStream) -> String;
6666
fn from_token_tree(
6767
tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>,

library/proc_macro/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ impl !Sync for TokenStream {}
110110
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
111111
#[non_exhaustive]
112112
#[derive(Debug)]
113-
pub struct LexError;
113+
pub struct LexError(String);
114114

115115
#[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
116116
impl fmt::Display for LexError {
117117
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118-
f.write_str("cannot parse string into token stream")
118+
f.write_str(&self.0)
119119
}
120120
}
121121

@@ -194,7 +194,7 @@ impl FromStr for TokenStream {
194194
type Err = LexError;
195195

196196
fn from_str(src: &str) -> Result<TokenStream, LexError> {
197-
Ok(TokenStream(Some(bridge::client::TokenStream::from_str(src))))
197+
Ok(TokenStream(Some(bridge::client::TokenStream::from_str(src).map_err(LexError)?)))
198198
}
199199
}
200200

@@ -1561,7 +1561,7 @@ impl FromStr for Literal {
15611561
fn from_str(src: &str) -> Result<Self, LexError> {
15621562
match bridge::client::FreeFunctions::literal_from_str(src) {
15631563
Ok(literal) => Ok(Literal(literal)),
1564-
Err(()) => Err(LexError),
1564+
Err(()) => Err(LexError("cannot parse string into token stream".to_string())),
15651565
}
15661566
}
15671567
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ impl server::TokenStream for RaSpanServer {
6565
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
6666
stream.is_empty()
6767
}
68+
#[cfg(not(bootstrap))]
69+
fn from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
70+
Self::TokenStream::from_str(src, self.call_site)
71+
.map_err(|e| format!("failed to parse str to token stream: {e}"))
72+
}
73+
#[cfg(bootstrap)]
6874
fn from_str(&mut self, src: &str) -> Self::TokenStream {
6975
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
7076
Self::TokenStream::from_str(

src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ impl server::TokenStream for SpanIdServer {
5454
fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
5555
stream.is_empty()
5656
}
57+
#[cfg(not(bootstrap))]
58+
fn from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
59+
Self::TokenStream::from_str(src, self.call_site)
60+
.map_err(|e| format!("failed to parse str to token stream: {e}"))
61+
}
62+
#[cfg(bootstrap)]
5763
fn from_str(&mut self, src: &str) -> Self::TokenStream {
5864
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
5965
Self::TokenStream::from_str(

tests/ui/proc-macro/auxiliary/invalid-punct-ident.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ pub fn invalid_raw_ident(_: TokenStream) -> TokenStream {
2020

2121
#[proc_macro]
2222
pub fn lexer_failure(_: TokenStream) -> TokenStream {
23-
"a b ) c".parse().expect("parsing failed without panic")
23+
assert!("a b ) c".parse::<TokenStream>().is_err());
24+
TokenStream::new()
2425
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
//@ proc-macro: invalid-punct-ident.rs
2-
//@ needs-unwind proc macro panics to report errors
2+
//@ check-pass
33

44
#[macro_use]
55
extern crate invalid_punct_ident;
66

77
lexer_failure!();
8-
//~^ ERROR proc macro panicked
9-
//~| ERROR unexpected closing delimiter: `)`
108

11-
fn main() {
12-
let _recovery_witness: () = 0; //~ ERROR mismatched types
13-
}
9+
fn main() {}

tests/ui/proc-macro/invalid-punct-ident-4.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)