@@ -17,8 +17,8 @@ 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 , AttributeOnEmptyGeneric , AttributeOnGenericType , FnPathFoundNamedParams ,
21+ PathFoundAttributeInParams , PathFoundCVariadicParams , PathSingleColon , PathTripleColon ,
2222} ;
2323use crate :: exp;
2424use crate :: parser:: { CommaRecoveryMode , RecoverColon , RecoverComma } ;
@@ -880,6 +880,15 @@ 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+ // Parse attribute.
886+ let attrs_wrapper = self . parse_outer_attributes ( ) ?;
887+ if !attrs_wrapper. is_empty ( ) {
888+ let raw_attrs = attrs_wrapper. attrs ( ) ;
889+ attr_span = Some ( raw_attrs[ 0 ] . span . to ( raw_attrs. last ( ) . unwrap ( ) . span ) ) ;
890+ }
891+ }
883892 let start = self . token . span ;
884893 let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
885894 // Parse lifetime argument.
@@ -934,6 +943,10 @@ impl<'a> Parser<'a> {
934943 }
935944 } else if self . token . is_keyword ( kw:: Const ) {
936945 return self . recover_const_param_declaration ( ty_generics) ;
946+ } else if let Some ( attr_span) = attr_span {
947+ // Handle case where attribute is present but no valid generic argument follows.
948+ let guar = self . dcx ( ) . emit_err ( AttributeOnEmptyGeneric { span : attr_span } ) ;
949+ return Ok ( Some ( GenericArg :: Type ( self . mk_ty ( attr_span, TyKind :: Err ( guar) ) ) ) ) ;
937950 } else {
938951 // Fall back by trying to parse a const-expr expression. If we successfully do so,
939952 // then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +966,18 @@ impl<'a> Parser<'a> {
953966 }
954967 }
955968 } ;
969+
970+ if let Some ( attr_span) = attr_span
971+ && !( arg. span ( ) . is_empty ( ) || arg. span ( ) . is_dummy ( ) )
972+ {
973+ let guar = self . dcx ( ) . emit_err ( AttributeOnGenericType {
974+ span : attr_span,
975+ fix_span : attr_span. until ( arg. span ( ) ) ,
976+ } ) ;
977+
978+ return Ok ( Some ( GenericArg :: Type ( self . mk_ty ( attr_span, TyKind :: Err ( guar) ) ) ) ) ;
979+ }
980+
956981 Ok ( Some ( arg) )
957982 }
958983
0 commit comments