From 9037ebba0c243e5415879a2ef20736b71a453fa6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sun, 31 Jul 2022 06:57:53 +1000 Subject: [PATCH] Improve size assertions. - For any file with four or more size assertions, move them into a separate module (as is already done for `hir.rs`). - Add some more for AST nodes and THIR nodes. - Put the `hir.rs` ones in alphabetical order. --- compiler/rustc_ast/src/ast.rs | 37 ++++++++----- .../rustc_const_eval/src/interpret/operand.rs | 23 ++++---- .../rustc_const_eval/src/interpret/place.rs | 27 ++++----- compiler/rustc_hir/src/hir.rs | 27 ++++----- compiler/rustc_middle/src/mir/syntax.rs | 29 ++++------ compiler/rustc_middle/src/thir.rs | 15 +++-- src/librustdoc/clean/types.rs | 55 ++++++------------- 7 files changed, 100 insertions(+), 113 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index ac2328a5824..116497109f1 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1111,10 +1111,6 @@ pub struct Expr { pub tokens: Option, } -// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Expr, 104); - impl Expr { /// Returns `true` if this expression would be valid somewhere that expects a value; /// for example, an `if` condition. @@ -2883,9 +2879,6 @@ pub enum ItemKind { MacroDef(MacroDef), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(ItemKind, 112); - impl ItemKind { pub fn article(&self) -> &str { use ItemKind::*; @@ -2957,9 +2950,6 @@ pub enum AssocItemKind { MacCall(MacCall), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(AssocItemKind, 72); - impl AssocItemKind { pub fn defaultness(&self) -> Defaultness { match *self { @@ -3009,9 +2999,6 @@ pub enum ForeignItemKind { MacCall(MacCall), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(ForeignItemKind, 72); - impl From for ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { match foreign_item_kind { @@ -3038,3 +3025,27 @@ impl TryFrom for ForeignItemKind { } pub type ForeignItem = Item; + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(AssocItemKind, 72); + rustc_data_structures::static_assert_size!(Attribute, 152); + rustc_data_structures::static_assert_size!(Block, 48); + rustc_data_structures::static_assert_size!(Expr, 104); + rustc_data_structures::static_assert_size!(Fn, 192); + rustc_data_structures::static_assert_size!(ForeignItemKind, 72); + rustc_data_structures::static_assert_size!(GenericBound, 88); + rustc_data_structures::static_assert_size!(Generics, 72); + rustc_data_structures::static_assert_size!(Impl, 200); + rustc_data_structures::static_assert_size!(Item, 200); + rustc_data_structures::static_assert_size!(ItemKind, 112); + rustc_data_structures::static_assert_size!(Lit, 48); + rustc_data_structures::static_assert_size!(Pat, 120); + rustc_data_structures::static_assert_size!(Path, 40); + rustc_data_structures::static_assert_size!(PathSegment, 24); + rustc_data_structures::static_assert_size!(Stmt, 32); + rustc_data_structures::static_assert_size!(Ty, 96); +} diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index de284bd3bae..94ba62c160c 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -37,9 +37,6 @@ pub enum Immediate { Uninit, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Immediate, 56); - impl From> for Immediate { #[inline(always)] fn from(val: ScalarMaybeUninit) -> Self { @@ -117,9 +114,6 @@ pub struct ImmTy<'tcx, Prov: Provenance = AllocId> { pub layout: TyAndLayout<'tcx>, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(ImmTy<'_>, 72); - impl std::fmt::Display for ImmTy<'_, Prov> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { /// Helper function for printing a scalar to a FmtPrinter @@ -187,9 +181,6 @@ pub enum Operand { Indirect(MemPlace), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Operand, 64); - #[derive(Clone, Debug)] pub struct OpTy<'tcx, Prov: Provenance = AllocId> { op: Operand, // Keep this private; it helps enforce invariants. @@ -204,9 +195,6 @@ pub struct OpTy<'tcx, Prov: Provenance = AllocId> { pub align: Option, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(OpTy<'_>, 88); - impl<'tcx, Prov: Provenance> std::ops::Deref for OpTy<'tcx, Prov> { type Target = Operand; #[inline(always)] @@ -830,3 +818,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }) } } + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(Immediate, 56); + rustc_data_structures::static_assert_size!(ImmTy<'_>, 72); + rustc_data_structures::static_assert_size!(Operand, 64); + rustc_data_structures::static_assert_size!(OpTy<'_>, 88); +} diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 473da71a0ab..f4571a1ca3d 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -25,9 +25,6 @@ pub enum MemPlaceMeta { None, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(MemPlaceMeta, 24); - impl MemPlaceMeta { pub fn unwrap_meta(self) -> Scalar { match self { @@ -56,9 +53,6 @@ pub struct MemPlace { pub meta: MemPlaceMeta, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(MemPlace, 40); - /// A MemPlace with its layout. Constructing it is only possible in this module. #[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)] pub struct MPlaceTy<'tcx, Prov: Provenance = AllocId> { @@ -71,9 +65,6 @@ pub struct MPlaceTy<'tcx, Prov: Provenance = AllocId> { pub align: Align, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64); - #[derive(Copy, Clone, Debug)] pub enum Place { /// A place referring to a value allocated in the `Memory` system. @@ -84,9 +75,6 @@ pub enum Place { Local { frame: usize, local: mir::Local }, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Place, 48); - #[derive(Clone, Debug)] pub struct PlaceTy<'tcx, Prov: Provenance = AllocId> { place: Place, // Keep this private; it helps enforce invariants. @@ -98,9 +86,6 @@ pub struct PlaceTy<'tcx, Prov: Provenance = AllocId> { pub align: Align, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(PlaceTy<'_>, 72); - impl<'tcx, Prov: Provenance> std::ops::Deref for PlaceTy<'tcx, Prov> { type Target = Place; #[inline(always)] @@ -901,3 +886,15 @@ where Ok(mplace) } } + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(MemPlaceMeta, 24); + rustc_data_structures::static_assert_size!(MemPlace, 40); + rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64); + rustc_data_structures::static_assert_size!(Place, 48); + rustc_data_structures::static_assert_size!(PlaceTy<'_>, 72); +} diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index f71400898e6..7230555e961 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3489,17 +3489,18 @@ impl<'hir> Node<'hir> { // Some nodes are used a lot. Make sure they don't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { - rustc_data_structures::static_assert_size!(super::Block<'static>, 48); - rustc_data_structures::static_assert_size!(super::Expr<'static>, 56); - rustc_data_structures::static_assert_size!(super::Pat<'static>, 88); - rustc_data_structures::static_assert_size!(super::QPath<'static>, 24); - rustc_data_structures::static_assert_size!(super::Ty<'static>, 72); - rustc_data_structures::static_assert_size!(super::GenericBound<'_>, 48); - rustc_data_structures::static_assert_size!(super::Generics<'static>, 56); - rustc_data_structures::static_assert_size!(super::Impl<'static>, 80); - - rustc_data_structures::static_assert_size!(super::Item<'static>, 80); - rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 88); - rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 80); - rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 72); + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(Block<'static>, 48); + rustc_data_structures::static_assert_size!(Expr<'static>, 56); + rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72); + rustc_data_structures::static_assert_size!(GenericBound<'_>, 48); + rustc_data_structures::static_assert_size!(Generics<'static>, 56); + rustc_data_structures::static_assert_size!(ImplItem<'static>, 80); + rustc_data_structures::static_assert_size!(Impl<'static>, 80); + rustc_data_structures::static_assert_size!(Item<'static>, 80); + rustc_data_structures::static_assert_size!(Pat<'static>, 88); + rustc_data_structures::static_assert_size!(QPath<'static>, 24); + rustc_data_structures::static_assert_size!(TraitItem<'static>, 88); + rustc_data_structures::static_assert_size!(Ty<'static>, 72); } diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 510316c778b..edc072aac8e 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -796,9 +796,6 @@ pub struct Place<'tcx> { pub projection: &'tcx List>, } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(Place<'_>, 16); - #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable)] pub enum ProjectionElem { @@ -862,11 +859,6 @@ pub enum ProjectionElem { /// and the index is a local. pub type PlaceElem<'tcx> = ProjectionElem>; -// This type is fairly frequently used, so we shouldn't unintentionally increase -// its size. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(PlaceElem<'_>, 24); - /////////////////////////////////////////////////////////////////////////// // Operands @@ -909,9 +901,6 @@ pub enum Operand<'tcx> { Constant(Box>), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(Operand<'_>, 24); - /////////////////////////////////////////////////////////////////////////// // Rvalues @@ -1063,9 +1052,6 @@ pub enum Rvalue<'tcx> { CopyForDeref(Place<'tcx>), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(Rvalue<'_>, 40); - #[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)] pub enum CastKind { /// An exposing pointer to address cast. A cast between a pointer and an integer type, or @@ -1099,9 +1085,6 @@ pub enum AggregateKind<'tcx> { Generator(DefId, SubstsRef<'tcx>, hir::Movability), } -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(AggregateKind<'_>, 48); - #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)] pub enum NullOp { /// Returns the size of a value of that type @@ -1165,3 +1148,15 @@ pub enum BinOp { /// The `ptr.offset` operator Offset, } + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + static_assert_size!(AggregateKind<'_>, 48); + static_assert_size!(Operand<'_>, 24); + static_assert_size!(Place<'_>, 16); + static_assert_size!(PlaceElem<'_>, 24); + static_assert_size!(Rvalue<'_>, 40); +} diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index 36db8d04918..617ec078dcb 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -189,10 +189,6 @@ pub enum StmtKind<'tcx> { }, } -// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Expr<'_>, 104); - #[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)] #[derive(TypeFoldable, TypeVisitable)] pub struct LocalVarId(pub hir::HirId); @@ -811,3 +807,14 @@ impl<'tcx> fmt::Display for Pat<'tcx> { } } } + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(Block, 56); + rustc_data_structures::static_assert_size!(Expr<'_>, 104); + rustc_data_structures::static_assert_size!(Pat<'_>, 24); + rustc_data_structures::static_assert_size!(Stmt<'_>, 120); +} diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index d022ce9696a..1a46d077f1b 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -122,10 +122,6 @@ pub(crate) struct Crate { pub(crate) external_traits: Rc>>, } -// `Crate` is frequently moved by-value. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Crate, 72); - impl Crate { pub(crate) fn name(&self, tcx: TyCtxt<'_>) -> Symbol { ExternalCrate::LOCAL.name(tcx) @@ -389,10 +385,6 @@ impl fmt::Debug for Item { } } -// `Item` is used a lot. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Item, 56); - pub(crate) fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span { Span::new(def_id.as_local().map_or_else( || tcx.def_span(def_id), @@ -771,10 +763,6 @@ pub(crate) enum ItemKind { KeywordItem, } -// `ItemKind` is an enum and large variants can bloat up memory usage even for smaller ones -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(ItemKind, 112); - impl ItemKind { /// Some items contain others such as structs (for their fields) and Enums /// (for their variants). This method returns those contained items. @@ -994,10 +982,6 @@ pub(crate) struct DocFragment { pub(crate) indent: usize, } -// `DocFragment` is used a lot. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(DocFragment, 32); - #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) enum DocFragmentKind { /// A doc fragment created from a `///` or `//!` doc comment. @@ -1382,10 +1366,6 @@ pub(crate) struct GenericParamDef { pub(crate) kind: GenericParamDefKind, } -// `GenericParamDef` is used in many places. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(GenericParamDef, 56); - impl GenericParamDef { pub(crate) fn is_synthetic_type_param(&self) -> bool { match self.kind { @@ -1590,10 +1570,6 @@ pub(crate) enum Type { ImplTrait(Vec), } -// `Type` is used a lot. Make sure it doesn't unintentionally get bigger. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Type, 72); - impl Type { /// When comparing types for equality, it can help to ignore `&` wrapping. pub(crate) fn without_borrowed_ref(&self) -> &Type { @@ -2230,33 +2206,18 @@ pub(crate) enum GenericArg { Infer, } -// `GenericArg` can occur many times in a single `Path`, so make sure it -// doesn't increase in size unexpectedly. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(GenericArg, 80); - #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub(crate) enum GenericArgs { AngleBracketed { args: Box<[GenericArg]>, bindings: ThinVec }, Parenthesized { inputs: Box<[Type]>, output: Option> }, } -// `GenericArgs` is in every `PathSegment`, so its size can significantly -// affect rustdoc's memory usage. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(GenericArgs, 32); - #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub(crate) struct PathSegment { pub(crate) name: Symbol, pub(crate) args: GenericArgs, } -// `PathSegment` usually occurs multiple times in every `Path`, so its size can -// significantly affect rustdoc's memory usage. -#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(PathSegment, 40); - #[derive(Clone, Debug)] pub(crate) struct Typedef { pub(crate) type_: Type, @@ -2527,3 +2488,19 @@ impl SubstParam { if let Self::Lifetime(lt) = self { Some(lt) } else { None } } } + +// Some nodes are used a lot. Make sure they don't unintentionally get bigger. +#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] +mod size_asserts { + use super::*; + // These are in alphabetical order, which is easy to maintain. + rustc_data_structures::static_assert_size!(Crate, 72); // frequently moved by-value + rustc_data_structures::static_assert_size!(DocFragment, 32); + rustc_data_structures::static_assert_size!(GenericArg, 80); + rustc_data_structures::static_assert_size!(GenericArgs, 32); + rustc_data_structures::static_assert_size!(GenericParamDef, 56); + rustc_data_structures::static_assert_size!(Item, 56); + rustc_data_structures::static_assert_size!(ItemKind, 112); + rustc_data_structures::static_assert_size!(PathSegment, 40); + rustc_data_structures::static_assert_size!(Type, 72); +}