@@ -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} ;
@@ -483,35 +483,42 @@ impl server::FreeFunctions for Rustc<'_, '_> {
483483 self . psess ( ) . file_depinfo . borrow_mut ( ) . insert ( Symbol :: intern ( path) ) ;
484484 }
485485
486- fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , ( ) > {
486+ fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , String > {
487+ const ERROR_MSG : & str = "cannot parse string into literal" ;
488+
487489 let name = FileName :: proc_macro_source_code ( s) ;
488490
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- ) ) ;
491+ let mut parser =
492+ new_parser_from_source_str ( self . psess ( ) , name, s. to_owned ( ) , StripTokens :: Nothing )
493+ . map_err ( |diags| {
494+ let mut messages = diags. into_iter ( ) . map ( Diag :: cancel_into_message) . flatten ( ) ;
495+ if let Some ( msg) = messages. next ( ) {
496+ messages. for_each ( drop) ;
497+ format ! ( "{ERROR_MSG}: {msg}" )
498+ } else {
499+ ERROR_MSG . to_string ( )
500+ }
501+ } ) ?;
495502
496503 let first_span = parser. token . span . data ( ) ;
497504 let minus_present = parser. eat ( exp ! ( Minus ) ) ;
498505
499506 let lit_span = parser. token . span . data ( ) ;
500507 let token:: Literal ( mut lit) = parser. token . kind else {
501- return Err ( ( ) ) ;
508+ return Err ( "not a literal" . to_string ( ) ) ;
502509 } ;
503510
504511 // Check no comment or whitespace surrounding the (possibly negative)
505512 // literal, or more tokens after it.
506513 if ( lit_span. hi . 0 - first_span. lo . 0 ) as usize != s. len ( ) {
507- return Err ( ( ) ) ;
514+ return Err ( "comment or whitespace around literal" . to_string ( ) ) ;
508515 }
509516
510517 if minus_present {
511518 // If minus is present, check no comment or whitespace in between it
512519 // and the literal token.
513520 if first_span. hi . 0 != lit_span. lo . 0 {
514- return Err ( ( ) ) ;
521+ return Err ( "comment or whitespace after minus" . to_string ( ) ) ;
515522 }
516523
517524 // Check literal is a kind we allow to be negated in a proc macro token.
@@ -525,7 +532,9 @@ impl server::FreeFunctions for Rustc<'_, '_> {
525532 | token:: LitKind :: ByteStrRaw ( _)
526533 | token:: LitKind :: CStr
527534 | token:: LitKind :: CStrRaw ( _)
528- | token:: LitKind :: Err ( _) => return Err ( ( ) ) ,
535+ | token:: LitKind :: Err ( _) => {
536+ return Err ( "non-numeric literal may not be negated" . to_string ( ) ) ;
537+ }
529538 token:: LitKind :: Integer | token:: LitKind :: Float => { }
530539 }
531540
@@ -562,13 +571,24 @@ impl server::TokenStream for Rustc<'_, '_> {
562571 stream. is_empty ( )
563572 }
564573
565- fn from_str ( & mut self , src : & str ) -> Self :: TokenStream {
566- unwrap_or_emit_fatal ( source_str_to_stream (
574+ fn from_str ( & mut self , src : & str ) -> Result < Self :: TokenStream , String > {
575+ const ERROR_MSG : & str = "cannot parse string into token stream" ;
576+
577+ source_str_to_stream (
567578 self . psess ( ) ,
568579 FileName :: proc_macro_source_code ( src) ,
569580 src. to_string ( ) ,
570581 Some ( self . call_site ) ,
571- ) )
582+ )
583+ . map_err ( |diags| {
584+ let mut messages = diags. into_iter ( ) . map ( Diag :: cancel_into_message) . flatten ( ) ;
585+ if let Some ( msg) = messages. next ( ) {
586+ messages. for_each ( drop) ;
587+ format ! ( "{ERROR_MSG}: {msg}" )
588+ } else {
589+ ERROR_MSG . to_string ( )
590+ }
591+ } )
572592 }
573593
574594 fn to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
0 commit comments