@@ -17,11 +17,11 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
17
17
use super :: { Parser , Restrictions , TokenType } ;
18
18
use crate :: ast:: { PatKind , TyKind } ;
19
19
use crate :: errors:: {
20
- self , FnPathFoundNamedParams , PathFoundAttributeInParams , PathFoundCVariadicParams ,
21
- PathSingleColon , PathTripleColon ,
20
+ self , AttributeOnEmptyType , AttributeOnGenericType , FnPathFoundNamedParams ,
21
+ PathFoundAttributeInParams , PathFoundCVariadicParams , PathSingleColon , PathTripleColon ,
22
22
} ;
23
23
use crate :: exp;
24
- use crate :: parser:: { CommaRecoveryMode , RecoverColon , RecoverComma } ;
24
+ use crate :: parser:: { CommaRecoveryMode , ExprKind , RecoverColon , RecoverComma } ;
25
25
26
26
/// Specifies how to parse a path.
27
27
#[ derive( Copy , Clone , PartialEq ) ]
@@ -880,6 +880,13 @@ impl<'a> Parser<'a> {
880
880
& mut self ,
881
881
ty_generics : Option < & Generics > ,
882
882
) -> 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
+ }
883
890
let start = self . token . span ;
884
891
let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
885
892
// Parse lifetime argument.
@@ -934,6 +941,9 @@ impl<'a> Parser<'a> {
934
941
}
935
942
} else if self . token . is_keyword ( kw:: Const ) {
936
943
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) ;
937
947
} else {
938
948
// Fall back by trying to parse a const-expr expression. If we successfully do so,
939
949
// then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +963,23 @@ impl<'a> Parser<'a> {
953
963
}
954
964
}
955
965
} ;
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
+
956
983
Ok ( Some ( arg) )
957
984
}
958
985
0 commit comments