Skip to content

Commit 581cf70

Browse files
authored
Rollup merge of #60959 - petrochenkov:sassert, r=estebank
rustc: Improve type size assertions Now they - Tell what the new size is, when it changes - Do not require passing an identifier ``` ::: src\libsyntax\parse\token.rs:223:1 | 223 | static_assert_size!(Token, 123); | -------------------------------- in this macro invocation | = note: expected type `[(); 123]` found type `[(); 16]` ```
2 parents 864e7a9 + 88fa5c6 commit 581cf70

File tree

12 files changed

+31
-23
lines changed

12 files changed

+31
-23
lines changed

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ pub struct Expr {
13561356

13571357
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
13581358
#[cfg(target_arch = "x86_64")]
1359-
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 72);
1359+
static_assert_size!(Expr, 72);
13601360

13611361
impl Expr {
13621362
pub fn precedence(&self) -> ExprPrecedence {

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ newtype_index! {
158158
impl_stable_hash_for!(struct crate::middle::region::FirstStatementIndex { private });
159159

160160
// compilation error if size of `ScopeData` is not the same as a `u32`
161-
static_assert!(ASSERT_SCOPE_DATA: mem::size_of::<ScopeData>() == 4);
161+
static_assert_size!(ScopeData, 4);
162162

163163
impl Scope {
164164
/// Returns a item-local ID associated with this scope.

src/librustc/mir/interpret/pointer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct Pointer<Tag=(),Id=AllocId> {
7878
pub tag: Tag,
7979
}
8080

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

8383
/// Produces a `Pointer` which points to the beginning of the Allocation
8484
impl From<AllocId> for Pointer {

src/librustc/mir/interpret/value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub enum ConstValue<'tcx> {
5454
}
5555

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

5959
impl<'tcx> ConstValue<'tcx> {
6060
#[inline]
@@ -111,7 +111,7 @@ pub enum Scalar<Tag=(), Id=AllocId> {
111111
}
112112

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

116116
impl<Tag> fmt::Display for Scalar<Tag> {
117117
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/librustc/mir/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ pub struct Statement<'tcx> {
17381738

17391739
// `Statement` is used a lot. Make sure it doesn't unintentionally get bigger.
17401740
#[cfg(target_arch = "x86_64")]
1741-
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Statement<'_>>() == 56);
1741+
static_assert_size!(Statement<'_>, 56);
17421742

17431743
impl<'tcx> Statement<'tcx> {
17441744
/// Changes a statement to a nop. This is both faster than deleting instructions and avoids
@@ -1997,10 +1997,9 @@ pub type PlaceProjection<'tcx> = Projection<Place<'tcx>, Local, Ty<'tcx>>;
19971997
/// and the index is a local.
19981998
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
19991999

2000-
// at least on 64 bit systems, `PlaceElem` should not be larger than two pointers
2001-
static_assert!(PROJECTION_ELEM_IS_2_PTRS_LARGE:
2002-
mem::size_of::<PlaceElem<'_>>() <= 16
2003-
);
2000+
// At least on 64 bit systems, `PlaceElem` should not be larger than two pointers.
2001+
#[cfg(target_arch = "x86_64")]
2002+
static_assert_size!(PlaceElem<'_>, 16);
20042003

20052004
/// Alias for projections as they appear in `UserTypeProjection`, where we
20062005
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.

src/librustc/mir/tcx.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub struct PlaceTy<'tcx> {
1717
pub variant_index: Option<VariantIdx>,
1818
}
1919

20-
static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
21-
mem::size_of::<PlaceTy<'_>>() <= 24
22-
);
20+
// At least on 64 bit systems, `PlaceTy` should not be larger than two or three pointers.
21+
#[cfg(target_arch = "x86_64")]
22+
static_assert_size!(PlaceTy<'_>, 16);
2323

2424
impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
2525
pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> {

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ pub struct TyS<'tcx> {
510510

511511
// `TyS` is used a lot. Make sure it doesn't unintentionally get bigger.
512512
#[cfg(target_arch = "x86_64")]
513-
static_assert!(MEM_SIZE_OF_TY_S: ::std::mem::size_of::<TyS<'_>>() == 32);
513+
static_assert_size!(TyS<'_>, 32);
514514

515515
impl<'tcx> Ord for TyS<'tcx> {
516516
fn cmp(&self, other: &TyS<'tcx>) -> Ordering {

src/librustc/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub enum TyKind<'tcx> {
211211

212212
// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
213213
#[cfg(target_arch = "x86_64")]
214-
static_assert!(MEM_SIZE_OF_TY_KIND: ::std::mem::size_of::<TyKind<'_>>() == 24);
214+
static_assert_size!(TyKind<'_>, 24);
215215

216216
/// A closure can be modeled as a struct that looks like:
217217
///
@@ -2207,7 +2207,7 @@ pub struct Const<'tcx> {
22072207
}
22082208

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

22122212
impl<'tcx> Const<'tcx> {
22132213
#[inline]

src/librustc_data_structures/macros.rs

+9
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ macro_rules! static_assert {
1010
static $name: () = [()][!($test: bool) as usize];
1111
}
1212
}
13+
14+
/// Type size assertion. The first argument is a type and the second argument is its expected size.
15+
#[macro_export]
16+
#[allow_internal_unstable(underscore_const_names)]
17+
macro_rules! static_assert_size {
18+
($ty:ty, $size:expr) => {
19+
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
20+
}
21+
}

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::ThinVec;
1616

1717
use rustc_data_structures::indexed_vec::Idx;
1818
#[cfg(target_arch = "x86_64")]
19-
use rustc_data_structures::static_assert;
19+
use rustc_data_structures::static_assert_size;
2020
use rustc_target::spec::abi::Abi;
2121
use syntax_pos::{Span, DUMMY_SP};
2222

@@ -964,7 +964,7 @@ pub struct Expr {
964964

965965
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
966966
#[cfg(target_arch = "x86_64")]
967-
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 96);
967+
static_assert_size!(Expr, 96);
968968

969969
impl Expr {
970970
/// Whether this expression would be valid somewhere that expects a value; for example, an `if`

src/libsyntax/parse/token.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use log::info;
1919
use std::fmt;
2020
use std::mem;
2121
#[cfg(target_arch = "x86_64")]
22-
use rustc_data_structures::static_assert;
22+
use rustc_data_structures::static_assert_size;
2323
use rustc_data_structures::sync::Lrc;
2424

2525
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
@@ -74,7 +74,7 @@ pub enum Lit {
7474
}
7575

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

7979
impl Lit {
8080
crate fn literal_name(&self) -> &'static str {
@@ -220,7 +220,7 @@ pub enum Token {
220220

221221
// `Token` is used a lot. Make sure it doesn't unintentionally get bigger.
222222
#[cfg(target_arch = "x86_64")]
223-
static_assert!(MEM_SIZE_OF_STATEMENT: mem::size_of::<Token>() == 16);
223+
static_assert_size!(Token, 16);
224224

225225
impl Token {
226226
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.

src/libsyntax/tokenstream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::print::pprust;
2121

2222
use syntax_pos::{BytePos, Mark, Span, DUMMY_SP};
2323
#[cfg(target_arch = "x86_64")]
24-
use rustc_data_structures::static_assert;
24+
use rustc_data_structures::static_assert_size;
2525
use rustc_data_structures::sync::Lrc;
2626
use serialize::{Decoder, Decodable, Encoder, Encodable};
2727
use smallvec::{SmallVec, smallvec};
@@ -158,7 +158,7 @@ pub type TreeAndJoint = (TokenTree, IsJoint);
158158

159159
// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
160160
#[cfg(target_arch = "x86_64")]
161-
static_assert!(MEM_SIZE_OF_TOKEN_STREAM: mem::size_of::<TokenStream>() == 8);
161+
static_assert_size!(TokenStream, 8);
162162

163163
#[derive(Clone, Copy, Debug, PartialEq)]
164164
pub enum IsJoint {

0 commit comments

Comments
 (0)