@@ -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 , AttributeOnGenericType , 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,13 @@ 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+ let raw_attrs = attrs_wrapper. attrs ( ) ;
888+ attr_span = Some ( raw_attrs[ 0 ] . span . to ( raw_attrs. last ( ) . unwrap ( ) . span ) ) ;
889+ }
883890 let start = self . token . span ;
884891 let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
885892 // Parse lifetime argument.
@@ -934,6 +941,9 @@ impl<'a> Parser<'a> {
934941 }
935942 } else if self . token . is_keyword ( kw:: Const ) {
936943 return self . recover_const_param_declaration ( ty_generics) ;
944+ } else if let Some ( attr_span) = attr_span {
945+ let diag = self . dcx ( ) . create_err ( AttributeOnEmptyType { span : attr_span } ) ;
946+ return Err ( diag) ;
937947 } else {
938948 // Fall back by trying to parse a const-expr expression. If we successfully do so,
939949 // then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +963,23 @@ impl<'a> Parser<'a> {
953963 }
954964 }
955965 } ;
966+
967+ if let Some ( attr_span) = attr_span {
968+ let guar = self . dcx ( ) . emit_err ( AttributeOnGenericType {
969+ span : attr_span,
970+ fix_span : attr_span. until ( arg. span ( ) ) ,
971+ } ) ;
972+ return Ok ( Some ( match arg {
973+ GenericArg :: Type ( _) => GenericArg :: Type ( self . mk_ty ( attr_span, TyKind :: Err ( guar) ) ) ,
974+ GenericArg :: Const ( _) => {
975+ // Create error const expression
976+ let error_expr = self . mk_expr ( attr_span, ExprKind :: Err ( guar) ) ;
977+ GenericArg :: Const ( AnonConst { id : ast:: DUMMY_NODE_ID , value : error_expr } )
978+ }
979+ GenericArg :: Lifetime ( lt) => GenericArg :: Lifetime ( lt) , // Less common case
980+ } ) ) ;
981+ }
982+
956983 Ok ( Some ( arg) )
957984 }
958985
0 commit comments