@@ -3,7 +3,7 @@ use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
3
3
use rustc_attr_data_structures:: { AttributeKind , IntType , ReprAttr } ;
4
4
use rustc_span:: { DUMMY_SP , Span , Symbol , sym} ;
5
5
6
- use super :: { CombineAttributeParser , ConvertFn } ;
6
+ use super :: { AcceptMapping , AttributeParser , CombineAttributeParser , ConvertFn , FinalizeContext } ;
7
7
use crate :: context:: { AcceptContext , Stage } ;
8
8
use crate :: parser:: { ArgParser , MetaItemListParser , MetaItemParser } ;
9
9
use crate :: session_diagnostics;
@@ -262,3 +262,54 @@ fn parse_alignment(node: &LitKind) -> Result<Align, &'static str> {
262
262
Err ( "not an unsuffixed integer" )
263
263
}
264
264
}
265
+
266
+ /// Parse #[align(<integer>)].
267
+ #[ derive( Default ) ]
268
+ pub ( crate ) struct AlignParser ( Option < ( Align , Span ) > ) ;
269
+
270
+ impl AlignParser {
271
+ const PATH : & ' static [ Symbol ] = & [ sym:: align] ;
272
+
273
+ fn parse < ' c , S : Stage > (
274
+ & mut self ,
275
+ cx : & ' c mut AcceptContext < ' _ , ' _ , S > ,
276
+ args : & ' c ArgParser < ' _ > ,
277
+ ) {
278
+ // The builtin attributes parser already emits an error in this case.
279
+ let Some ( list) = args. list ( ) else { return } ;
280
+
281
+ let Some ( align) = list. single ( ) else {
282
+ cx. dcx ( )
283
+ . emit_err ( session_diagnostics:: IncorrectReprFormatAlignOneArg { span : list. span } ) ;
284
+
285
+ return ;
286
+ } ;
287
+
288
+ let Some ( lit) = align. lit ( ) else {
289
+ cx. emit_err ( session_diagnostics:: IncorrectReprFormatExpectInteger {
290
+ span : align. span ( ) ,
291
+ } ) ;
292
+
293
+ return ;
294
+ } ;
295
+
296
+ match parse_alignment ( & lit. kind ) {
297
+ Ok ( literal) => self . 0 = Ord :: max ( self . 0 , Some ( ( literal, cx. attr_span ) ) ) ,
298
+ Err ( message) => {
299
+ cx. emit_err ( session_diagnostics:: InvalidAlignmentValue {
300
+ span : lit. span ,
301
+ error_part : message,
302
+ } ) ;
303
+ }
304
+ }
305
+ }
306
+ }
307
+
308
+ impl < S : Stage > AttributeParser < S > for AlignParser {
309
+ const ATTRIBUTES : AcceptMapping < Self , S > = & [ ( Self :: PATH , Self :: parse) ] ;
310
+
311
+ fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
312
+ let ( align, span) = self . 0 ?;
313
+ Some ( AttributeKind :: Align { align, span } )
314
+ }
315
+ }
0 commit comments