Skip to content

Commit 0274d78

Browse files
committed
restore precise parser error message
1 parent 80b67bb commit 0274d78

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,13 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
14191419
drop(self);
14201420
}
14211421

1422+
/// Cancels this diagnostic and returns its first message, if it exists.
1423+
pub fn cancel_into_message(self) -> Option<String> {
1424+
let s = self.diag.as_ref()?.messages.get(0)?.0.as_str().map(ToString::to_string);
1425+
self.cancel();
1426+
s
1427+
}
1428+
14221429
/// See `DiagCtxt::stash_diagnostic` for details.
14231430
pub fn stash(mut self, span: Span, key: StashKey) -> Option<ErrorGuaranteed> {
14241431
let diag = self.take_diag();

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,22 @@ impl server::TokenStream for Rustc<'_, '_> {
563563
}
564564

565565
fn from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
566+
const ERROR_MSG: &str = "cannot parse string into token stream";
567+
566568
source_str_to_stream(
567569
self.psess(),
568570
FileName::proc_macro_source_code(src),
569571
src.to_string(),
570572
Some(self.call_site),
571573
)
572574
.map_err(|diags| {
573-
diags.into_iter().for_each(Diag::cancel);
574-
"cannot parse string into token stream".to_string()
575+
let mut messages = diags.into_iter().map(Diag::cancel_into_message).flatten();
576+
if let Some(msg) = messages.next() {
577+
messages.for_each(drop);
578+
format!("{ERROR_MSG}: {msg}")
579+
} else {
580+
ERROR_MSG.to_string()
581+
}
575582
})
576583
}
577584

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

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

2121
#[proc_macro]
2222
pub fn lexer_failure(_: TokenStream) -> TokenStream {
23-
assert!("a b ) c".parse::<TokenStream>().is_err());
23+
assert_eq!(
24+
"a b ) c".parse::<TokenStream>().unwrap_err().to_string(),
25+
"cannot parse string into token stream: unexpected closing delimiter: `)`"
26+
);
2427
TokenStream::new()
2528
}

0 commit comments

Comments
 (0)