Skip to content

Commit

Permalink
Rollup merge of rust-lang#69899 - ecstatic-morse:const-idx-methods, r…
Browse files Browse the repository at this point in the history
…=oli-obk

Make methods declared by `newtype_index` macro `const`

Crates that use the macro to define an `Idx` type need to enable `#![feature(const_if_match, const_panic)]`.
  • Loading branch information
Dylan-DPC committed Mar 15, 2020
2 parents a338c0e + 08d345e commit 513702d
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 51 deletions.
3 changes: 3 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(const_transmute)]
#![feature(core_intrinsics)]
#![feature(drain_filter)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ rustc_index::newtype_index! {
}

impl UniverseIndex {
pub const ROOT: UniverseIndex = UniverseIndex::from_u32_const(0);
pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0);

/// Returns the "next" universe index in order -- this new index
/// is considered to extend all previous universes. This
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_ast/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(const_if_match)]
#![feature(const_fn)] // For the `transmute` in `P::new`
#![feature(const_panic)]
#![feature(const_transmute)]
#![feature(crate_visibility_modifier)]
#![feature(label_break_value)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rustc_index::newtype_index! {
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);

/// `NodeId` used to represent the root of the crate.
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0);
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);

/// When parsing and doing expansions, we initially give all AST nodes this AST
/// node value. Then later, in the renumber pass, we renumber them to have
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_hir/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);

/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
pub const CRATE_HIR_ID: HirId =
HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) };
HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32(0) };

pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID };

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_hir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html

