@@ -37,6 +37,16 @@ impl CombineAttributeParser for ReprParser {
3737 } ;
3838
3939 if list. is_empty ( ) {
40+ cx. check_target (
41+ "()" ,
42+ & AllowedTargets :: AllowList ( & [
43+ Allow ( Target :: Struct ) ,
44+ Allow ( Target :: Enum ) ,
45+ Allow ( Target :: Union ) ,
46+ Warn ( Target :: MacroCall ) ,
47+ ] ) ,
48+ ) ;
49+
4050 let attr_span = cx. attr_span ;
4151 cx. adcx ( ) . warn_empty_attribute ( attr_span) ;
4252 return vec ! [ ] ;
@@ -53,51 +63,120 @@ impl CombineAttributeParser for ReprParser {
5363 reprs
5464 }
5565
56- //FIXME Still checked fully in `check_attr.rs`
57- //This one is slightly more complicated because the allowed targets depend on the arguments
58- const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( ALL_TARGETS ) ;
66+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: ManuallyChecked ;
5967 const STABILITY : AttributeStability = AttributeStability :: Stable ;
6068}
6169
6270fn parse_repr ( cx : & mut AcceptContext < ' _ , ' _ > , param : & MetaItemParser ) -> Option < ReprAttr > {
6371 use ReprAttr :: * ;
6472
65- macro_rules! no_args {
66- ( $constructor: expr) => { {
73+ macro_rules! repr_int {
74+ ( $arg: ident, $constructor: expr) => { {
75+ cx. check_target(
76+ concat!( "(" , stringify!( $arg) , ")" ) ,
77+ & AllowedTargets :: AllowList ( & [ Allow ( Target :: Enum ) , Warn ( Target :: MacroCall ) ] ) ,
78+ ) ;
6779 cx. expect_no_args( param. args( ) ) ?;
6880 Some ( $constructor)
6981 } } ;
7082 }
7183
7284 match param. path ( ) . word_sym ( ) {
7385 Some ( sym:: align) => {
86+ cx. check_target (
87+ "(align(...))" ,
88+ & AllowedTargets :: AllowList ( & [
89+ Allow ( Target :: Struct ) ,
90+ Allow ( Target :: Enum ) ,
91+ Allow ( Target :: Union ) ,
92+ Warn ( Target :: MacroCall ) ,
93+ ] ) ,
94+ ) ;
7495 let l = cx. expect_list ( param. args ( ) , param. span ( ) ) ?;
7596 parse_repr_align ( cx, l, AlignKind :: Align )
7697 }
77- Some ( sym:: packed) => match param. args ( ) {
78- ArgParser :: NoArgs => Some ( ReprPacked ( Align :: ONE ) ) ,
79- ArgParser :: List ( l) => parse_repr_align ( cx, l, AlignKind :: Packed ) ,
80- ArgParser :: NameValue ( _) => {
81- cx. adcx ( ) . expected_list_or_no_args ( param. span ( ) ) ;
82- None
98+ Some ( sym:: packed) => {
99+ cx. check_target (
100+ "(packed)" ,
101+ & AllowedTargets :: AllowList ( & [
102+ Allow ( Target :: Struct ) ,
103+ Allow ( Target :: Union ) ,
104+ Warn ( Target :: MacroCall ) ,
105+ ] ) ,
106+ ) ;
107+ match param. args ( ) {
108+ ArgParser :: NoArgs => Some ( ReprPacked ( Align :: ONE ) ) ,
109+ ArgParser :: List ( l) => parse_repr_align ( cx, l, AlignKind :: Packed ) ,
110+ ArgParser :: NameValue ( _) => {
111+ cx. adcx ( ) . expected_list_or_no_args ( param. span ( ) ) ;
112+ None
113+ }
83114 }
84- } ,
85- Some ( sym:: Rust ) => no_args ! ( ReprRust ) ,
86- Some ( sym:: C ) => no_args ! ( ReprC ) ,
87- Some ( sym:: simd) => no_args ! ( ReprSimd ) ,
88- Some ( sym:: transparent) => no_args ! ( ReprTransparent ) ,
89- Some ( sym:: i8) => no_args ! ( ReprInt ( SignedInt ( IntTy :: I8 ) ) ) ,
90- Some ( sym:: u8) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: U8 ) ) ) ,
91- Some ( sym:: i16) => no_args ! ( ReprInt ( SignedInt ( IntTy :: I16 ) ) ) ,
92- Some ( sym:: u16) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: U16 ) ) ) ,
93- Some ( sym:: i32) => no_args ! ( ReprInt ( SignedInt ( IntTy :: I32 ) ) ) ,
94- Some ( sym:: u32) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: U32 ) ) ) ,
95- Some ( sym:: i64) => no_args ! ( ReprInt ( SignedInt ( IntTy :: I64 ) ) ) ,
96- Some ( sym:: u64) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: U64 ) ) ) ,
97- Some ( sym:: i128) => no_args ! ( ReprInt ( SignedInt ( IntTy :: I128 ) ) ) ,
98- Some ( sym:: u128) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: U128 ) ) ) ,
99- Some ( sym:: isize) => no_args ! ( ReprInt ( SignedInt ( IntTy :: Isize ) ) ) ,
100- Some ( sym:: usize) => no_args ! ( ReprInt ( UnsignedInt ( UintTy :: Usize ) ) ) ,
115+ }
116+
117+ Some ( sym:: Rust ) => {
118+ cx. check_target (
119+ "(Rust)" ,
120+ & AllowedTargets :: AllowList ( & [
121+ Allow ( Target :: Struct ) ,
122+ Allow ( Target :: Enum ) ,
123+ Allow ( Target :: Union ) ,
124+ Warn ( Target :: MacroCall ) ,
125+ ] ) ,
126+ ) ;
127+ cx. expect_no_args ( param. args ( ) ) ?;
128+ Some ( ReprRust )
129+ }
130+ Some ( sym:: C ) => {
131+ cx. check_target (
132+ "(C)" ,
133+ & AllowedTargets :: AllowList ( & [
134+ Allow ( Target :: Struct ) ,
135+ Allow ( Target :: Enum ) ,
136+ Allow ( Target :: Union ) ,
137+ Warn ( Target :: MacroCall ) ,
138+ ] ) ,
139+ ) ;
140+ cx. expect_no_args ( param. args ( ) ) ?;
141+ Some ( ReprC )
142+ }
143+ Some ( sym:: simd) => {
144+ cx. check_target (
145+ "(simd)" ,
146+ & AllowedTargets :: AllowList ( & [
147+ Allow ( Target :: Struct ) , // Feature gated in `rustc_ast_passes`
148+ Warn ( Target :: MacroCall ) , // FIXME: This is not feature gated (!!)
149+ ] ) ,
150+ ) ;
151+ cx. expect_no_args ( param. args ( ) ) ?;
152+ Some ( ReprSimd )
153+ }
154+ Some ( sym:: transparent) => {
155+ cx. check_target (
156+ "(transparent)" ,
157+ & AllowedTargets :: AllowList ( & [
158+ Allow ( Target :: Struct ) ,
159+ Allow ( Target :: Enum ) ,
160+ Allow ( Target :: Union ) , // Feature gated in `rustc_hir_analysis`
161+ Warn ( Target :: MacroCall ) ,
162+ ] ) ,
163+ ) ;
164+ cx. expect_no_args ( param. args ( ) ) ?;
165+ Some ( ReprTransparent )
166+ }
167+
168+ Some ( sym:: i8) => repr_int ! ( i8 , ReprInt ( SignedInt ( IntTy :: I8 ) ) ) ,
169+ Some ( sym:: u8) => repr_int ! ( u8 , ReprInt ( UnsignedInt ( UintTy :: U8 ) ) ) ,
170+ Some ( sym:: i16) => repr_int ! ( i16 , ReprInt ( SignedInt ( IntTy :: I16 ) ) ) ,
171+ Some ( sym:: u16) => repr_int ! ( u16 , ReprInt ( UnsignedInt ( UintTy :: U16 ) ) ) ,
172+ Some ( sym:: i32) => repr_int ! ( i32 , ReprInt ( SignedInt ( IntTy :: I32 ) ) ) ,
173+ Some ( sym:: u32) => repr_int ! ( u32 , ReprInt ( UnsignedInt ( UintTy :: U32 ) ) ) ,
174+ Some ( sym:: i64) => repr_int ! ( i64 , ReprInt ( SignedInt ( IntTy :: I64 ) ) ) ,
175+ Some ( sym:: u64) => repr_int ! ( u64 , ReprInt ( UnsignedInt ( UintTy :: U64 ) ) ) ,
176+ Some ( sym:: i128) => repr_int ! ( i128 , ReprInt ( SignedInt ( IntTy :: I128 ) ) ) ,
177+ Some ( sym:: u128) => repr_int ! ( u128 , ReprInt ( UnsignedInt ( UintTy :: U128 ) ) ) ,
178+ Some ( sym:: isize) => repr_int ! ( isize , ReprInt ( SignedInt ( IntTy :: Isize ) ) ) ,
179+ Some ( sym:: usize) => repr_int ! ( usize , ReprInt ( UnsignedInt ( UintTy :: Usize ) ) ) ,
101180 _ => {
102181 cx. adcx ( ) . expected_specific_argument (
103182 param. span ( ) ,
0 commit comments