@@ -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,37 @@ 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 > {
487487 let name = FileName :: proc_macro_source_code ( s) ;
488488
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- ) ) ;
489+ let mut parser =
490+ new_parser_from_source_str ( self . psess ( ) , name, s. to_owned ( ) , StripTokens :: Nothing )
491+ . map_err ( |diags| {
492+ let mut messages = diags. into_iter ( ) . map ( Diag :: cancel_into_message) . flatten ( ) ;
493+ let msg = messages. next ( ) . expect ( "no diagnostic has a message" ) ;
494+ messages. for_each ( drop) ;
495+ msg
496+ } ) ?;
495497
496498 let first_span = parser. token . span . data ( ) ;
497499 let minus_present = parser. eat ( exp ! ( Minus ) ) ;
498500
499501 let lit_span = parser. token . span . data ( ) ;
500502 let token:: Literal ( mut lit) = parser. token . kind else {
501- return Err ( ( ) ) ;
503+ return Err ( "not a literal" . to_string ( ) ) ;
502504 } ;
503505
504506 // Check no comment or whitespace surrounding the (possibly negative)
505507 // literal, or more tokens after it.
506508 if ( lit_span. hi . 0 - first_span. lo . 0 ) as usize != s. len ( ) {
507- return Err ( ( ) ) ;
509+ return Err ( "comment or whitespace around literal" . to_string ( ) ) ;
508510 }
509511
510512 if minus_present {
511513 // If minus is present, check no comment or whitespace in between it
512514 // and the literal token.
513515 if first_span. hi . 0 != lit_span. lo . 0 {
514- return Err ( ( ) ) ;
516+ return Err ( "comment or whitespace after minus" . to_string ( ) ) ;
515517 }
516518
517519 // Check literal is a kind we allow to be negated in a proc macro token.
@@ -525,7 +527,9 @@ impl server::FreeFunctions for Rustc<'_, '_> {
525527 | token:: LitKind :: ByteStrRaw ( _)
526528 | token:: LitKind :: CStr
527529 | token:: LitKind :: CStrRaw ( _)
528- | token:: LitKind :: Err ( _) => return Err ( ( ) ) ,
530+ | token:: LitKind :: Err ( _) => {
531+ return Err ( "non-numeric literal may not be negated" . to_string ( ) ) ;
532+ }
529533 token:: LitKind :: Integer | token:: LitKind :: Float => { }
530534 }
531535
@@ -562,13 +566,19 @@ impl server::TokenStream for Rustc<'_, '_> {
562566 stream. is_empty ( )
563567 }
564568
565- fn from_str ( & mut self , src : & str ) -> Self :: TokenStream {
566- unwrap_or_emit_fatal ( source_str_to_stream (
569+ fn from_str ( & mut self , src : & str ) -> Result < Self :: TokenStream , String > {
570+ source_str_to_stream (
567571 self . psess ( ) ,
568572 FileName :: proc_macro_source_code ( src) ,
569573 src. to_string ( ) ,
570574 Some ( self . call_site ) ,
571- ) )
575+ )
576+ . map_err ( |diags| {
577+ let mut messages = diags. into_iter ( ) . map ( Diag :: cancel_into_message) . flatten ( ) ;
578+ let msg = messages. next ( ) . expect ( "no diagnostic has a message" ) ;
579+ messages. for_each ( drop) ;
580+ msg
581+ } )
572582 }
573583
574584 fn to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
0 commit comments