]> git.lizzy.rs Git - rust.git/commitdiff
Forbid the upper indices of `IndexVec` indices to allow for niche optimizations
authorOliver Schneider <github35764891676564198441@oli-obk.de>
Fri, 7 Sep 2018 13:28:23 +0000 (15:28 +0200)
committerOliver Schneider <github35764891676564198441@oli-obk.de>
Tue, 11 Sep 2018 09:25:28 +0000 (11:25 +0200)
src/librustc/lib.rs
src/librustc/middle/region.rs
src/librustc_data_structures/indexed_vec.rs
src/librustc_mir/lib.rs
src/test/run-pass-fulldeps/newtype_index.rs [new file with mode: 0644]

index d79281666d63914e7f137927c04100b96ec02368..301fc76f93257eee3c869a2ec0073c5b4850c959 100644 (file)
@@ -59,6 +59,8 @@
 #![feature(optin_builtin_traits)]
 #![feature(refcell_replace_swap)]
 #![feature(rustc_diagnostic_macros)]
+#![feature(rustc_attrs)]
+#![cfg_attr(stage0, feature(attr_literals))]
 #![feature(slice_patterns)]
 #![feature(slice_sort_by_cached_key)]
 #![feature(specialization)]
index f6a8f8dc172d4c0266b9b73b9547ef3380ede793..a2a056a3590c999e700b1f16a6139fdbb71443f2 100644 (file)
@@ -114,7 +114,7 @@ pub struct Scope {
 const SCOPE_DATA_CALLSITE: u32 = !1;
 const SCOPE_DATA_ARGUMENTS: u32 = !2;
 const SCOPE_DATA_DESTRUCTION: u32 = !3;
-const SCOPE_DATA_REMAINDER_MAX: u32 = !4;
+// be sure to add the MAX of FirstStatementIndex if you add more constants here
 
 #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
 pub enum ScopeData {
@@ -160,9 +160,7 @@ pub struct BlockRemainder {
 }
 
 newtype_index! {
-    pub struct FirstStatementIndex {
-        MAX = SCOPE_DATA_REMAINDER_MAX
-    }
+    pub struct FirstStatementIndex;
 }
 
 impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });
index 186bc6d43ccc560a0afb47d0fd27ee926167357a..2d1709f2374a9ceab6e46accb9757d7ac9d37a9c 100644 (file)
@@ -72,7 +72,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX - 1]
+            @max          [0xFFFF_FFFE]
             @vis          [$v]
             @debug_format ["{}"]);
     );
@@ -82,7 +82,7 @@ macro_rules! newtype_index {
         newtype_index!(
             // Leave out derives marker so we can use its absence to ensure it comes first
             @type         [$name]
-            @max          [::std::u32::MAX - 1]
+            @max          [0xFFFF_FFFE]
             @vis          [$v]
             @debug_format ["{}"]
                           $($tokens)+);
@@ -97,6 +97,7 @@ macro_rules! newtype_index {
      @vis          [$v:vis]
      @debug_format [$debug_format:tt]) => (
         #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
+        #[rustc_layout_scalar_range_end($max)]
         $v struct $type {
             private: u32
         }
index d4024981c3754cac0e1445afc1c59b0d81730505..08ee40dbcf454bf33e26ea54a687e9c8dc83db21 100644 (file)
@@ -30,6 +30,8 @@
 #![feature(exhaustive_patterns)]
 #![feature(range_contains)]
 #![feature(rustc_diagnostic_macros)]
+#![feature(rustc_attrs)]
+#![cfg_attr(stage0, feature(attr_literals))]
 #![feature(never_type)]
 #![feature(specialization)]
 #![feature(try_trait)]
diff --git a/src/test/run-pass-fulldeps/newtype_index.rs b/src/test/run-pass-fulldeps/newtype_index.rs
new file mode 100644 (file)
index 0000000..d10c51b
--- /dev/null
@@ -0,0 +1,20 @@
+#![feature(rustc_attrs, step_trait, rustc_private)]
+
+#[macro_use] extern crate rustc_data_structures;
+extern crate rustc_serialize;
+
+use rustc_data_structures::indexed_vec::Idx;
+
+newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });
+
+use std::mem::size_of;
+
+fn main() {
+    assert_eq!(size_of::<MyIdx>(), 4);
+    assert_eq!(size_of::<Option<MyIdx>>(), 4);
+    assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
+    assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
+    assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
+    assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
+    assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
+}
\ No newline at end of file