@@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashMap;
1010use rustc_errors:: { Diag , ErrorGuaranteed , MultiSpan , PResult } ;
1111use rustc_parse:: lexer:: { StripTokens , nfc_normalize} ;
1212use rustc_parse:: parser:: Parser ;
13- use rustc_parse:: { exp, new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal } ;
13+ use rustc_parse:: { exp, new_parser_from_source_str, source_str_to_stream} ;
1414use rustc_proc_macro:: bridge:: {
1515 DelimSpan , Diagnostic , ExpnGlobals , Group , Ident , LitKind , Literal , Punct , TokenTree , server,
1616} ;
@@ -431,6 +431,13 @@ impl ToInternal<rustc_errors::Level> for Level {
431431 }
432432}
433433
434+ fn cancel_diags_into_string ( diags : Vec < Diag < ' _ > > ) -> String {
435+ let mut messages = diags. into_iter ( ) . map ( Diag :: cancel_into_message) . flatten ( ) ;
436+ let msg = messages. next ( ) . expect ( "no diagnostic has a message" ) ;
437+ messages. for_each ( drop) ;
438+ msg
439+ }
440+
434441pub ( crate ) struct FreeFunctions ;
435442
436443pub ( crate ) struct Rustc < ' a , ' b > {
@@ -483,35 +490,32 @@ impl server::FreeFunctions for Rustc<'_, '_> {
483490 self . psess ( ) . file_depinfo . borrow_mut ( ) . insert ( Symbol :: intern ( path) ) ;
484491 }
485492
486- fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , ( ) > {
493+ fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , String > {
487494 let name = FileName :: proc_macro_source_code ( s) ;
488495
489- let mut parser = unwrap_or_emit_fatal ( new_parser_from_source_str (
490- self . psess ( ) ,
491- name,
492- s. to_owned ( ) ,
493- StripTokens :: Nothing ,
494- ) ) ;
496+ let mut parser =
497+ new_parser_from_source_str ( self . psess ( ) , name, s. to_owned ( ) , StripTokens :: Nothing )
498+ . map_err ( cancel_diags_into_string) ?;
495499
496500 let first_span = parser. token . span . data ( ) ;
497501 let minus_present = parser. eat ( exp ! ( Minus ) ) ;
498502
499503 let lit_span = parser. token . span . data ( ) ;
500504 let token:: Literal ( mut lit) = parser. token . kind else {
501- return Err ( ( ) ) ;
505+ return Err ( "not a literal" . to_string ( ) ) ;
502506 } ;
503507
504508 // Check no comment or whitespace surrounding the (possibly negative)
505509 // literal, or more tokens after it.
506510 if ( lit_span. hi . 0 - first_span. lo . 0 ) as usize != s. len ( ) {
507- return Err ( ( ) ) ;
511+ return Err ( "comment or whitespace around literal" . to_string ( ) ) ;
508512 }
509513
510514 if minus_present {
511515 // If minus is present, check no comment or whitespace in between it
512516 // and the literal token.
513517 if first_span. hi . 0 != lit_span. lo . 0 {
514- return Err ( ( ) ) ;
518+ return Err ( "comment or whitespace after minus" . to_string ( ) ) ;
515519 }
516520
517521 // Check literal is a kind we allow to be negated in a proc macro token.
@@ -525,7 +529,9 @@ impl server::FreeFunctions for Rustc<'_, '_> {
525529 | token:: LitKind :: ByteStrRaw ( _)
526530 | token:: LitKind :: CStr
527531 | token:: LitKind :: CStrRaw ( _)
528- | token:: LitKind :: Err ( _) => return Err ( ( ) ) ,
532+ | token:: LitKind :: Err ( _) => {
533+ return Err ( "non-numeric literal may not be negated" . to_string ( ) ) ;
534+ }
529535 token:: LitKind :: Integer | token:: LitKind :: Float => { }
530536 }
531537
@@ -562,13 +568,14 @@ impl server::TokenStream for Rustc<'_, '_> {
562568 stream. is_empty ( )
563569 }
564570
565- fn from_str ( & mut self , src : & str ) -> Self :: TokenStream {
566- unwrap_or_emit_fatal ( source_str_to_stream (
571+ fn from_str ( & mut self , src : & str ) -> Result < Self :: TokenStream , String > {
572+ source_str_to_stream (
567573 self . psess ( ) ,
568574 FileName :: proc_macro_source_code ( src) ,
569575 src. to_string ( ) ,
570576 Some ( self . call_site ) ,
571- ) )
577+ )
578+ . map_err ( cancel_diags_into_string)
572579 }
573580
574581 fn to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
0 commit comments