Skip to content

Commit c8d743c

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 5226ac4 + 9ac93ee commit c8d743c

File tree

16 files changed

+53
-52
lines changed

16 files changed

+53
-52
lines changed

src/librustc/hir/map/mod.rs

+1-1
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

+3
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

+1-1
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

+2
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

+1-1
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

+1-1
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

+2
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

+3
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

+7-25
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

+3
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)]

src/librustc_mir/transform/generator.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
107107
}
108108

109109
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
110-
assert_ne!(*local, self_arg());
110+
assert_ne!(*local, SELF_ARG);
111111
}
112112

113113
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
114-
if place.local == self_arg() {
114+
if place.local == SELF_ARG {
115115
replace_base(
116116
place,
117117
Place {
118-
local: self_arg(),
118+
local: SELF_ARG,
119119
projection: self.tcx().intern_place_elems(&[ProjectionElem::Deref]),
120120
},
121121
self.tcx,
@@ -125,7 +125,7 @@ impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor<'tcx> {
125125

126126
for elem in place.projection.iter() {
127127
if let PlaceElem::Index(local) = elem {
128-
assert_ne!(*local, self_arg());
128+
assert_ne!(*local, SELF_ARG);
129129
}
130130
}
131131
}
@@ -143,15 +143,15 @@ impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {
143143
}
144144

145145
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
146-
assert_ne!(*local, self_arg());
146+
assert_ne!(*local, SELF_ARG);
147147
}
148148

149149
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
150-
if place.local == self_arg() {
150+
if place.local == SELF_ARG {
151151
replace_base(
152152
place,
153153
Place {
154-
local: self_arg(),
154+
local: SELF_ARG,
155155
projection: self.tcx().intern_place_elems(&[ProjectionElem::Field(
156156
Field::new(0),
157157
self.ref_gen_ty,
@@ -164,7 +164,7 @@ impl<'tcx> MutVisitor<'tcx> for PinArgVisitor<'tcx> {
164164

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

183-
fn self_arg() -> Local {
184-
Local::new(1)
185-
}
183+
const SELF_ARG: Local = Local::from_u32(1);
186184

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

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

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

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

542540
// The generator argument is ignored.
543-
live_locals_here.remove(self_arg());
541+
live_locals_here.remove(SELF_ARG);
544542

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

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

839837
let param_env = tcx.param_env(def_id);
840-
let gen = self_arg();
841838

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

844841
for (block, block_data) in body.basic_blocks().iter_enumerated() {
845842
let (target, unwind, source_info) = match block_data.terminator() {
846843
Terminator { source_info, kind: TerminatorKind::Drop { location, target, unwind } } => {
847844
if let Some(local) = location.as_local() {
848-
if local == gen {
845+
if local == SELF_ARG {
849846
(target, unwind, source_info)
850847
} else {
851848
continue;
@@ -864,7 +861,7 @@ fn elaborate_generator_drops<'tcx>(
864861
elaborate_drop(
865862
&mut elaborator,
866863
*source_info,
867-
&Place::from(gen),
864+
&Place::from(SELF_ARG),
868865
(),
869866
*target,
870867
unwind,
@@ -918,7 +915,7 @@ fn create_generator_drop_shim<'tcx>(
918915
make_generator_state_argument_indirect(tcx, def_id, &mut body);
919916

920917
// Change the generator argument from &mut to *mut
921-
body.local_decls[self_arg()] = LocalDecl {
918+
body.local_decls[SELF_ARG] = LocalDecl {
922919
mutability: Mutability::Mut,
923920
ty: tcx.mk_ptr(ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut }),
924921
user_ty: UserTypeProjections::none(),
@@ -933,7 +930,7 @@ fn create_generator_drop_shim<'tcx>(
933930
0,
934931
Statement {
935932
source_info,
936-
kind: StatementKind::Retag(RetagKind::Raw, box Place::from(self_arg())),
933+
kind: StatementKind::Retag(RetagKind::Raw, box Place::from(SELF_ARG)),
937934
},
938935
)
939936
}
@@ -1042,7 +1039,7 @@ fn insert_clean_drop(body: &mut BodyAndCache<'_>) -> BasicBlock {
10421039
// Create a block to destroy an unresumed generators. This can only destroy upvars.
10431040
let drop_clean = BasicBlock::new(body.basic_blocks().len());
10441041
let term = TerminatorKind::Drop {
1045-
location: Place::from(self_arg()),
1042+
location: Place::from(SELF_ARG),
10461043
target: return_block,
10471044
unwind: None,
10481045
};

src/librustc_mir_build/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
#![feature(box_patterns)]
66
#![feature(box_syntax)]
7+
#![feature(const_if_match)]
8+
#![feature(const_fn)]
9+
#![feature(const_panic)]
710
#![feature(crate_visibility_modifier)]
811
#![feature(bool_to_option)]
912
#![recursion_limit = "256"]

src/librustc_span/def_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum CrateNum {
2525

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

3030
impl Idx for CrateNum {
3131
#[inline]

src/librustc_span/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
88
#![feature(crate_visibility_modifier)]
9+
#![feature(const_if_match)]
10+
#![feature(const_fn)]
11+
#![feature(const_panic)]
912
#![feature(nll)]
1013
#![feature(optin_builtin_traits)]
1114
#![feature(specialization)]

src/librustc_span/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ rustc_index::newtype_index! {
10261026

10271027
impl Symbol {
10281028
const fn new(n: u32) -> Self {
1029-
Symbol(SymbolIndex::from_u32_const(n))
1029+
Symbol(SymbolIndex::from_u32(n))
10301030
}
10311031

10321032
/// Maps a string to its interned representation.

src/librustc_target/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
1010
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
1111
#![feature(bool_to_option)]
12+
#![feature(const_if_match)]
13+
#![feature(const_fn)]
14+
#![feature(const_panic)]
1215
#![feature(nll)]
1316
#![feature(never_type)]
1417
#![feature(associated_type_bounds)]

0 commit comments

Comments
 (0)