about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-03-18 18:03:42 +0100
committerGitHub <noreply@github.com>2020-03-18 18:03:42 +0100
commit252184969b7191e8be9b485b14a4f0fd216947da (patch)
treeb05137f2581a6b579d904df1fc7433c17341889b
parenta958314472f07a71e6d2e59d8dbf73f9edee70c5 (diff)
parent9ac93eee6df4ca08e69ec97a112657640deb8bb3 (diff)
downloadrust-252184969b7191e8be9b485b14a4f0fd216947da.tar.gz
rust-252184969b7191e8be9b485b14a4f0fd216947da.zip
Rollup merge of #69899 - ecstatic-morse:const-idx-methods, r=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)]`.
-rw-r--r--src/librustc/hir/map/mod.rs2
-rw-r--r--src/librustc/lib.rs3
-rw-r--r--src/librustc/ty/mod.rs2
-rw-r--r--src/librustc_ast/lib.rs2
-rw-r--r--src/librustc_ast/node_id.rs2
-rw-r--r--src/librustc_hir/hir_id.rs2
-rw-r--r--src/librustc_hir/lib.rs2
-rw-r--r--src/librustc_index/lib.rs3
-rw-r--r--src/librustc_index/vec.rs32
-rw-r--r--src/librustc_mir/lib.rs3
-rw-r--r--src/librustc_mir/transform/generator.rs39
-rw-r--r--src/librustc_mir_build/lib.rs3
-rw-r--r--src/librustc_span/def_id.rs2
-rw-r--r--src/librustc_span/lib.rs3
-rw-r--r--src/librustc_span/symbol.rs2
-rw-r--r--src/librustc_target/lib.rs3
16 files changed, 53 insertions, 52 deletions
diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs
index 55ed07a97d1..ba1665fb530 100644
--- a/src/librustc/hir/map/mod.rs
+++ b/src/librustc/hir/map/mod.rs
@@ -346,7 +346,7 @@ impl<'hir> Map<'hir> {
     }
 
     fn get_entry(&self, id: HirId) -> Entry<'hir> {
-        if id.local_id == ItemLocalId::from_u32_const(0) {
+        if id.local_id == ItemLocalId::from_u32(0) {
             let owner = self.tcx.hir_owner(id.owner_def_id());
             Entry { parent: owner.parent, node: owner.node }
         } else {
diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs
index 24237235e0c..555a85fbfb3 100644
--- a/src/librustc/lib.rs
+++ b/src/librustc/lib.rs
@@ -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)]
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index d622d6fbe2c..dd834d9109d 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -1700,7 +1700,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
diff --git a/src/librustc_ast/lib.rs b/src/librustc_ast/lib.rs
index adb96356aae..2594cc536ac 100644
--- a/src/librustc_ast/lib.rs
+++ b/src/librustc_ast/lib.rs
@@ -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)]
diff --git a/src/librustc_ast/node_id.rs b/src/librustc_ast/node_id.rs
index 58d2334a7b1..cd562c48e91 100644
--- a/src/librustc_ast/node_id.rs
+++ b/src/librustc_ast/node_id.rs
@@ -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
diff --git a/src/librustc_hir/hir_id.rs b/src/librustc_hir/hir_id.rs
index 6d2ec445763..a11638a3bb2 100644
--- a/src/librustc_hir/hir_id.rs
+++ b/src/librustc_hir/hir_id.rs
@@ -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 };
 
diff --git a/src/librustc_hir/lib.rs b/src/librustc_hir/lib.rs
index 45f806b53f5..fa5c72b060d 100644
--- a/src/librustc_hir/lib.rs
+++ b/src/librustc_hir/lib.rs
@@ -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"]
diff --git a/src/librustc_index/lib.rs b/src/librustc_index/lib.rs
index 86dd1a29d0c..e8aa1a209e9 100644
--- a/src/librustc_index/lib.rs
+++ b/src/librustc_index/lib.rs
@@ -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)]
diff --git a/src/librustc_index/vec.rs b/src/librustc_index/vec.rs
index 7020939fa20..d8c67f6210c 100644
--- a/src/librustc_index/vec.rs
+++ b/src/librustc_index/vec.rs
@@ -120,10 +120,10 @@ 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)
@@ -131,31 +131,13 @@ macro_rules! newtype_index {
             }
 
             #[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 }
@@ -163,19 +145,19 @@ macro_rules! newtype_index {
 
             /// 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
             }
         }
@@ -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])*]
diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs
index 284dd74ce99..7d3aba3ff03 100644
--- a/src/librustc_mir/lib.rs
+++ b/src/librustc_mir/lib.rs
@@ -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)]
diff --git a/src/librustc_mir/transform/generator.rs b/src/librustc_mir/transform/generator.rs
index 7418a7519ba..a179cd31075 100644
--- a/src/librustc_mir/transform/generator.rs
+++ b/src/librustc_mir/transform/generator.rs
@@ -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,
@@ -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);
                 }
             }
         }
@@ -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,
@@ -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);
                 }
             }
         }
@@ -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;
@@ -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));
@@ -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 {
@@ -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))),
@@ -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);
 
@@ -837,7 +835,6 @@ 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 };
 
@@ -845,7 +842,7 @@ fn elaborate_generator_drops<'tcx>(
         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;
@@ -864,7 +861,7 @@ fn elaborate_generator_drops<'tcx>(
         elaborate_drop(
             &mut elaborator,
             *source_info,
-            &Place::from(gen),
+            &Place::from(SELF_ARG),
             (),
             *target,
             unwind,
@@ -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(),
@@ -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)),
             },
         )
     }
@@ -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,
     };
diff --git a/src/librustc_mir_build/lib.rs b/src/librustc_mir_build/lib.rs
index 3c35827d15d..5a8b5a32963 100644
--- a/src/librustc_mir_build/lib.rs
+++ b/src/librustc_mir_build/lib.rs
@@ -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"]
diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs
index a2944782e91..af8d5ce09b5 100644
--- a/src/librustc_span/def_id.rs
+++ b/src/librustc_span/def_id.rs
@@ -25,7 +25,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]
diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs
index caa50e9a41c..1f6d10f4e8f 100644
--- a/src/librustc_span/lib.rs
+++ b/src/librustc_span/lib.rs
@@ -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)]
diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs
index 19754c83038..5685505f694 100644
--- a/src/librustc_span/symbol.rs
+++ b/src/librustc_span/symbol.rs
@@ -1026,7 +1026,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.
diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs
index 3c397eb444d..98190867d49 100644
--- a/src/librustc_target/lib.rs
+++ b/src/librustc_target/lib.rs
@@ -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)]