Skip to content

Commit 8e28729

Browse files
committed
Add doc-comments for NaiveLayout
1 parent feb20f2 commit 8e28729

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,11 @@ rustc_queries! {
13941394
desc { "computing layout of `{}`", key.value }
13951395
}
13961396

1397-
/// Computes the naive layout estimate of a type. Note that this implicitly
1397+
/// Computes the naive layout approximation of a type. Note that this implicitly
13981398
/// executes in "reveal all" mode, and will normalize the input type.
13991399
///
1400-
/// Unlike `layout_of`, this doesn't recurse behind reference types.
1400+
/// Unlike `layout_of`, this doesn't look past references (beyond the `Pointee::Metadata`
1401+
/// projection), and as such can be called on generic types like `Option<&T>`.
14011402
query naive_layout_of(
14021403
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>
14031404
) -> Result<ty::layout::TyAndNaiveLayout<'tcx>, &'tcx ty::layout::LayoutError<'tcx>> {

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,15 +650,16 @@ impl std::ops::DerefMut for TyAndNaiveLayout<'_> {
650650
}
651651
}
652652

653-
/// Extremely simplified representation of a type's layout.
654-
///
655-
///
653+
/// Extremely simplified approximation of a type's layout returned by the
654+
/// `naive_layout_of` query.
656655
#[derive(Copy, Clone, Debug, HashStable)]
657656
pub struct NaiveLayout {
658657
pub abi: NaiveAbi,
658+
/// An underestimate of the layout's size.
659659
pub size: Size,
660+
/// An underestimate of the layout's required alignment.
660661
pub align: Align,
661-
/// If `true`, `size` and `align` are exact.
662+
/// If `true`, `size` and `align` must be exact values.
662663
pub exact: bool,
663664
}
664665

@@ -670,23 +671,28 @@ pub enum NaiveAbi {
670671
Uninhabited,
671672
/// An unsized aggregate. (needed to properly track `Scalar`)
672673
Unsized,
673-
Any,
674+
/// Any other sized layout.
675+
Sized,
674676
}
675677

676678
impl NaiveAbi {
677679
#[inline]
678680
pub fn as_aggregate(self) -> Self {
679681
match self {
680-
NaiveAbi::Scalar(_) => NaiveAbi::Any,
682+
NaiveAbi::Scalar(_) => NaiveAbi::Sized,
681683
_ => self,
682684
}
683685
}
684686
}
685687

686688
impl NaiveLayout {
689+
/// The layout of an empty aggregate, e.g. `()`.
687690
pub const EMPTY: Self =
688-
Self { size: Size::ZERO, align: Align::ONE, exact: true, abi: NaiveAbi::Any };
691+
Self { size: Size::ZERO, align: Align::ONE, exact: true, abi: NaiveAbi::Sized };
689692

693+
/// Returns whether `self` is a valid approximation of the given full `layout`.
694+
///
695+
/// This should always return `true` when both layouts are computed from the same type.
690696
pub fn is_refined_by(&self, layout: Layout<'_>) -> bool {
691697
if self.size > layout.size() || self.align > layout.align().abi {
692698
return false;
@@ -712,11 +718,12 @@ impl NaiveLayout {
712718
Some(self.size == dl.pointer_size && self.align == dl.pointer_align.abi)
713719
}
714720
NaiveAbi::Uninhabited | NaiveAbi::Unsized => Some(false),
715-
NaiveAbi::Any if self.exact => Some(false),
716-
NaiveAbi::Any => None,
721+
NaiveAbi::Sized if self.exact => Some(false),
722+
NaiveAbi::Sized => None,
717723
}
718724
}
719725

726+
/// Artificially lowers the alignment of this layout.
720727
#[must_use]
721728
#[inline]
722729
pub fn packed(mut self, align: Align) -> Self {
@@ -727,6 +734,7 @@ impl NaiveLayout {
727734
self
728735
}
729736

737+
/// Artificially raises the alignment of this layout.
730738
#[must_use]
731739
#[inline]
732740
pub fn align_to(mut self, align: Align) -> Self {
@@ -737,6 +745,7 @@ impl NaiveLayout {
737745
self
738746
}
739747

748+
/// Pads this layout so that its size is a multiple of `align`.
740749
#[must_use]
741750
#[inline]
742751
pub fn pad_to_align(mut self, align: Align) -> Self {
@@ -748,6 +757,8 @@ impl NaiveLayout {
748757
self
749758
}
750759

760+
/// Returns the layout of `self` immediately followed by `other`, without any
761+
/// padding between them, as in a packed `struct` or tuple.
751762
#[must_use]
752763
#[inline]
753764
pub fn concat(&self, other: &Self, dl: &TargetDataLayout) -> Option<Self> {
@@ -764,11 +775,13 @@ impl NaiveLayout {
764775
(_, s @ Scalar(_)) if exact && self.size == Size::ZERO => s,
765776
(s @ Scalar(_), _) if exact && other.size == Size::ZERO => s,
766777
// Default case.
767-
(_, _) => Any,
778+
(_, _) => Sized,
768779
};
769780
Some(Self { abi, size, align, exact })
770781
}
771782

783+
/// Returns the layout of `self` superposed with `other`, as in an `enum`
784+
/// or an `union`.
772785
#[must_use]
773786
#[inline]
774787
pub fn union(&self, other: &Self) -> Self {
@@ -787,7 +800,7 @@ impl NaiveLayout {
787800
(Scalar(s1), Scalar(s2)) if s1 == s2 => Scalar(s1),
788801
// Default cases.
789802
(Uninhabited, Uninhabited) => Uninhabited,
790-
(_, _) => Any,
803+
(_, _) => Sized,
791804
};
792805
Self { abi, size, align, exact }
793806
}
@@ -849,7 +862,8 @@ pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {
849862
/// Computes the naive layout estimate of a type. Note that this implicitly
850863
/// executes in "reveal all" mode, and will normalize the input type.
851864
///
852-
/// Unlike `layout_of`, this doesn't recurse behind reference types.
865+
/// Unlike `layout_of`, this doesn't look past references (beyond the `Pointee::Metadata`
866+
/// projection), and as such can be called on generic types like `Option<&T>`.
853867
#[inline]
854868
fn naive_layout_of(
855869
&self,

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn naive_layout_of_uncached<'tcx>(
244244

245245
// Unions are always inhabited, and never scalar if `repr(C)`.
246246
if !matches!(layout.abi, NaiveAbi::Scalar(_)) || repr.inhibit_enum_layout_opt() {
247-
layout.abi = NaiveAbi::Any;
247+
layout.abi = NaiveAbi::Sized;
248248
}
249249

250250
if let Some(align) = repr.align {

0 commit comments

Comments
 (0)