@@ -650,15 +650,16 @@ impl std::ops::DerefMut for TyAndNaiveLayout<'_> {
650
650
}
651
651
}
652
652
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.
656
655
#[ derive( Copy , Clone , Debug , HashStable ) ]
657
656
pub struct NaiveLayout {
658
657
pub abi : NaiveAbi ,
658
+ /// An underestimate of the layout's size.
659
659
pub size : Size ,
660
+ /// An underestimate of the layout's required alignment.
660
661
pub align : Align ,
661
- /// If `true`, `size` and `align` are exact.
662
+ /// If `true`, `size` and `align` must be exact values .
662
663
pub exact : bool ,
663
664
}
664
665
@@ -670,23 +671,28 @@ pub enum NaiveAbi {
670
671
Uninhabited ,
671
672
/// An unsized aggregate. (needed to properly track `Scalar`)
672
673
Unsized ,
673
- Any ,
674
+ /// Any other sized layout.
675
+ Sized ,
674
676
}
675
677
676
678
impl NaiveAbi {
677
679
#[ inline]
678
680
pub fn as_aggregate ( self ) -> Self {
679
681
match self {
680
- NaiveAbi :: Scalar ( _) => NaiveAbi :: Any ,
682
+ NaiveAbi :: Scalar ( _) => NaiveAbi :: Sized ,
681
683
_ => self ,
682
684
}
683
685
}
684
686
}
685
687
686
688
impl NaiveLayout {
689
+ /// The layout of an empty aggregate, e.g. `()`.
687
690
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 } ;
689
692
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.
690
696
pub fn is_refined_by ( & self , layout : Layout < ' _ > ) -> bool {
691
697
if self . size > layout. size ( ) || self . align > layout. align ( ) . abi {
692
698
return false ;
@@ -712,11 +718,12 @@ impl NaiveLayout {
712
718
Some ( self . size == dl. pointer_size && self . align == dl. pointer_align . abi )
713
719
}
714
720
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 ,
717
723
}
718
724
}
719
725
726
+ /// Artificially lowers the alignment of this layout.
720
727
#[ must_use]
721
728
#[ inline]
722
729
pub fn packed ( mut self , align : Align ) -> Self {
@@ -727,6 +734,7 @@ impl NaiveLayout {
727
734
self
728
735
}
729
736
737
+ /// Artificially raises the alignment of this layout.
730
738
#[ must_use]
731
739
#[ inline]
732
740
pub fn align_to ( mut self , align : Align ) -> Self {
@@ -737,6 +745,7 @@ impl NaiveLayout {
737
745
self
738
746
}
739
747
748
+ /// Pads this layout so that its size is a multiple of `align`.
740
749
#[ must_use]
741
750
#[ inline]
742
751
pub fn pad_to_align ( mut self , align : Align ) -> Self {
@@ -748,6 +757,8 @@ impl NaiveLayout {
748
757
self
749
758
}
750
759
760
+ /// Returns the layout of `self` immediately followed by `other`, without any
761
+ /// padding between them, as in a packed `struct` or tuple.
751
762
#[ must_use]
752
763
#[ inline]
753
764
pub fn concat ( & self , other : & Self , dl : & TargetDataLayout ) -> Option < Self > {
@@ -764,11 +775,13 @@ impl NaiveLayout {
764
775
( _, s @ Scalar ( _) ) if exact && self . size == Size :: ZERO => s,
765
776
( s @ Scalar ( _) , _) if exact && other. size == Size :: ZERO => s,
766
777
// Default case.
767
- ( _, _) => Any ,
778
+ ( _, _) => Sized ,
768
779
} ;
769
780
Some ( Self { abi, size, align, exact } )
770
781
}
771
782
783
+ /// Returns the layout of `self` superposed with `other`, as in an `enum`
784
+ /// or an `union`.
772
785
#[ must_use]
773
786
#[ inline]
774
787
pub fn union ( & self , other : & Self ) -> Self {
@@ -787,7 +800,7 @@ impl NaiveLayout {
787
800
( Scalar ( s1) , Scalar ( s2) ) if s1 == s2 => Scalar ( s1) ,
788
801
// Default cases.
789
802
( Uninhabited , Uninhabited ) => Uninhabited ,
790
- ( _, _) => Any ,
803
+ ( _, _) => Sized ,
791
804
} ;
792
805
Self { abi, size, align, exact }
793
806
}
@@ -849,7 +862,8 @@ pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {
849
862
/// Computes the naive layout estimate of a type. Note that this implicitly
850
863
/// executes in "reveal all" mode, and will normalize the input type.
851
864
///
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>`.
853
867
#[ inline]
854
868
fn naive_layout_of (
855
869
& self ,
0 commit comments