@@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashMap;
1010use rustc_errors:: { Diag , ErrorGuaranteed , MultiSpan } ;
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 ( ) . flat_map ( Diag :: cancel_into_message) ;
436+ let msg = messages. next ( ) . expect ( "no diagnostic has a message" ) ;
437+ messages. for_each ( |_| ( ) ) ; // consume iterator to cancel the remaining diagnostics
438+ msg
439+ }
440+
434441pub ( crate ) struct Rustc < ' a , ' b > {
435442 ecx : & ' a mut ExtCtxt < ' b > ,
436443 def_site : Span ,
@@ -494,35 +501,32 @@ impl server::Server for Rustc<'_, '_> {
494501 self . psess ( ) . file_depinfo . borrow_mut ( ) . insert ( Symbol :: intern ( path) ) ;
495502 }
496503
497- fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , ( ) > {
504+ fn literal_from_str ( & mut self , s : & str ) -> Result < Literal < Self :: Span , Self :: Symbol > , String > {
498505 let name = FileName :: proc_macro_source_code ( s) ;
499506
500- let mut parser = unwrap_or_emit_fatal ( new_parser_from_source_str (
501- self . psess ( ) ,
502- name,
503- s. to_owned ( ) ,
504- StripTokens :: Nothing ,
505- ) ) ;
507+ let mut parser =
508+ new_parser_from_source_str ( self . psess ( ) , name, s. to_owned ( ) , StripTokens :: Nothing )
509+ . map_err ( cancel_diags_into_string) ?;
506510
507511 let first_span = parser. token . span . data ( ) ;
508512 let minus_present = parser. eat ( exp ! ( Minus ) ) ;
509513
510514 let lit_span = parser. token . span . data ( ) ;
511515 let token:: Literal ( mut lit) = parser. token . kind else {
512- return Err ( ( ) ) ;
516+ return Err ( "not a literal" . to_string ( ) ) ;
513517 } ;
514518
515519 // Check no comment or whitespace surrounding the (possibly negative)
516520 // literal, or more tokens after it.
517521 if ( lit_span. hi . 0 - first_span. lo . 0 ) as usize != s. len ( ) {
518- return Err ( ( ) ) ;
522+ return Err ( "comment or whitespace around literal" . to_string ( ) ) ;
519523 }
520524
521525 if minus_present {
522526 // If minus is present, check no comment or whitespace in between it
523527 // and the literal token.
524528 if first_span. hi . 0 != lit_span. lo . 0 {
525- return Err ( ( ) ) ;
529+ return Err ( "comment or whitespace after minus" . to_string ( ) ) ;
526530 }
527531
528532 // Check literal is a kind we allow to be negated in a proc macro token.
@@ -536,7 +540,9 @@ impl server::Server for Rustc<'_, '_> {
536540 | token:: LitKind :: ByteStrRaw ( _)
537541 | token:: LitKind :: CStr
538542 | token:: LitKind :: CStrRaw ( _)
539- | token:: LitKind :: Err ( _) => return Err ( ( ) ) ,
543+ | token:: LitKind :: Err ( _) => {
544+ return Err ( "non-numeric literal may not be negated" . to_string ( ) ) ;
545+ }
540546 token:: LitKind :: Integer | token:: LitKind :: Float => { }
541547 }
542548
@@ -576,13 +582,14 @@ impl server::Server for Rustc<'_, '_> {
576582 stream. is_empty ( )
577583 }
578584
579- fn ts_from_str ( & mut self , src : & str ) -> Self :: TokenStream {
580- unwrap_or_emit_fatal ( source_str_to_stream (
585+ fn ts_from_str ( & mut self , src : & str ) -> Result < Self :: TokenStream , String > {
586+ source_str_to_stream (
581587 self . psess ( ) ,
582588 FileName :: proc_macro_source_code ( src) ,
583589 src. to_string ( ) ,
584590 Some ( self . call_site ) ,
585- ) )
591+ )
592+ . map_err ( cancel_diags_into_string)
586593 }
587594
588595 fn ts_to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
0 commit comments