|
1 | 1 | //! Parsing and validation of builtin attributes |
2 | 2 |
|
| 3 | +use rustc_abi::Align; |
3 | 4 | use rustc_ast::{self as ast, attr}; |
4 | 5 | use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId}; |
5 | 6 | use rustc_ast_pretty::pprust; |
@@ -919,10 +920,10 @@ pub enum ReprAttr { |
919 | 920 | ReprInt(IntType), |
920 | 921 | ReprRust, |
921 | 922 | ReprC, |
922 | | - ReprPacked(u32), |
| 923 | + ReprPacked(Align), |
923 | 924 | ReprSimd, |
924 | 925 | ReprTransparent, |
925 | | - ReprAlign(u32), |
| 926 | + ReprAlign(Align), |
926 | 927 | } |
927 | 928 |
|
928 | 929 | #[derive(Eq, PartialEq, Debug, Copy, Clone)] |
@@ -968,7 +969,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> { |
968 | 969 | let hint = match item.name_or_empty() { |
969 | 970 | sym::Rust => Some(ReprRust), |
970 | 971 | sym::C => Some(ReprC), |
971 | | - sym::packed => Some(ReprPacked(1)), |
| 972 | + sym::packed => Some(ReprPacked(Align::ONE)), |
972 | 973 | sym::simd => Some(ReprSimd), |
973 | 974 | sym::transparent => Some(ReprTransparent), |
974 | 975 | sym::align => { |
@@ -1209,11 +1210,17 @@ fn allow_unstable<'a>( |
1209 | 1210 | }) |
1210 | 1211 | } |
1211 | 1212 |
|
1212 | | -pub fn parse_alignment(node: &ast::LitKind) -> Result<u32, &'static str> { |
| 1213 | +pub fn parse_alignment(node: &ast::LitKind) -> Result<Align, &'static str> { |
1213 | 1214 | if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node { |
| 1215 | + // `Align::from_bytes` accepts 0 as an input, check is_power_of_two() first |
1214 | 1216 | if literal.get().is_power_of_two() { |
1215 | | - // rustc_middle::ty::layout::Align restricts align to <= 2^29 |
1216 | | - if *literal <= 1 << 29 { Ok(literal.get() as u32) } else { Err("larger than 2^29") } |
| 1217 | + // Only possible error is larger than 2^29 |
| 1218 | + literal |
| 1219 | + .get() |
| 1220 | + .try_into() |
| 1221 | + .ok() |
| 1222 | + .and_then(|v| Align::from_bytes(v).ok()) |
| 1223 | + .ok_or("larger than 2^29") |
1217 | 1224 | } else { |
1218 | 1225 | Err("not a power of two") |
1219 | 1226 | } |
|
0 commit comments