Skip to content

Commit 2521849

Browse files
authored
Rollup merge of rust-lang#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)]`.
2 parents a958314 + 9ac93ee commit 2521849

File tree

16 files changed

+53
-52
lines changed

16 files changed

+53
-52
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl<'hir> Map<'hir> {
346346
}
347347

348348
fn get_entry(&self, id: HirId) -> Entry<'hir> {
349-
if id.local_id == ItemLocalId::from_u32_const(0) {
349+
if id.local_id == ItemLocalId::from_u32(0) {
350350
let owner = self.tcx.hir_owner(id.owner_def_id());
351351
Entry { parent: owner.parent, node: owner.node }
352352
} else {

src/librustc/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#![feature(bool_to_option)]
2727
#![feature(box_patterns)]
2828
#![feature(box_syntax)]
29+
#![feature(const_if_match)]
30+
#![feature(const_fn)]
31+
#![feature(const_panic)]
2932
#![feature(const_transmute)]
3033
#![feature(core_intrinsics)]
3134
#![feature(drain_filter)]

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ rustc_index::newtype_index! {
17001700
}
17011701

17021702
impl UniverseIndex {
1703-
pub const ROOT: UniverseIndex = UniverseIndex::from_u32_const(0);
1703+
pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0);
17041704

17051705
/// Returns the "next" universe index in order -- this new index
17061706
/// is considered to extend all previous universes. This

src/librustc_ast/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
88
#![feature(bool_to_option)]
99
#![feature(box_syntax)]
10+
#![feature(const_if_match)]
1011
#![feature(const_fn)] // For the `transmute` in `P::new`
12+
#![feature(const_panic)]
1113
#![feature(const_transmute)]
1214
#![feature(crate_visibility_modifier)]
1315
#![feature(label_break_value)]

src/librustc_ast/node_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rustc_index::newtype_index! {
1212
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
1313

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

1717
/// When parsing and doing expansions, we initially give all AST nodes this AST
1818
/// node value. Then later, in the renumber pass, we renumber them to have

src/librustc_hir/hir_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
7171

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

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

src/librustc_hir/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
44
55
#![feature(crate_visibility_modifier)]
6+
#![feature(const_if_match)]
67
#![feature(const_fn)] // For the unsizing cast on `&[]`
8+
#![feature(const_panic)]
79
#![feature(in_band_lifetimes)]
810
#![feature(specialization)]
911
#![recursion_limit = "256"]

src/librustc_index/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![feature(allow_internal_unstable)]
2+
#![feature(const_if_match)]
3+
#![feature(const_fn)]
4+
#![feature(const_panic)]
25
#![feature(unboxed_closures)]
36
#![feature(test)]
47
#![feature(fn_traits)]

src/librustc_index/vec.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,62 +120,44 @@ macro_rules! newtype_index {
120120
impl $type {
121121
$v const MAX_AS_U32: u32 = $max;
122122

123-
$v const MAX: Self = Self::from_u32_const($max);
123+
$v const MAX: Self = Self::from_u32($max);
124124

125125
#[inline]
126-
$v fn from_usize(value: usize) -> Self {
126+
$v const fn from_usize(value: usize) -> Self {
127127
assert!(value <= ($max as usize));
128128
unsafe {
129129
Self::from_u32_unchecked(value as u32)
130130
}
131131
}
132132

133133
#[inline]
134-
$v fn from_u32(value: u32) -> Self {
134+
$v const fn from_u32(value: u32) -> Self {
135135
assert!(value <= $max);
136136
unsafe {
137137
Self::from_u32_unchecked(value)
138138
}
139139
}
140140

141-
/// Hacky variant of `from_u32` for use in constants.
142-
/// This version checks the "max" constraint by using an
143-
/// invalid array dereference.
144-
#[inline]
145-
$v const fn from_u32_const(value: u32) -> Self {
146-
// This will fail at const eval time unless `value <=
147-
// max` is true (in which case we get the index 0).
148-
// It will also fail at runtime, of course, but in a
149-
// kind of wacky way.
150-
let _ = ["out of range value used"][
151-
!(value <= $max) as usize
152-
];
153-
154-
unsafe {
155-
Self { private: value }
156-
}
157-
}
158-
159141
#[inline]
160142
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
161143
Self { private: value }
162144
}
163145

164146
/// Extracts the value of this index as an integer.
165147
#[inline]
166-
$v fn index(self) -> usize {
148+
$v const fn index(self) -> usize {
167149
self.as_usize()
168150
}
169151

170152
/// Extracts the value of this index as a `u32`.
171153
#[inline]
172-
$v fn as_u32(self) -> u32 {
154+
$v const fn as_u32(self) -> u32 {
173155
self.private
174156
}
175157

176158
/// Extracts the value of this index as a `usize`.
177159
#[inline]
178-
$v fn as_usize(self) -> usize {
160+
$v const fn as_usize(self) -> usize {
179161
self.as_u32() as usize
180162
}
181163
}
@@ -500,7 +482,7 @@ macro_rules! newtype_index {
500482
const $name:ident = $constant:expr,
501483
$($tokens:tt)*) => (
502484
$(#[doc = $doc])*
503-
$v const $name: $type = $type::from_u32_const($constant);
485+
$v const $name: $type = $type::from_u32($constant);
504486
$crate::newtype_index!(
505487
@derives [$($derives,)*]
506488
@attrs [$(#[$attrs])*]

src/librustc_mir/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Rust MIR: a lowered representation of Rust.
99
#![feature(bool_to_option)]
1010
#![feature(box_patterns)]
1111
#![feature(box_syntax)]
12+
#![feature(const_if_match)]
13+
#![feature(const_fn)]
14+
#![feature(const_panic)]
1215
#![feature(crate_visibility_modifier)]
1316
#![feature(drain_filter)]
1417
#![feature(exhaustive_patterns)]

0 commit comments

Comments
 (0)