Skip to content

Commit a332387

Browse files
committed
add a macro for static assertions
1 parent 4623d48 commit a332387

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/librustc/macros.rs

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ macro_rules! span_bug {
6262
})
6363
}
6464

65+
#[macro_export]
66+
macro_rules! static_assert {
67+
($name:ident: $test:expr) => {
68+
// Use the bool to access an array such that if the bool is false, the access
69+
// is out-of-bounds.
70+
#[allow(dead_code)]
71+
static $name: () = [()][!$test as usize];
72+
}
73+
}
74+
6575
#[macro_export]
6676
macro_rules! __impl_stable_hash_field {
6777
($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher));

src/librustc/middle/region.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ newtype_index! {
167167
impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });
168168

169169
// compilation error if size of `ScopeData` is not the same as a `u32`
170-
#[allow(dead_code)]
171-
static ASSERT: () = [()][!(mem::size_of::<ScopeData>() == 4) as usize];
170+
static_assert!(ASSERT_SCOPE_DATA: mem::size_of::<ScopeData>() == 4);
172171

173172
impl Scope {
174173
/// Returns a item-local id associated with this scope.

src/librustc/ty/context.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,9 @@ impl<'tcx> CommonTypes<'tcx> {
827827
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
828828
// Ensure our type representation does not grow
829829
#[cfg(target_pointer_width = "64")]
830-
#[allow(dead_code)]
831-
static ASSERT_TY_KIND: () =
832-
[()][!(::std::mem::size_of::<ty::TyKind<'_>>() <= 24) as usize];
830+
static_assert!(ASSERT_TY_KIND: ::std::mem::size_of::<ty::TyKind<'_>>() <= 24);
833831
#[cfg(target_pointer_width = "64")]
834-
#[allow(dead_code)]
835-
static ASSERT_TYS: () = [()][!(::std::mem::size_of::<ty::TyS<'_>>() <= 32) as usize];
832+
static_assert!(ASSERT_TYS: ::std::mem::size_of::<ty::TyS<'_>>() <= 32);
836833

837834
let mk = |sty| CtxtInterners::intern_ty(interners, interners, sty);
838835
let mk_region = |r| {

0 commit comments

Comments
 (0)