File tree Expand file tree Collapse file tree 2 files changed +38
-6
lines changed Expand file tree Collapse file tree 2 files changed +38
-6
lines changed Original file line number Diff line number Diff line change @@ -638,21 +638,41 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
638
638
let res = self . typeck . qpath_res ( qpath, id) ;
639
639
match res {
640
640
Res :: Def ( DefKind :: Const | DefKind :: AssocConst , def_id) => {
641
- // Check if this constant is based on `cfg!(..)`,
642
- // which is NOT constant for our purposes.
643
641
if let Some ( node) = self . tcx . hir_get_if_local ( def_id)
644
642
&& let Node :: Item ( Item {
645
643
kind : ItemKind :: Const ( .., body_id) ,
644
+ owner_id,
646
645
..
647
646
} ) = node
648
- && let Node :: Expr ( Expr {
647
+ {
648
+ // check for `const C: _ = cfg!(foo);`
649
+ if let Node :: Expr ( Expr {
649
650
kind : ExprKind :: Lit ( _) ,
650
651
span,
651
652
..
652
653
} ) = self . tcx . hir_node ( body_id. hir_id )
653
- && is_direct_expn_of ( * span, sym:: cfg) . is_some ( )
654
- {
655
- return None ;
654
+ && is_direct_expn_of ( * span, sym:: cfg) . is_some ( )
655
+ {
656
+ return None ;
657
+ }
658
+
659
+ // check for:
660
+ // ```
661
+ // #[cfg(foo)]
662
+ // const C: _ = _;
663
+ // ```
664
+ //
665
+ // NOTE: it shouldn't be possible to have `#[cfg]` applied to the initializer, because e.g.
666
+ // something like this wouldn't work:
667
+ // const C: _ = {
668
+ // #[cfg(foo)]
669
+ // 1
670
+ // #[cfg(bar)]
671
+ // 2
672
+ // };
673
+ if self . tcx . has_attr ( owner_id. def_id , sym:: cfg_trace) {
674
+ return None ;
675
+ }
656
676
}
657
677
658
678
let args = self . typeck . node_args ( id) ;
Original file line number Diff line number Diff line change @@ -86,3 +86,15 @@ fn ineffective() {
86
86
x | 3 > 4 ; // not an error (yet), better written as x >= 4
87
87
x | 4 <= 19 ;
88
88
}
89
+
90
+ fn issue14167 ( ) {
91
+ #[ cfg( test) ]
92
+ const FORCE_DYNAMIC_DETECTION : u8 = 0b00010000 ;
93
+
94
+ #[ cfg( not( test) ) ]
95
+ const FORCE_DYNAMIC_DETECTION : u8 = 0b0000000 ;
96
+
97
+ const CAPS_STATIC : u8 = 0b00001111 ;
98
+
99
+ CAPS_STATIC & FORCE_DYNAMIC_DETECTION == 0 ;
100
+ }
You can’t perform that action at this time.
0 commit comments