@@ -10,7 +10,7 @@ use rustc_ast::visit::{AssocCtxt, Visitor};
1010use rustc_attr:: { self as attr, Deprecation , HasAttrs , Stability } ;
1111use rustc_data_structures:: fx:: FxHashMap ;
1212use rustc_data_structures:: sync:: { self , Lrc } ;
13- use rustc_errors:: { DiagnosticBuilder , DiagnosticId } ;
13+ use rustc_errors:: { DiagnosticBuilder , ErrorReported } ;
1414use rustc_parse:: { self , parser, MACRO_ARGUMENTS } ;
1515use rustc_session:: parse:: ParseSess ;
1616use rustc_span:: edition:: Edition ;
@@ -296,16 +296,26 @@ where
296296}
297297
298298pub trait ProcMacro {
299- fn expand < ' cx > ( & self , ecx : & ' cx mut ExtCtxt < ' _ > , span : Span , ts : TokenStream ) -> TokenStream ;
299+ fn expand < ' cx > (
300+ & self ,
301+ ecx : & ' cx mut ExtCtxt < ' _ > ,
302+ span : Span ,
303+ ts : TokenStream ,
304+ ) -> Result < TokenStream , ErrorReported > ;
300305}
301306
302307impl < F > ProcMacro for F
303308where
304309 F : Fn ( TokenStream ) -> TokenStream ,
305310{
306- fn expand < ' cx > ( & self , _ecx : & ' cx mut ExtCtxt < ' _ > , _span : Span , ts : TokenStream ) -> TokenStream {
311+ fn expand < ' cx > (
312+ & self ,
313+ _ecx : & ' cx mut ExtCtxt < ' _ > ,
314+ _span : Span ,
315+ ts : TokenStream ,
316+ ) -> Result < TokenStream , ErrorReported > {
307317 // FIXME setup implicit context in TLS before calling self.
308- ( * self ) ( ts)
318+ Ok ( ( * self ) ( ts) )
309319 }
310320}
311321
@@ -316,7 +326,7 @@ pub trait AttrProcMacro {
316326 span : Span ,
317327 annotation : TokenStream ,
318328 annotated : TokenStream ,
319- ) -> TokenStream ;
329+ ) -> Result < TokenStream , ErrorReported > ;
320330}
321331
322332impl < F > AttrProcMacro for F
@@ -329,9 +339,9 @@ where
329339 _span : Span ,
330340 annotation : TokenStream ,
331341 annotated : TokenStream ,
332- ) -> TokenStream {
342+ ) -> Result < TokenStream , ErrorReported > {
333343 // FIXME setup implicit context in TLS before calling self.
334- ( * self ) ( annotation, annotated)
344+ Ok ( ( * self ) ( annotation, annotated) )
335345 }
336346}
337347
@@ -1004,31 +1014,9 @@ impl<'a> ExtCtxt<'a> {
10041014 self . current_expansion . id . expansion_cause ( )
10051015 }
10061016
1007- pub fn struct_span_warn < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
1008- self . parse_sess . span_diagnostic . struct_span_warn ( sp, msg)
1009- }
10101017 pub fn struct_span_err < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
10111018 self . parse_sess . span_diagnostic . struct_span_err ( sp, msg)
10121019 }
1013- pub fn struct_span_fatal < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
1014- self . parse_sess . span_diagnostic . struct_span_fatal ( sp, msg)
1015- }
1016-
1017- /// Emit `msg` attached to `sp`, and stop compilation immediately.
1018- ///
1019- /// `span_err` should be strongly preferred where-ever possible:
1020- /// this should *only* be used when:
1021- ///
1022- /// - continuing has a high risk of flow-on errors (e.g., errors in
1023- /// declaring a macro would cause all uses of that macro to
1024- /// complain about "undefined macro"), or
1025- /// - there is literally nothing else that can be done (however,
1026- /// in most cases one can construct a dummy expression/item to
1027- /// substitute; we never hit resolve/type-checking so the dummy
1028- /// value doesn't have to match anything)
1029- pub fn span_fatal < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> ! {
1030- self . parse_sess . span_diagnostic . span_fatal ( sp, msg) . raise ( ) ;
1031- }
10321020
10331021 /// Emit `msg` attached to `sp`, without immediately stopping
10341022 /// compilation.
@@ -1038,9 +1026,6 @@ impl<'a> ExtCtxt<'a> {
10381026 pub fn span_err < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
10391027 self . parse_sess . span_diagnostic . span_err ( sp, msg) ;
10401028 }
1041- pub fn span_err_with_code < S : Into < MultiSpan > > ( & self , sp : S , msg : & str , code : DiagnosticId ) {
1042- self . parse_sess . span_diagnostic . span_err_with_code ( sp, msg, code) ;
1043- }
10441029 pub fn span_warn < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
10451030 self . parse_sess . span_diagnostic . span_warn ( sp, msg) ;
10461031 }
@@ -1168,6 +1153,18 @@ pub fn check_zero_tts(cx: &ExtCtxt<'_>, sp: Span, tts: TokenStream, name: &str)
11681153 }
11691154}
11701155
1156+ /// Parse an expression. On error, emit it, advancing to `Eof`, and return `None`.
1157+ pub fn parse_expr ( p : & mut parser:: Parser < ' _ > ) -> Option < P < ast:: Expr > > {
1158+ match p. parse_expr ( ) {
1159+ Ok ( e) => return Some ( e) ,
1160+ Err ( mut err) => err. emit ( ) ,
1161+ }
1162+ while p. token != token:: Eof {
1163+ p. bump ( ) ;
1164+ }
1165+ None
1166+ }
1167+
11711168/// Interpreting `tts` as a comma-separated sequence of expressions,
11721169/// expect exactly one string literal, or emit an error and return `None`.
11731170pub fn get_single_str_from_tts (
@@ -1181,7 +1178,7 @@ pub fn get_single_str_from_tts(
11811178 cx. span_err ( sp, & format ! ( "{} takes 1 argument" , name) ) ;
11821179 return None ;
11831180 }
1184- let ret = panictry ! ( p . parse_expr( ) ) ;
1181+ let ret = parse_expr ( & mut p ) ? ;
11851182 let _ = p. eat ( & token:: Comma ) ;
11861183
11871184 if p. token != token:: Eof {
@@ -1190,8 +1187,8 @@ pub fn get_single_str_from_tts(
11901187 expr_to_string ( cx, ret, "argument must be a string literal" ) . map ( |( s, _) | s. to_string ( ) )
11911188}
11921189
1193- /// Extracts comma-separated expressions from `tts`. If there is a
1194- /// parsing error, emit a non-fatal error and return `None`.
1190+ /// Extracts comma-separated expressions from `tts`.
1191+ /// On error, emit it, and return `None`.
11951192pub fn get_exprs_from_tts (
11961193 cx : & mut ExtCtxt < ' _ > ,
11971194 sp : Span ,
@@ -1200,7 +1197,7 @@ pub fn get_exprs_from_tts(
12001197 let mut p = cx. new_parser_from_tts ( tts) ;
12011198 let mut es = Vec :: new ( ) ;
12021199 while p. token != token:: Eof {
1203- let expr = panictry ! ( p . parse_expr( ) ) ;
1200+ let expr = parse_expr ( & mut p ) ? ;
12041201
12051202 // Perform eager expansion on the expression.
12061203 // We want to be able to handle e.g., `concat!("foo", "bar")`.
0 commit comments