Skip to content

Commit 4160021

Browse files
Define Qualif indices via macro
1 parent 1e0dfac commit 4160021

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ impl fmt::Display for Mode {
7070
}
7171
}
7272

73-
const QUALIF_COUNT: usize = 4;
74-
7573
// FIXME(eddyb) once we can use const generics, replace this array with
7674
// something like `IndexVec` but for fixed-size arrays (`IndexArray`?).
7775
#[derive(Copy, Clone, Default)]
@@ -153,15 +151,17 @@ enum ValueSource<'a, 'tcx> {
153151
},
154152
}
155153

154+
trait QualifIdx {
155+
const IDX: usize;
156+
}
157+
156158
/// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
157159
/// code for promotion or prevent it from evaluating at compile time. So `return true` means
158160
/// "I found something bad, no reason to go on searching". `false` is only returned if we
159161
/// definitely cannot find anything bad anywhere.
160162
///
161163
/// The default implementations proceed structurally.
162-
trait Qualif {
163-
const IDX: usize;
164-
164+
trait Qualif: QualifIdx {
165165
/// Return the qualification that is (conservatively) correct for any value
166166
/// of the type, or `None` if the qualification is not value/type-based.
167167
fn in_any_value_of_ty(_cx: &ConstCx<'_, 'tcx>, _ty: Ty<'tcx>) -> Option<bool> {
@@ -343,8 +343,6 @@ trait Qualif {
343343
struct HasMutInterior;
344344

345345
impl Qualif for HasMutInterior {
346-
const IDX: usize = 0;
347-
348346
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> Option<bool> {
349347
Some(!ty.is_freeze(cx.tcx, cx.param_env, DUMMY_SP))
350348
}
@@ -404,8 +402,6 @@ impl Qualif for HasMutInterior {
404402
struct NeedsDrop;
405403

406404
impl Qualif for NeedsDrop {
407-
const IDX: usize = 1;
408-
409405
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> Option<bool> {
410406
Some(ty.needs_drop(cx.tcx, cx.param_env))
411407
}
@@ -432,8 +428,6 @@ impl Qualif for NeedsDrop {
432428
struct IsNotPromotable;
433429

434430
impl Qualif for IsNotPromotable {
435-
const IDX: usize = 2;
436-
437431
fn in_static(cx: &ConstCx<'_, 'tcx>, static_: &Static<'tcx>) -> bool {
438432
match static_.kind {
439433
StaticKind::Promoted(_, _) => unreachable!(),
@@ -584,8 +578,6 @@ impl Qualif for IsNotPromotable {
584578
struct IsNotImplicitlyPromotable;
585579

586580
impl Qualif for IsNotImplicitlyPromotable {
587-
const IDX: usize = 3;
588-
589581
fn in_call(
590582
cx: &ConstCx<'_, 'tcx>,
591583
callee: &Operand<'tcx>,
@@ -607,21 +599,23 @@ impl Qualif for IsNotImplicitlyPromotable {
607599
}
608600

609601
// Ensure the `IDX` values are sequential (`0..QUALIF_COUNT`).
610-
macro_rules! static_assert_seq_qualifs {
602+
macro_rules! define_qualif_indices {
611603
($i:expr => $first:ident $(, $rest:ident)*) => {
612-
static_assert!({
613-
static_assert_seq_qualifs!($i + 1 => $($rest),*);
604+
impl QualifIdx for $first {
605+
const IDX: usize = $i;
606+
}
614607

615-
$first::IDX == $i
616-
});
608+
define_qualif_indices!($i + 1 => $($rest),*);
617609
};
618610
($i:expr =>) => {
619-
static_assert!(QUALIF_COUNT == $i);
611+
const QUALIF_COUNT: usize = $i;
620612
};
621613
}
622-
static_assert_seq_qualifs!(
614+
615+
define_qualif_indices!(
623616
0 => HasMutInterior, NeedsDrop, IsNotPromotable, IsNotImplicitlyPromotable
624617
);
618+
static_assert!(QUALIF_COUNT == 4);
625619

626620
impl ConstCx<'_, 'tcx> {
627621
fn qualifs_in_any_value_of_ty(&self, ty: Ty<'tcx>) -> PerQualif<bool> {

0 commit comments

Comments
 (0)