From 88fa5c6a45a533a78c698a22f4b16002a3bc9fc3 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 19 May 2019 13:59:44 +0300 Subject: [PATCH] Improve type size assertions Now they - Tell what the new size is, when it changes - Do not require passing an identifier --- src/librustc/hir/mod.rs | 2 +- src/librustc/middle/region.rs | 2 +- src/librustc/mir/interpret/pointer.rs | 2 +- src/librustc/mir/interpret/value.rs | 4 ++-- src/librustc/mir/mod.rs | 9 ++++----- src/librustc/mir/tcx.rs | 6 +++--- src/librustc/ty/mod.rs | 2 +- src/librustc/ty/sty.rs | 4 ++-- src/librustc_data_structures/macros.rs | 9 +++++++++ src/libsyntax/ast.rs | 4 ++-- src/libsyntax/parse/token.rs | 6 +++--- src/libsyntax/tokenstream.rs | 4 ++-- 12 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index f407be4e87b..3ca79cb8501 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -1356,7 +1356,7 @@ pub struct Expr { // `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::() == 72); +static_assert_size!(Expr, 72); impl Expr { pub fn precedence(&self) -> ExprPrecedence { diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index 8ef4e9ac8f4..37681ad7fcd 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -158,7 +158,7 @@ pub struct FirstStatementIndex { .. } impl_stable_hash_for!(struct crate::middle::region::FirstStatementIndex { private }); // compilation error if size of `ScopeData` is not the same as a `u32` -static_assert!(ASSERT_SCOPE_DATA: mem::size_of::() == 4); +static_assert_size!(ScopeData, 4); impl Scope { /// Returns a item-local ID associated with this scope. diff --git a/src/librustc/mir/interpret/pointer.rs b/src/librustc/mir/interpret/pointer.rs index 75e0f704a58..59b7891b90f 100644 --- a/src/librustc/mir/interpret/pointer.rs +++ b/src/librustc/mir/interpret/pointer.rs @@ -78,7 +78,7 @@ pub struct Pointer { pub tag: Tag, } -static_assert!(POINTER_SIZE: ::std::mem::size_of::() == 16); +static_assert_size!(Pointer, 16); /// Produces a `Pointer` which points to the beginning of the Allocation impl From for Pointer { diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 7e45568725f..551b86390db 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -54,7 +54,7 @@ pub enum ConstValue<'tcx> { } #[cfg(target_arch = "x86_64")] -static_assert!(CONST_SIZE: ::std::mem::size_of::>() == 40); +static_assert_size!(ConstValue<'_>, 40); impl<'tcx> ConstValue<'tcx> { #[inline] @@ -111,7 +111,7 @@ pub enum Scalar { } #[cfg(target_arch = "x86_64")] -static_assert!(SCALAR_SIZE: ::std::mem::size_of::() == 24); +static_assert_size!(Scalar, 24); impl fmt::Display for Scalar { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index bd67aabfe8e..dd43cb2f18e 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -1738,7 +1738,7 @@ pub struct Statement<'tcx> { // `Statement` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::>() == 56); +static_assert_size!(Statement<'_>, 56); impl<'tcx> Statement<'tcx> { /// Changes a statement to a nop. This is both faster than deleting instructions and avoids @@ -1997,10 +1997,9 @@ pub enum ProjectionElem { /// and the index is a local. pub type PlaceElem<'tcx> = ProjectionElem>; -// at least on 64 bit systems, `PlaceElem` should not be larger than two pointers -static_assert!(PROJECTION_ELEM_IS_2_PTRS_LARGE: - mem::size_of::>() <= 16 -); +// At least on 64 bit systems, `PlaceElem` should not be larger than two pointers. +#[cfg(target_arch = "x86_64")] +static_assert_size!(PlaceElem<'_>, 16); /// Alias for projections as they appear in `UserTypeProjection`, where we /// need neither the `V` parameter for `Index` nor the `T` for `Field`. diff --git a/src/librustc/mir/tcx.rs b/src/librustc/mir/tcx.rs index d3fa5e84b6a..5135aeb2392 100644 --- a/src/librustc/mir/tcx.rs +++ b/src/librustc/mir/tcx.rs @@ -17,9 +17,9 @@ pub struct PlaceTy<'tcx> { pub variant_index: Option, } -static_assert!(PLACE_TY_IS_3_PTRS_LARGE: - mem::size_of::>() <= 24 -); +// At least on 64 bit systems, `PlaceTy` should not be larger than two or three pointers. +#[cfg(target_arch = "x86_64")] +static_assert_size!(PlaceTy<'_>, 16); impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> { pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 2f71861d4dc..1e4bb37c44e 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -510,7 +510,7 @@ pub struct TyS<'tcx> { // `TyS` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_TY_S: ::std::mem::size_of::>() == 32); +static_assert_size!(TyS<'_>, 32); impl<'tcx> Ord for TyS<'tcx> { fn cmp(&self, other: &TyS<'tcx>) -> Ordering { diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 760f3d60d05..269064af93b 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -211,7 +211,7 @@ pub enum TyKind<'tcx> { // `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_TY_KIND: ::std::mem::size_of::>() == 24); +static_assert_size!(TyKind<'_>, 24); /// A closure can be modeled as a struct that looks like: /// @@ -2207,7 +2207,7 @@ pub struct Const<'tcx> { } #[cfg(target_arch = "x86_64")] -static_assert!(CONST_SIZE: ::std::mem::size_of::>() == 48); +static_assert_size!(Const<'_>, 48); impl<'tcx> Const<'tcx> { #[inline] diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs index 029e7267c82..7fc23999284 100644 --- a/src/librustc_data_structures/macros.rs +++ b/src/librustc_data_structures/macros.rs @@ -10,3 +10,12 @@ macro_rules! static_assert { static $name: () = [()][!($test: bool) as usize]; } } + +/// Type size assertion. The first argument is a type and the second argument is its expected size. +#[macro_export] +#[allow_internal_unstable(underscore_const_names)] +macro_rules! static_assert_size { + ($ty:ty, $size:expr) => { + const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()]; + } +} diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index d12240655e6..b55ca453fb3 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -16,7 +16,7 @@ use rustc_data_structures::indexed_vec::Idx; #[cfg(target_arch = "x86_64")] -use rustc_data_structures::static_assert; +use rustc_data_structures::static_assert_size; use rustc_target::spec::abi::Abi; use syntax_pos::{Span, DUMMY_SP}; @@ -964,7 +964,7 @@ pub struct Expr { // `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::() == 96); +static_assert_size!(Expr, 96); impl Expr { /// Whether this expression would be valid somewhere that expects a value; for example, an `if` diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 049fb6cb78b..068fc41c87a 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -19,7 +19,7 @@ use std::fmt; use std::mem; #[cfg(target_arch = "x86_64")] -use rustc_data_structures::static_assert; +use rustc_data_structures::static_assert_size; use rustc_data_structures::sync::Lrc; #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] @@ -74,7 +74,7 @@ pub enum Lit { } #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_LIT: mem::size_of::() == 8); +static_assert_size!(Lit, 8); impl Lit { crate fn literal_name(&self) -> &'static str { @@ -220,7 +220,7 @@ pub enum Token { // `Token` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::() == 16); +static_assert_size!(Token, 16); impl Token { /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary. diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 93b5ecadd14..3cb16c30a50 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -21,7 +21,7 @@ use syntax_pos::{BytePos, Mark, Span, DUMMY_SP}; #[cfg(target_arch = "x86_64")] -use rustc_data_structures::static_assert; +use rustc_data_structures::static_assert_size; use rustc_data_structures::sync::Lrc; use serialize::{Decoder, Decodable, Encoder, Encodable}; use smallvec::{SmallVec, smallvec}; @@ -158,7 +158,7 @@ pub fn close_tt(span: Span, delim: DelimToken) -> TokenTree { // `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(target_arch = "x86_64")] -static_assert!(MEM_SIZE_OF_TOKEN_STREAM: mem::size_of::() == 8); +static_assert_size!(TokenStream, 8); #[derive(Clone, Copy, Debug, PartialEq)] pub enum IsJoint { -- 2.44.0