Skip to content

rustc: Improve type size assertions #60959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Expr>() == 72);
static_assert_size!(Expr, 72);

impl Expr {
pub fn precedence(&self) -> ExprPrecedence {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ newtype_index! {
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::<ScopeData>() == 4);
static_assert_size!(ScopeData, 4);

impl Scope {
/// Returns a item-local ID associated with this scope.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct Pointer<Tag=(),Id=AllocId> {
pub tag: Tag,
}

static_assert!(POINTER_SIZE: ::std::mem::size_of::<Pointer>() == 16);
static_assert_size!(Pointer, 16);

/// Produces a `Pointer` which points to the beginning of the Allocation
impl From<AllocId> for Pointer {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum ConstValue<'tcx> {
}

#[cfg(target_arch = "x86_64")]
static_assert!(CONST_SIZE: ::std::mem::size_of::<ConstValue<'static>>() == 40);
static_assert_size!(ConstValue<'_>, 40);

impl<'tcx> ConstValue<'tcx> {
#[inline]
Expand Down Expand Up @@ -111,7 +111,7 @@ pub enum Scalar<Tag=(), Id=AllocId> {
}

#[cfg(target_arch = "x86_64")]
static_assert!(SCALAR_SIZE: ::std::mem::size_of::<Scalar>() == 24);
static_assert_size!(Scalar, 24);

impl<Tag> fmt::Display for Scalar<Tag> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Statement<'_>>() == 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
Expand Down Expand Up @@ -1997,10 +1997,9 @@ pub type PlaceProjection<'tcx> = Projection<Place<'tcx>, Local, Ty<'tcx>>;
/// and the index is a local.
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;

// 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::<PlaceElem<'_>>() <= 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`.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct PlaceTy<'tcx> {
pub variant_index: Option<VariantIdx>,
}

static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
mem::size_of::<PlaceTy<'_>>() <= 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> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TyS<'_>>() == 32);
static_assert_size!(TyS<'_>, 32);

impl<'tcx> Ord for TyS<'tcx> {
fn cmp(&self, other: &TyS<'tcx>) -> Ordering {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TyKind<'_>>() == 24);
static_assert_size!(TyKind<'_>, 24);

/// A closure can be modeled as a struct that looks like:
///
Expand Down Expand Up @@ -2207,7 +2207,7 @@ pub struct Const<'tcx> {
}

#[cfg(target_arch = "x86_64")]
static_assert!(CONST_SIZE: ::std::mem::size_of::<Const<'static>>() == 48);
static_assert_size!(Const<'_>, 48);

impl<'tcx> Const<'tcx> {
#[inline]
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_data_structures/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()];
}
}
4 changes: 2 additions & 2 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::ThinVec;

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};

Expand Down Expand Up @@ -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::<Expr>() == 96);
static_assert_size!(Expr, 96);

impl Expr {
/// Whether this expression would be valid somewhere that expects a value; for example, an `if`
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use log::info;
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)]
Expand Down Expand Up @@ -74,7 +74,7 @@ pub enum Lit {
}

#[cfg(target_arch = "x86_64")]
static_assert!(MEM_SIZE_OF_LIT: mem::size_of::<Lit>() == 8);
static_assert_size!(Lit, 8);

impl Lit {
crate fn literal_name(&self) -> &'static str {
Expand Down Expand Up @@ -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::<Token>() == 16);
static_assert_size!(Token, 16);

impl Token {
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::print::pprust;

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};
Expand Down Expand Up @@ -158,7 +158,7 @@ pub type TreeAndJoint = (TokenTree, IsJoint);

// `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::<TokenStream>() == 8);
static_assert_size!(TokenStream, 8);

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum IsJoint {
Expand Down