@@ -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 , AttributeOnGenericArg , 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,12 @@ 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
+ 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
+ }
883
889
let start = self . token . span ;
884
890
let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
885
891
// Parse lifetime argument.
@@ -934,6 +940,9 @@ impl<'a> Parser<'a> {
934
940
}
935
941
} else if self . token . is_keyword ( kw:: Const ) {
936
942
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) ;
937
946
} else {
938
947
// Fall back by trying to parse a const-expr expression. If we successfully do so,
939
948
// then we should report an error that it needs to be wrapped in braces.
@@ -953,6 +962,22 @@ impl<'a> Parser<'a> {
953
962
}
954
963
}
955
964
} ;
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
+
956
981
Ok ( Some ( arg) )
957
982
}
958
983
0 commit comments