Skip to content

Commit

Permalink
Forbid the upper indices of IndexVec indices to allow for niche opt…
Browse files Browse the repository at this point in the history
…imizations
  • Loading branch information
oli-obk committed Sep 11, 2018
1 parent d272e2f commit 06a041c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const SCOPE_DATA_NODE: u32 = !0;
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 {
Expand Down Expand Up @@ -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 });
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_data_structures/indexed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ["{}"]);
);
Expand All @@ -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)+);
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![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)]
Expand Down
20 changes: 20 additions & 0 deletions src/test/run-pass-fulldeps/newtype_index.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 06a041c

Please sign in to comment.