From ab8a947fa0e8f8397ad9c831741e6429fbed2c0b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 9 Nov 2018 09:57:02 +1100 Subject: [PATCH 1/4] Move `static_assert!` into librustc_data_structures. This means it can be used by more crates. --- src/librustc/macros.rs | 10 ---------- src/librustc_data_structures/lib.rs | 1 + src/librustc_data_structures/macros.rs | 21 +++++++++++++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 src/librustc_data_structures/macros.rs diff --git a/src/librustc/macros.rs b/src/librustc/macros.rs index f21f949c9f5cd..781a0fa3f663a 100644 --- a/src/librustc/macros.rs +++ b/src/librustc/macros.rs @@ -62,16 +62,6 @@ macro_rules! span_bug { }) } -#[macro_export] -macro_rules! static_assert { - ($name:ident: $test:expr) => { - // Use the bool to access an array such that if the bool is false, the access - // is out-of-bounds. - #[allow(dead_code)] - static $name: () = [()][!$test as usize]; - } -} - #[macro_export] macro_rules! __impl_stable_hash_field { ($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher)); diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 07e5548216f3c..135abebdacb7a 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -57,6 +57,7 @@ extern crate rustc_cratesio_shim; pub use rustc_serialize::hex::ToHex; +pub mod macros; pub mod svh; pub mod base_n; pub mod bit_set; diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs new file mode 100644 index 0000000000000..3cc91b0e93f04 --- /dev/null +++ b/src/librustc_data_structures/macros.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +/// A simple static assertion macro. The first argument should be a unique +/// ALL_CAPS identifier that describes the condition. +#[macro_export] +macro_rules! static_assert { + ($name:ident: $test:expr) => { + // Use the bool to access an array such that if the bool is false, the access + // is out-of-bounds. + #[allow(dead_code)] + static $name: () = [()][!$test as usize]; + } +} From 49f482f5372c247dc50c1a2159a1ba5ee3cf5f15 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 9 Nov 2018 10:19:51 +1100 Subject: [PATCH 2/4] Move a `static_assert!` to a better spot. And make it x86_64-only so it can use `==` instead of `<=`. --- src/librustc/mir/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 8a32e8f46c290..46b61d8ffe64f 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -1719,14 +1719,14 @@ pub struct Statement<'tcx> { pub kind: StatementKind<'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); + impl<'tcx> Statement<'tcx> { /// Changes a statement to a nop. This is both faster than deleting instructions and avoids /// invalidating statement indices in `Location`s. pub fn make_nop(&mut self) { - // `Statement` contributes significantly to peak memory usage. Make - // sure it doesn't get bigger. - static_assert!(STATEMENT_IS_AT_MOST_56_BYTES: mem::size_of::>() <= 56); - self.kind = StatementKind::Nop } From fb3dd9f64ee9b34d6d0e25363ceda3a6ba9dcec3 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 9 Nov 2018 10:10:46 +1100 Subject: [PATCH 3/4] Add a static assertion about the size of `ast::Expr`. --- src/libsyntax/ast.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 2f17bc0548cad..fa6fe34783389 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,6 +20,7 @@ use print::pprust; use ptr::P; use rustc_data_structures::indexed_vec; use rustc_data_structures::indexed_vec::Idx; +use rustc_data_structures::static_assert; use rustc_target::spec::abi::Abi; use source_map::{dummy_spanned, respan, Spanned}; use symbol::{keywords, Symbol}; @@ -924,6 +925,10 @@ pub struct Expr { pub attrs: ThinVec, } +// `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::() == 88); + impl Expr { /// Whether this expression would be valid somewhere that expects a value, for example, an `if` /// condition. From 2bd4d5b1a0b05827401970f0cfbe17e6b6f32d0d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 13 Nov 2018 06:34:11 +1100 Subject: [PATCH 4/4] Move two `static_assert!`s to better spots. And make them x86_64-only so they can use `==` instead of `<=`. --- src/librustc/ty/context.rs | 6 ------ src/librustc/ty/mod.rs | 4 ++++ src/librustc/ty/sty.rs | 4 ++++ src/libsyntax/ast.rs | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 82095a2f5b01d..f78fad9f7aa9a 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -823,12 +823,6 @@ impl<'a, 'gcx> HashStable> for TypeckTables<'gcx> { impl<'tcx> CommonTypes<'tcx> { fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> { - // Ensure our type representation does not grow - #[cfg(target_pointer_width = "64")] - static_assert!(ASSERT_TY_KIND: ::std::mem::size_of::>() <= 24); - #[cfg(target_pointer_width = "64")] - static_assert!(ASSERT_TYS: ::std::mem::size_of::>() <= 32); - let mk = |sty| CtxtInterners::intern_ty(interners, interners, sty); let mk_region = |r| { if let Some(r) = interners.region.borrow().get(&r) { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index ef9b3e3efab27..3250342613510 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -514,6 +514,10 @@ pub struct TyS<'tcx> { outer_exclusive_binder: ty::DebruijnIndex, } +// `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); + impl<'tcx> Ord for TyS<'tcx> { fn cmp(&self, other: &TyS<'tcx>) -> Ordering { self.sty.cmp(&other.sty) diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index a4130bf15cb82..bd3a34cae90f4 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -211,6 +211,10 @@ pub enum TyKind<'tcx> { Error, } +// `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); + /// A closure can be modeled as a struct that looks like: /// /// struct Closure<'l0...'li, T0...Tj, CK, CS, U0...Uk> { diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index fa6fe34783389..808e19d6f120f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,6 +20,7 @@ use print::pprust; use ptr::P; use rustc_data_structures::indexed_vec; use rustc_data_structures::indexed_vec::Idx; +#[cfg(target_arch = "x86_64")] use rustc_data_structures::static_assert; use rustc_target::spec::abi::Abi; use source_map::{dummy_spanned, respan, Spanned};