1- use crate :: errors;
2-
31use super :: diagnostics:: { dummy_arg, ConsumeClosingDelim } ;
42use super :: ty:: { AllowPlus , RecoverQPath , RecoverReturnSign } ;
53use super :: { AttrWrapper , FollowedByType , ForceCollect , Parser , PathStyle , TrailingToken } ;
4+ use crate :: errors:: { self , MacroExpandsToAdtField } ;
5+ use crate :: fluent_generated as fluent;
66use ast:: StaticItem ;
77use rustc_ast:: ast:: * ;
88use rustc_ast:: ptr:: P ;
@@ -1342,6 +1342,13 @@ impl<'a> Parser<'a> {
13421342 }
13431343 let ident = this. parse_field_ident ( "enum" , vlo) ?;
13441344
1345+ if this. token == token:: Not {
1346+ return this. unexpected ( ) . map_err ( |mut err| {
1347+ err. note ( fluent:: parse_macro_expands_to_enum_variant) ;
1348+ err
1349+ } ) ;
1350+ }
1351+
13451352 let struct_def = if this. check ( & token:: OpenDelim ( Delimiter :: Brace ) ) {
13461353 // Parse a struct variant.
13471354 let ( fields, recovered) =
@@ -1369,7 +1376,7 @@ impl<'a> Parser<'a> {
13691376
13701377 Ok ( ( Some ( vr) , TrailingToken :: MaybeComma ) )
13711378 } ,
1372- ) . map_err ( |mut err|{
1379+ ) . map_err ( |mut err| {
13731380 err. help ( "enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`" ) ;
13741381 err
13751382 } )
@@ -1691,9 +1698,10 @@ impl<'a> Parser<'a> {
16911698 Ok ( a_var)
16921699 }
16931700
1694- fn expect_field_ty_separator ( & mut self ) -> PResult < ' a , ( ) > {
1701+ fn expect_field_ty_separator ( & mut self , adt_ty : & str ) -> PResult < ' a , ( ) > {
16951702 if let Err ( mut err) = self . expect ( & token:: Colon ) {
16961703 let sm = self . sess . source_map ( ) ;
1704+ let mac_invoc = self . token . kind == token:: Not ;
16971705 let eq_typo = self . token . kind == token:: Eq && self . look_ahead ( 1 , |t| t. is_path_start ( ) ) ;
16981706 let semi_typo = self . token . kind == token:: Semi
16991707 && self . look_ahead ( 1 , |t| {
@@ -1705,16 +1713,18 @@ impl<'a> Parser<'a> {
17051713 _ => true ,
17061714 }
17071715 } ) ;
1708- if eq_typo || semi_typo {
1716+ if mac_invoc {
1717+ err. subdiagnostic ( MacroExpandsToAdtField { adt_ty } ) . emit ( ) ;
1718+ } else if eq_typo || semi_typo {
17091719 self . bump ( ) ;
17101720 // Gracefully handle small typos.
17111721 err. span_suggestion_short (
17121722 self . prev_token . span ,
17131723 "field names and their types are separated with `:`" ,
17141724 ":" ,
17151725 Applicability :: MachineApplicable ,
1716- ) ;
1717- err . emit ( ) ;
1726+ )
1727+ . emit ( ) ;
17181728 } else {
17191729 return Err ( err) ;
17201730 }
@@ -1731,7 +1741,7 @@ impl<'a> Parser<'a> {
17311741 attrs : AttrVec ,
17321742 ) -> PResult < ' a , FieldDef > {
17331743 let name = self . parse_field_ident ( adt_ty, lo) ?;
1734- self . expect_field_ty_separator ( ) ?;
1744+ self . expect_field_ty_separator ( adt_ty ) ?;
17351745 let ty = self . parse_ty ( ) ?;
17361746 if self . token . kind == token:: Colon && self . look_ahead ( 1 , |tok| tok. kind != token:: Colon ) {
17371747 self . sess . emit_err ( errors:: SingleColonStructType { span : self . token . span } ) ;
0 commit comments