@@ -70,8 +70,6 @@ impl fmt::Display for Mode {
70
70
}
71
71
}
72
72
73
- const QUALIF_COUNT : usize = 4 ;
74
-
75
73
// FIXME(eddyb) once we can use const generics, replace this array with
76
74
// something like `IndexVec` but for fixed-size arrays (`IndexArray`?).
77
75
#[ derive( Copy , Clone , Default ) ]
@@ -153,15 +151,17 @@ enum ValueSource<'a, 'tcx> {
153
151
} ,
154
152
}
155
153
154
+ trait QualifIdx {
155
+ const IDX : usize ;
156
+ }
157
+
156
158
/// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
157
159
/// code for promotion or prevent it from evaluating at compile time. So `return true` means
158
160
/// "I found something bad, no reason to go on searching". `false` is only returned if we
159
161
/// definitely cannot find anything bad anywhere.
160
162
///
161
163
/// The default implementations proceed structurally.
162
- trait Qualif {
163
- const IDX : usize ;
164
-
164
+ trait Qualif : QualifIdx {
165
165
/// Return the qualification that is (conservatively) correct for any value
166
166
/// of the type, or `None` if the qualification is not value/type-based.
167
167
fn in_any_value_of_ty ( _cx : & ConstCx < ' _ , ' tcx > , _ty : Ty < ' tcx > ) -> Option < bool > {
@@ -343,8 +343,6 @@ trait Qualif {
343
343
struct HasMutInterior ;
344
344
345
345
impl Qualif for HasMutInterior {
346
- const IDX : usize = 0 ;
347
-
348
346
fn in_any_value_of_ty ( cx : & ConstCx < ' _ , ' tcx > , ty : Ty < ' tcx > ) -> Option < bool > {
349
347
Some ( !ty. is_freeze ( cx. tcx , cx. param_env , DUMMY_SP ) )
350
348
}
@@ -404,8 +402,6 @@ impl Qualif for HasMutInterior {
404
402
struct NeedsDrop ;
405
403
406
404
impl Qualif for NeedsDrop {
407
- const IDX : usize = 1 ;
408
-
409
405
fn in_any_value_of_ty ( cx : & ConstCx < ' _ , ' tcx > , ty : Ty < ' tcx > ) -> Option < bool > {
410
406
Some ( ty. needs_drop ( cx. tcx , cx. param_env ) )
411
407
}
@@ -432,8 +428,6 @@ impl Qualif for NeedsDrop {
432
428
struct IsNotPromotable ;
433
429
434
430
impl Qualif for IsNotPromotable {
435
- const IDX : usize = 2 ;
436
-
437
431
fn in_static ( cx : & ConstCx < ' _ , ' tcx > , static_ : & Static < ' tcx > ) -> bool {
438
432
match static_. kind {
439
433
StaticKind :: Promoted ( _, _) => unreachable ! ( ) ,
@@ -584,8 +578,6 @@ impl Qualif for IsNotPromotable {
584
578
struct IsNotImplicitlyPromotable ;
585
579
586
580
impl Qualif for IsNotImplicitlyPromotable {
587
- const IDX : usize = 3 ;
588
-
589
581
fn in_call (
590
582
cx : & ConstCx < ' _ , ' tcx > ,
591
583
callee : & Operand < ' tcx > ,
@@ -607,21 +599,23 @@ impl Qualif for IsNotImplicitlyPromotable {
607
599
}
608
600
609
601
// Ensure the `IDX` values are sequential (`0..QUALIF_COUNT`).
610
- macro_rules! static_assert_seq_qualifs {
602
+ macro_rules! define_qualif_indices {
611
603
( $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
+ }
614
607
615
- $first:: IDX == $i
616
- } ) ;
608
+ define_qualif_indices!( $i + 1 => $( $rest) ,* ) ;
617
609
} ;
618
610
( $i: expr =>) => {
619
- static_assert! ( QUALIF_COUNT == $i) ;
611
+ const QUALIF_COUNT : usize = $i;
620
612
} ;
621
613
}
622
- static_assert_seq_qualifs ! (
614
+
615
+ define_qualif_indices ! (
623
616
0 => HasMutInterior , NeedsDrop , IsNotPromotable , IsNotImplicitlyPromotable
624
617
) ;
618
+ static_assert ! ( QUALIF_COUNT == 4 ) ;
625
619
626
620
impl ConstCx < ' _ , ' tcx > {
627
621
fn qualifs_in_any_value_of_ty ( & self , ty : Ty < ' tcx > ) -> PerQualif < bool > {
0 commit comments