@@ -9,7 +9,7 @@ use rustc_ast::visit::{AssocCtxt, Visitor};
99use rustc_attr:: { self as attr, Deprecation , HasAttrs , Stability } ;
1010use rustc_data_structures:: fx:: FxHashMap ;
1111use rustc_data_structures:: sync:: { self , Lrc } ;
12- use rustc_errors:: { DiagnosticBuilder , DiagnosticId } ;
12+ use rustc_errors:: { DiagnosticBuilder , ErrorReported } ;
1313use rustc_parse:: { self , parser, DirectoryOwnership , MACRO_ARGUMENTS } ;
1414use rustc_session:: parse:: ParseSess ;
1515use rustc_span:: edition:: Edition ;
@@ -295,16 +295,26 @@ where
295295}
296296
297297pub trait ProcMacro {
298- fn expand < ' cx > ( & self , ecx : & ' cx mut ExtCtxt < ' _ > , span : Span , ts : TokenStream ) -> TokenStream ;
298+ fn expand < ' cx > (
299+ & self ,
300+ ecx : & ' cx mut ExtCtxt < ' _ > ,
301+ span : Span ,
302+ ts : TokenStream ,
303+ ) -> Result < TokenStream , ErrorReported > ;
299304}
300305
301306impl < F > ProcMacro for F
302307where
303308 F : Fn ( TokenStream ) -> TokenStream ,
304309{
305- fn expand < ' cx > ( & self , _ecx : & ' cx mut ExtCtxt < ' _ > , _span : Span , ts : TokenStream ) -> TokenStream {
310+ fn expand < ' cx > (
311+ & self ,
312+ _ecx : & ' cx mut ExtCtxt < ' _ > ,
313+ _span : Span ,
314+ ts : TokenStream ,
315+ ) -> Result < TokenStream , ErrorReported > {
306316 // FIXME setup implicit context in TLS before calling self.
307- ( * self ) ( ts)
317+ Ok ( ( * self ) ( ts) )
308318 }
309319}
310320
@@ -315,7 +325,7 @@ pub trait AttrProcMacro {
315325 span : Span ,
316326 annotation : TokenStream ,
317327 annotated : TokenStream ,
318- ) -> TokenStream ;
328+ ) -> Result < TokenStream , ErrorReported > ;
319329}
320330
321331impl < F > AttrProcMacro for F
@@ -328,9 +338,9 @@ where
328338 _span : Span ,
329339 annotation : TokenStream ,
330340 annotated : TokenStream ,
331- ) -> TokenStream {
341+ ) -> Result < TokenStream , ErrorReported > {
332342 // FIXME setup implicit context in TLS before calling self.
333- ( * self ) ( annotation, annotated)
343+ Ok ( ( * self ) ( annotation, annotated) )
334344 }
335345}
336346
@@ -999,31 +1009,9 @@ impl<'a> ExtCtxt<'a> {
9991009 self . current_expansion . id . expansion_cause ( )
10001010 }
10011011
1002- pub fn struct_span_warn < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
1003- self . parse_sess . span_diagnostic . struct_span_warn ( sp, msg)
1004- }
10051012 pub fn struct_span_err < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
10061013 self . parse_sess . span_diagnostic . struct_span_err ( sp, msg)
10071014 }
1008- pub fn struct_span_fatal < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> DiagnosticBuilder < ' a > {
1009- self . parse_sess . span_diagnostic . struct_span_fatal ( sp, msg)
1010- }
1011-
1012- /// Emit `msg` attached to `sp`, and stop compilation immediately.
1013- ///
1014- /// `span_err` should be strongly preferred where-ever possible:
1015- /// this should *only* be used when:
1016- ///
1017- /// - continuing has a high risk of flow-on errors (e.g., errors in
1018- /// declaring a macro would cause all uses of that macro to
1019- /// complain about "undefined macro"), or
1020- /// - there is literally nothing else that can be done (however,
1021- /// in most cases one can construct a dummy expression/item to
1022- /// substitute; we never hit resolve/type-checking so the dummy
1023- /// value doesn't have to match anything)
1024- pub fn span_fatal < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> ! {
1025- self . parse_sess . span_diagnostic . span_fatal ( sp, msg) . raise ( ) ;
1026- }
10271015
10281016 /// Emit `msg` attached to `sp`, without immediately stopping
10291017 /// compilation.
@@ -1033,9 +1021,6 @@ impl<'a> ExtCtxt<'a> {
10331021 pub fn span_err < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
10341022 self . parse_sess . span_diagnostic . span_err ( sp, msg) ;
10351023 }
1036- pub fn span_err_with_code < S : Into < MultiSpan > > ( & self , sp : S , msg : & str , code : DiagnosticId ) {
1037- self . parse_sess . span_diagnostic . span_err_with_code ( sp, msg, code) ;
1038- }
10391024 pub fn span_warn < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
10401025 self . parse_sess . span_diagnostic . span_warn ( sp, msg) ;
10411026 }
@@ -1163,6 +1148,18 @@ pub fn check_zero_tts(cx: &ExtCtxt<'_>, sp: Span, tts: TokenStream, name: &str)
11631148 }
11641149}
11651150
1151+ /// Parse an expression. On error, emit it, advancing to `Eof`, and return `None`.
1152+ pub fn parse_expr ( p : & mut parser:: Parser < ' _ > ) -> Option < P < ast:: Expr > > {
1153+ match p. parse_expr ( ) {
1154+ Ok ( e) => return Some ( e) ,
1155+ Err ( mut err) => err. emit ( ) ,
1156+ }
1157+ while p. token != token:: Eof {
1158+ p. bump ( ) ;
1159+ }
1160+ None
1161+ }
1162+
11661163/// Interpreting `tts` as a comma-separated sequence of expressions,
11671164/// expect exactly one string literal, or emit an error and return `None`.
11681165pub fn get_single_str_from_tts (
@@ -1176,7 +1173,7 @@ pub fn get_single_str_from_tts(
11761173 cx. span_err ( sp, & format ! ( "{} takes 1 argument" , name) ) ;
11771174 return None ;
11781175 }
1179- let ret = panictry ! ( p . parse_expr( ) ) ;
1176+ let ret = parse_expr ( & mut p ) ? ;
11801177 let _ = p. eat ( & token:: Comma ) ;
11811178
11821179 if p. token != token:: Eof {
@@ -1185,8 +1182,8 @@ pub fn get_single_str_from_tts(
11851182 expr_to_string ( cx, ret, "argument must be a string literal" ) . map ( |( s, _) | s. to_string ( ) )
11861183}
11871184
1188- /// Extracts comma-separated expressions from `tts`. If there is a
1189- /// parsing error, emit a non-fatal error and return `None`.
1185+ /// Extracts comma-separated expressions from `tts`.
1186+ /// On error, emit it, and return `None`.
11901187pub fn get_exprs_from_tts (
11911188 cx : & mut ExtCtxt < ' _ > ,
11921189 sp : Span ,
@@ -1195,7 +1192,7 @@ pub fn get_exprs_from_tts(
11951192 let mut p = cx. new_parser_from_tts ( tts) ;
11961193 let mut es = Vec :: new ( ) ;
11971194 while p. token != token:: Eof {
1198- let expr = panictry ! ( p . parse_expr( ) ) ;
1195+ let expr = parse_expr ( & mut p ) ? ;
11991196
12001197 // Perform eager expansion on the expression.
12011198 // We want to be able to handle e.g., `concat!("foo", "bar")`.
0 commit comments