Skip to content

Commit 06a041c

Browse files
committed
Forbid the upper indices of IndexVec indices to allow for niche optimizations
1 parent d272e2f commit 06a041c

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

src/librustc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
#![feature(optin_builtin_traits)]
6060
#![feature(refcell_replace_swap)]
6161
#![feature(rustc_diagnostic_macros)]
62+
#![feature(rustc_attrs)]
63+
#![cfg_attr(stage0, feature(attr_literals))]
6264
#![feature(slice_patterns)]
6365
#![feature(slice_sort_by_cached_key)]
6466
#![feature(specialization)]

src/librustc/middle/region.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const SCOPE_DATA_NODE: u32 = !0;
114114
const SCOPE_DATA_CALLSITE: u32 = !1;
115115
const SCOPE_DATA_ARGUMENTS: u32 = !2;
116116
const SCOPE_DATA_DESTRUCTION: u32 = !3;
117-
const SCOPE_DATA_REMAINDER_MAX: u32 = !4;
117+
// be sure to add the MAX of FirstStatementIndex if you add more constants here
118118

119119
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, RustcEncodable, RustcDecodable)]
120120
pub enum ScopeData {
@@ -160,9 +160,7 @@ pub struct BlockRemainder {
160160
}
161161

162162
newtype_index! {
163-
pub struct FirstStatementIndex {
164-
MAX = SCOPE_DATA_REMAINDER_MAX
165-
}
163+
pub struct FirstStatementIndex;
166164
}
167165

168166
impl_stable_hash_for!(struct ::middle::region::FirstStatementIndex { private });

src/librustc_data_structures/indexed_vec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ macro_rules! newtype_index {
7272
newtype_index!(
7373
// Leave out derives marker so we can use its absence to ensure it comes first
7474
@type [$name]
75-
@max [::std::u32::MAX - 1]
75+
@max [0xFFFF_FFFE]
7676
@vis [$v]
7777
@debug_format ["{}"]);
7878
);
@@ -82,7 +82,7 @@ macro_rules! newtype_index {
8282
newtype_index!(
8383
// Leave out derives marker so we can use its absence to ensure it comes first
8484
@type [$name]
85-
@max [::std::u32::MAX - 1]
85+
@max [0xFFFF_FFFE]
8686
@vis [$v]
8787
@debug_format ["{}"]
8888
$($tokens)+);
@@ -97,6 +97,7 @@ macro_rules! newtype_index {
9797
@vis [$v:vis]
9898
@debug_format [$debug_format:tt]) => (
9999
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]
100+
#[rustc_layout_scalar_range_end($max)]
100101
$v struct $type {
101102
private: u32
102103
}

src/librustc_mir/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
3030
#![feature(exhaustive_patterns)]
3131
#![feature(range_contains)]
3232
#![feature(rustc_diagnostic_macros)]
33+
#![feature(rustc_attrs)]
34+
#![cfg_attr(stage0, feature(attr_literals))]
3335
#![feature(never_type)]
3436
#![feature(specialization)]
3537
#![feature(try_trait)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(rustc_attrs, step_trait, rustc_private)]
2+
3+
#[macro_use] extern crate rustc_data_structures;
4+
extern crate rustc_serialize;
5+
6+
use rustc_data_structures::indexed_vec::Idx;
7+
8+
newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });
9+
10+
use std::mem::size_of;
11+
12+
fn main() {
13+
assert_eq!(size_of::<MyIdx>(), 4);
14+
assert_eq!(size_of::<Option<MyIdx>>(), 4);
15+
assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
16+
assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
17+
assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
18+
assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
19+
assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
20+
}

0 commit comments

Comments
 (0)