@@ -17,11 +17,11 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
1717use super :: { Parser , Restrictions , TokenType } ;
1818use crate :: ast:: { PatKind , TyKind } ;
1919use crate :: errors:: {
20- self , FnPathFoundNamedParams , PathFoundAttributeInParams , PathFoundCVariadicParams ,
21- PathSingleColon , PathTripleColon ,
20+ self , AttributeOnEmptyType , AttributeOnGenericArg , FnPathFoundNamedParams ,
21+ PathFoundAttributeInParams , PathFoundCVariadicParams , PathSingleColon , PathTripleColon ,
2222} ;
2323use crate :: exp;
24- use crate :: parser:: { CommaRecoveryMode , RecoverColon , RecoverComma } ;
24+ use crate :: parser:: { CommaRecoveryMode , ExprKind , RecoverColon , RecoverComma } ;
2525
2626/// Specifies how to parse a path.
2727#[ derive( Copy , Clone , PartialEq ) ]
@@ -880,6 +880,12 @@ impl<'a> Parser<'a> {
880880 & mut self ,
881881 ty_generics : Option < & Generics > ,
882882 ) -> PResult < ' a , Option < GenericArg > > {
883+ let mut attr_span: Option < Span > = None ;
884+ if self . token == token:: Pound && self . look_ahead ( 1 , |t| * t == token:: OpenBracket ) {
885+ let attrs_wrapper = self . parse_outer_attributes ( ) ?;
886+ let raw_attrs = attrs_wrapper. take_for_recovery ( self . psess ) ;
887+ attr_span = Some ( raw_attrs[ 0 ] . span . to ( raw_attrs. last ( ) . unwrap ( ) . span ) ) ;
888+ }
883889 let start = self . token . span ;
884890 let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
885891 // Parse lifetime argument.
@@ -934,6 +940,9 @@ impl<'a> Parser<'a> {
934940 }
935941 } else if self . token . is_keyword ( kw:: Const ) {
936942 return self . recover_const_param_declaration ( ty_generics) ;
943+ } else if let Some ( attr_span) = attr_span {
944+ let diag = self . dcx ( ) . create_err ( AttributeOnEmptyType { span : attr_span } ) ;
945+ return Err ( diag) ;
937946 } else {
938947 // Fall back by trying to parse a const-expr expression. If we successfully do so,
939948 // then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +962,22 @@ impl<'a> Parser<'a> {
953962 }
954963 }
955964 } ;
965+
966+ if let Some ( attr_span) = attr_span {
967+ let guar = self . dcx ( ) . emit_err ( AttributeOnGenericArg {
968+ span : attr_span,
969+ fix_span : attr_span. until ( arg. span ( ) ) ,
970+ } ) ;
971+ return Ok ( Some ( match arg {
972+ GenericArg :: Type ( _) => GenericArg :: Type ( self . mk_ty ( attr_span, TyKind :: Err ( guar) ) ) ,
973+ GenericArg :: Const ( _) => {
974+ let error_expr = self . mk_expr ( attr_span, ExprKind :: Err ( guar) ) ;
975+ GenericArg :: Const ( AnonConst { id : ast:: DUMMY_NODE_ID , value : error_expr } )
976+ }
977+ GenericArg :: Lifetime ( lt) => GenericArg :: Lifetime ( lt) ,
978+ } ) ) ;
979+ }
980+
956981 Ok ( Some ( arg) )
957982 }
958983
0 commit comments