@@ -5116,12 +5116,8 @@ impl<'a> Parser<'a> {
5116
5116
5117
5117
let ident = self . parse_ident ( ) ?;
5118
5118
let ( delim, tokens) = self . expect_delimited_token_tree ( ) ?;
5119
- if delim != MacDelimiter :: Brace {
5120
- if !self . eat ( & token:: Semi ) {
5121
- let msg = "macros that expand to items must either \
5122
- be surrounded with braces or followed by a semicolon";
5123
- self . span_err ( self . prev_span , msg) ;
5124
- }
5119
+ if delim != MacDelimiter :: Brace && !self . eat ( & token:: Semi ) {
5120
+ self . report_invalid_macro_expansion_item ( ) ;
5125
5121
}
5126
5122
5127
5123
( ident, ast:: MacroDef { tokens : tokens, legacy : true } )
@@ -5264,13 +5260,8 @@ impl<'a> Parser<'a> {
5264
5260
// if it has a special ident, it's definitely an item
5265
5261
//
5266
5262
// Require a semicolon or braces.
5267
- if style != MacStmtStyle :: Braces {
5268
- if !self . eat ( & token:: Semi ) {
5269
- self . span_err ( self . prev_span ,
5270
- "macros that expand to items must \
5271
- either be surrounded with braces or \
5272
- followed by a semicolon") ;
5273
- }
5263
+ if style != MacStmtStyle :: Braces && !self . eat ( & token:: Semi ) {
5264
+ self . report_invalid_macro_expansion_item ( ) ;
5274
5265
}
5275
5266
let span = lo. to ( hi) ;
5276
5267
Stmt {
@@ -8360,13 +8351,8 @@ impl<'a> Parser<'a> {
8360
8351
} ;
8361
8352
// eat a matched-delimiter token tree:
8362
8353
let ( delim, tts) = self . expect_delimited_token_tree ( ) ?;
8363
- if delim != MacDelimiter :: Brace {
8364
- if !self . eat ( & token:: Semi ) {
8365
- self . span_err ( self . prev_span ,
8366
- "macros that expand to items must either \
8367
- be surrounded with braces or followed by \
8368
- a semicolon") ;
8369
- }
8354
+ if delim != MacDelimiter :: Brace && !self . eat ( & token:: Semi ) {
8355
+ self . report_invalid_macro_expansion_item ( ) ;
8370
8356
}
8371
8357
8372
8358
let hi = self . prev_span ;
@@ -8597,6 +8583,25 @@ impl<'a> Parser<'a> {
8597
8583
}
8598
8584
}
8599
8585
}
8586
+
8587
+ fn report_invalid_macro_expansion_item ( & self ) {
8588
+ self . struct_span_err (
8589
+ self . prev_span ,
8590
+ "macros that expand to items must be delimited with braces or followed by a semicolon" ,
8591
+ ) . multipart_suggestion (
8592
+ "change the delimiters to curly braces" ,
8593
+ vec ! [
8594
+ ( self . prev_span. with_hi( self . prev_span. lo( ) + BytePos ( 1 ) ) , String :: from( " {" ) ) ,
8595
+ ( self . prev_span. with_lo( self . prev_span. hi( ) - BytePos ( 1 ) ) , '}' . to_string( ) ) ,
8596
+ ] ,
8597
+ Applicability :: MaybeIncorrect ,
8598
+ ) . span_suggestion (
8599
+ self . sess . source_map . next_point ( self . prev_span ) ,
8600
+ "add a semicolon" ,
8601
+ ';' . to_string ( ) ,
8602
+ Applicability :: MaybeIncorrect ,
8603
+ ) . emit ( ) ;
8604
+ }
8600
8605
}
8601
8606
8602
8607
pub fn emit_unclosed_delims ( unclosed_delims : & mut Vec < UnmatchedBrace > , handler : & errors:: Handler ) {
0 commit comments