#![feature(crate_visibility_modifier)]
#![feature(const_if_match)]
#![feature(const_fn)] // For the unsizing cast on `&[]`
#![feature(const_panic)]
#![feature(in_band_lifetimes)]
#![feature(specialization)]
#![recursion_limit = "256"]
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_index/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![feature(allow_internal_unstable)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(unboxed_closures)]
#![feature(test)]
#![feature(fn_traits)]
Expand Down
32 changes: 7 additions & 25 deletions src/librustc_index/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,62 +120,44 @@ macro_rules! newtype_index {
impl $type {
$v const MAX_AS_U32: u32 = $max;

$v const MAX: Self = Self::from_u32_const($max);
$v const MAX: Self = Self::from_u32($max);

#[inline]
$v fn from_usize(value: usize) -> Self {
$v const fn from_usize(value: usize) -> Self {
assert!(value <= ($max as usize));
unsafe {
Self::from_u32_unchecked(value as u32)
}
}

#[inline]
$v fn from_u32(value: u32) -> Self {
$v const fn from_u32(value: u32) -> Self {
assert!(value <= $max);
unsafe {
Self::from_u32_unchecked(value)
}
}

/// Hacky variant of `from_u32` for use in constants.
/// This version checks the "max" constraint by using an
/// invalid array dereference.
#[inline]
$v const fn from_u32_const(value: u32) -> Self {
// This will fail at const eval time unless `value <=
// max` is true (in which case we get the index 0).
// It will also fail at runtime, of course, but in a
// kind of wacky way.
let _ = ["out of range value used"][
!(value <= $max) as usize
];

unsafe {
Self { private: value }
}
}

#[inline]
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self { private: value }
}

/// Extracts the value of this index as an integer.
#[inline]
$v fn index(self) -> usize {
$v const fn index(self) -> usize {
self.as_usize()
}

/// Extracts the value of this index as a `u32`.
#[inline]
$v fn as_u32(self) -> u32 {
$v const fn as_u32(self) -> u32 {
self.private
}

/// Extracts the value of this index as a `usize`.
#[inline]
$v fn as_usize(self) -> usize {
$v const fn as_usize(self) -> usize {
self.as_u32() as usize
}
}
Expand Down Expand Up @@ -500,7 +482,7 @@ macro_rules! newtype_index {
const $name:ident = $constant:expr,
$($tokens:tt)*) => (
$(#[doc = $doc])*
$v const $name: $type = $type::from_u32_const($constant);
$v const $name: $type = $type::from_u32($constant);
$crate::newtype_index!(
@derives [$($derives,)*]
@attrs [$(#[$attrs])*]
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Rust MIR: a lowered representation of Rust.
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
#![feature(drain_filter)]
#![feature(exhaustive_patterns)]
Expand Down
39 changes: 18 additions & 21 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
}

fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
assert_ne!(*local, self_arg());
assert_ne!(*local, SELF_ARG);
}

fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
if place.local == self_arg() {
if place.local == SELF_ARG {
replace_base(
place,
Place {
local: self_arg(),
local: SELF_ARG,
projection: self.tcx().intern_place_elems(&[ProjectionElem::Deref]),
},
self.tcx,
Expand All @@ -125,7 +125,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {

for elem in place.projection.iter() {
if let PlaceElem::Index(local) = elem {
assert_ne!(*local, self_arg());
assert_ne!(*local, SELF_ARG);
}
}
}
Expand All @@ -143,15 +143,15 @@ impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {
}

fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
assert_ne!(*local, self_arg());
assert_ne!(*local, SELF_ARG);
}

fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
if place.local == self_arg() {
if place.local == SELF_ARG {
replace_base(
place,
Place {
local: self_arg(),
local: SELF_ARG,
projection: self.tcx().intern_place_elems(&[ProjectionElem::Field(
Field::new(0),
self.ref_gen_ty,
Expand All @@ -164,7 +164,7 @@ impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {

for elem in place.projection.iter() {
if let PlaceElem::Index(local) = elem {
assert_ne!(*local, self_arg());
assert_ne!(*local, SELF_ARG);
}
}
}
Expand All @@ -180,9 +180,7 @@ fn replace_base<'tcx>(place: &mut Place<'tcx>, new_base: Place<'tcx>, tcx: TyCtx
place.projection = tcx.intern_place_elems(&new_projection);
}

fn self_arg() -> Local {
Local::new(1)
}
const SELF_ARG: Local = Local::from_u32(1);

/// Generator has not been resumed yet.
const UNRESUMED: usize = GeneratorSubsts::UNRESUMED;
Expand Down Expand Up @@ -237,7 +235,7 @@ impl TransformVisitor<'tcx> {

// Create a Place referencing a generator struct field
fn make_field(&self, variant_index: VariantIdx, idx: usize, ty: Ty<'tcx>) -> Place<'tcx> {
let self_place = Place::from(self_arg());
let self_place = Place::from(SELF_ARG);
let base = self.tcx.mk_place_downcast_unnamed(self_place, variant_index);
let mut projection = base.projection.to_vec();
projection.push(ProjectionElem::Field(Field::new(idx), ty));
Expand All @@ -247,7 +245,7 @@ impl TransformVisitor<'tcx> {

// Create a statement which changes the discriminant
fn set_discr(&self, state_disc: VariantIdx, source_info: SourceInfo) -> Statement<'tcx> {
let self_place = Place::from(self_arg());
let self_place = Place::from(SELF_ARG);
Statement {
source_info,
kind: StatementKind::SetDiscriminant {
Expand All @@ -263,7 +261,7 @@ impl TransformVisitor<'tcx> {
let local_decls_len = body.local_decls.push(temp_decl);
let temp = Place::from(local_decls_len);

let self_place = Place::from(self_arg());
let self_place = Place::from(SELF_ARG);
let assign = Statement {
source_info: source_info(body),
kind: StatementKind::Assign(box (temp, Rvalue::Discriminant(self_place))),
Expand Down Expand Up @@ -540,7 +538,7 @@ fn locals_live_across_suspend_points(
live_locals_here.intersect(&liveness.outs[block]);

// The generator argument is ignored.
live_locals_here.remove(self_arg());
live_locals_here.remove(SELF_ARG);

debug!("loc = {:?}, live_locals_here = {:?}", loc, live_locals_here);

Expand Down Expand Up @@ -837,15 +835,14 @@ fn elaborate_generator_drops<'tcx>(
// generator's resume function.

let param_env = tcx.param_env(def_id);
let gen = self_arg();

let mut elaborator = DropShimElaborator { body, patch: MirPatch::new(body), tcx, param_env };

for (block, block_data) in body.basic_blocks().iter_enumerated() {
let (target, unwind, source_info) = match block_data.terminator() {
Terminator { source_info, kind: TerminatorKind::Drop { location, target, unwind } } => {
if let Some(local) = location.as_local() {
if local == gen {
if local == SELF_ARG {
(target, unwind, source_info)
} else {
continue;
Expand All @@ -864,7 +861,7 @@ fn elaborate_generator_drops<'tcx>(
elaborate_drop(
&mut elaborator,
*source_info,
&Place::from(gen),
&Place::from(SELF_ARG),
(),
*target,
unwind,
Expand Down Expand Up @@ -918,7 +915,7 @@ fn create_generator_drop_shim<'tcx>(
make_generator_state_argument_indirect(tcx, def_id, &mut body);

// Change the generator argument from &mut to *mut
body.local_decls[self_arg()] = LocalDecl {
body.local_decls[SELF_ARG] = LocalDecl {
mutability: Mutability::Mut,
ty: tcx.mk_ptr(ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut }),
user_ty: UserTypeProjections::none(),
Expand All @@ -933,7 +930,7 @@ fn create_generator_drop_shim<'tcx>(
0,
Statement {
source_info,
kind: StatementKind::Retag(RetagKind::Raw, box Place::from(self_arg())),
kind: StatementKind::Retag(RetagKind::Raw, box Place::from(SELF_ARG)),
},
)
}
Expand Down Expand Up @@ -1042,7 +1039,7 @@ fn insert_clean_drop(body: &mut BodyAndCache<'_>) -> BasicBlock {
// Create a block to destroy an unresumed generators. This can only destroy upvars.
let drop_clean = BasicBlock::new(body.basic_blocks().len());
let term = TerminatorKind::Drop {
location: Place::from(self_arg()),
location: Place::from(SELF_ARG),
target: return_block,
unwind: None,
};
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir_build/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
#![feature(bool_to_option)]
#![recursion_limit = "256"]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_span/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum CrateNum {

/// Item definitions in the currently-compiled crate would have the `CrateNum`
/// `LOCAL_CRATE` in their `DefId`.
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32(0));

impl Idx for CrateNum {
#[inline]
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(crate_visibility_modifier)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(nll)]
#![feature(optin_builtin_traits)]
#![feature(specialization)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ rustc_index::newtype_index! {

impl Symbol {
const fn new(n: u32) -> Self {
Symbol(SymbolIndex::from_u32_const(n))
Symbol(SymbolIndex::from_u32(n))
}

/// Maps a string to its interned representation.
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_target/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(bool_to_option)]
#![feature(const_if_match)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(nll)]
#![feature(never_type)]
#![feature(associated_type_bounds)]
Expand Down

0 comments on commit 513702d

Please sign in to comment.