@@ -34,6 +34,7 @@ use rustc_session::lint::builtin::{
3434use rustc_session:: parse:: feature_err;
3535use rustc_span:: symbol:: { Symbol , kw, sym} ;
3636use rustc_span:: { BytePos , DUMMY_SP , Span } ;
37+ use rustc_target:: abi:: Size ;
3738use rustc_target:: spec:: abi:: Abi ;
3839use rustc_trait_selection:: error_reporting:: InferCtxtErrorExt ;
3940use rustc_trait_selection:: infer:: { TyCtxtInferExt , ValuePairs } ;
@@ -1785,7 +1786,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17851786 | Target :: Union
17861787 | Target :: Enum
17871788 | Target :: Fn
1788- | Target :: Method ( _) => continue ,
1789+ | Target :: Method ( _) => { }
17891790 _ => {
17901791 self . dcx ( ) . emit_err (
17911792 errors:: AttrApplication :: StructEnumFunctionMethodUnion {
@@ -1795,6 +1796,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
17951796 ) ;
17961797 }
17971798 }
1799+
1800+ self . check_align_value ( hint) ;
17981801 }
17991802 sym:: packed => {
18001803 if target != Target :: Struct && target != Target :: Union {
@@ -1892,6 +1895,45 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18921895 }
18931896 }
18941897
1898+ fn check_align_value ( & self , item : & MetaItemInner ) {
1899+ match item. singleton_lit_list ( ) {
1900+ Some ( (
1901+ _,
1902+ MetaItemLit {
1903+ kind : ast:: LitKind :: Int ( literal, ast:: LitIntType :: Unsuffixed ) , ..
1904+ } ,
1905+ ) ) => {
1906+ let val = literal. get ( ) as u64 ;
1907+ if val > 2_u64 . pow ( 29 ) {
1908+ // for values greater than 2^29, a different error will be emitted, make sure that happens
1909+ self . dcx ( ) . span_delayed_bug (
1910+ item. span ( ) ,
1911+ "alignment greater than 2^29 should be errored on elsewhere" ,
1912+ ) ;
1913+ } else {
1914+ // only do this check when <= 2^29 to prevent duplicate errors:
1915+ // alignment greater than 2^29 not supported
1916+ // alignment is too large for the current target
1917+
1918+ let max =
1919+ Size :: from_bits ( self . tcx . sess . target . pointer_width ) . signed_int_max ( ) as u64 ;
1920+ if val > max {
1921+ self . dcx ( ) . emit_err ( errors:: InvalidReprAlignForTarget {
1922+ span : item. span ( ) ,
1923+ size : max,
1924+ } ) ;
1925+ }
1926+ }
1927+ }
1928+
1929+ // if the attribute is malformed, singleton_lit_list may not be of the expected type or may be None
1930+ // but an error will have already been emitted, so this code should just skip such attributes
1931+ Some ( ( _, _) ) | None => {
1932+ self . dcx ( ) . span_delayed_bug ( item. span ( ) , "malformed repr(align(N))" ) ;
1933+ }
1934+ }
1935+ }
1936+
18951937 fn check_used ( & self , attrs : & [ Attribute ] , target : Target , target_span : Span ) {
18961938 let mut used_linker_span = None ;
18971939 let mut used_compiler_span = None ;
0 commit comments