Skip to content

Commit a4deaf5

Browse files
compiler: BackendRepr::inherent_align -> required_align
Change inherent_align's name to reflect its purpose and simplify returns from Option<AbiAndPrefAlign> to Option<Align> because every site was mapping away the "preferred" alignment anyways.
1 parent 8cf4c76 commit a4deaf5

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
377377
let abi = match common_non_zst_abi_and_align {
378378
Err(AbiMismatch) | Ok(None) => BackendRepr::Memory { sized: true },
379379
Ok(Some((abi, _))) => {
380-
if abi.inherent_align(dl).map(|a| a.abi) != Some(align.abi) {
380+
if abi.required_align(dl) != Some(align.abi) {
381381
// Mismatched alignment (e.g. union is #[repr(packed)]): disable opt
382382
BackendRepr::Memory { sized: true }
383383
} else {

compiler/rustc_abi/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,18 @@ impl BackendRepr {
14621462
matches!(*self, BackendRepr::Scalar(s) if s.is_bool())
14631463
}
14641464

1465-
/// Returns the fixed alignment of this ABI, if any is mandated.
1466-
pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAndPrefAlign> {
1467-
Some(match *self {
1468-
BackendRepr::Scalar(s) => s.align(cx),
1469-
BackendRepr::ScalarPair(s1, s2) => s1.align(cx).max(s2.align(cx)),
1465+
/// fixed alignment of this repr, if any is mandated
1466+
///
1467+
/// NOTE: returning None is not an error! not all types have set alignments
1468+
pub fn required_align<C: HasDataLayout>(&self, cx: &C) -> Option<Align> {
1469+
match *self {
1470+
BackendRepr::Scalar(s) => Some(s.align(cx).abi),
1471+
BackendRepr::ScalarPair(s1, s2) => Some(s1.align(cx).max(s2.align(cx)).abi),
14701472
BackendRepr::Vector { element, count } => {
1471-
cx.data_layout().vector_align(element.size(cx) * count)
1473+
Some(cx.data_layout().vector_align(element.size(cx) * count).abi)
14721474
}
1473-
BackendRepr::Uninhabited | BackendRepr::Memory { .. } => return None,
1474-
})
1475+
BackendRepr::Uninhabited | BackendRepr::Memory { .. } => None,
1476+
}
14751477
}
14761478

14771479
/// Returns the fixed size of this ABI, if any is mandated.
@@ -1484,12 +1486,12 @@ impl BackendRepr {
14841486
BackendRepr::ScalarPair(s1, s2) => {
14851487
// May have some padding between the pair.
14861488
let field2_offset = s1.size(cx).align_to(s2.align(cx).abi);
1487-
(field2_offset + s2.size(cx)).align_to(self.inherent_align(cx)?.abi)
1489+
(field2_offset + s2.size(cx)).align_to(self.required_align(cx)?)
14881490
}
14891491
BackendRepr::Vector { element, count } => {
14901492
// No padding in vectors, except possibly for trailing padding
14911493
// to make the size a multiple of align (e.g. for vectors of size 3).
1492-
(element.size(cx) * count).align_to(self.inherent_align(cx)?.abi)
1494+
(element.size(cx) * count).align_to(self.required_align(cx)?)
14931495
}
14941496
BackendRepr::Uninhabited | BackendRepr::Memory { .. } => return None,
14951497
})

compiler/rustc_ty_utils/src/layout/invariant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou
6666

6767
fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
6868
// Verify the ABI mandated alignment and size.
69-
let align = layout.backend_repr.inherent_align(cx).map(|align| align.abi);
69+
let align = layout.backend_repr.required_align(cx);
7070
let size = layout.backend_repr.inherent_size(cx);
7171
let Some((align, size)) = align.zip(size) else {
7272
assert_matches!(

0 commit comments

Comments
 (0)