@@ -52,6 +52,9 @@ pub struct Inherited<'a, 'tcx> {
5252 pub ( super ) deferred_generator_interiors :
5353 RefCell < Vec < ( hir:: BodyId , Ty < ' tcx > , hir:: GeneratorKind ) > > ,
5454
55+ /// Reports whether this is in a const context.
56+ pub ( super ) constness : hir:: Constness ,
57+
5558 pub ( super ) body_id : Option < hir:: BodyId > ,
5659}
5760
@@ -93,6 +96,12 @@ impl<'tcx> InheritedBuilder<'tcx> {
9396
9497impl Inherited < ' a , ' tcx > {
9598 pub ( super ) fn new ( infcx : InferCtxt < ' a , ' tcx > , def_id : LocalDefId ) -> Self {
99+ let tcx = infcx. tcx ;
100+ let item_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
101+ Self :: with_constness ( infcx, def_id, tcx. hir ( ) . get ( item_id) . constness ( ) )
102+ }
103+
104+ pub ( super ) fn with_constness ( infcx : InferCtxt < ' a , ' tcx > , def_id : LocalDefId , constness : hir:: Constness ) -> Self {
96105 let tcx = infcx. tcx ;
97106 let item_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
98107 let body_id = tcx. hir ( ) . maybe_body_owned_by ( item_id) ;
@@ -108,12 +117,29 @@ impl Inherited<'a, 'tcx> {
108117 deferred_call_resolutions : RefCell :: new ( Default :: default ( ) ) ,
109118 deferred_cast_checks : RefCell :: new ( Vec :: new ( ) ) ,
110119 deferred_generator_interiors : RefCell :: new ( Vec :: new ( ) ) ,
120+ constness,
111121 body_id,
112122 }
113123 }
114124
115- pub ( super ) fn register_predicate ( & self , obligation : traits:: PredicateObligation < ' tcx > ) {
125+ #[ instrument( level = "debug" , skip( self ) ) ]
126+ fn transform_predicate ( & self , p : & mut ty:: Predicate < ' tcx > ) {
127+ // Don't transform non-const bounds into const bounds,
128+ // but transform const bounds to non-const when we are
129+ // not in a const context.
130+ if let hir:: Constness :: NotConst = self . constness {
131+ let kind = p. kind ( ) ;
132+ if let ty:: PredicateKind :: Trait ( pred) = kind. as_ref ( ) . skip_binder ( ) {
133+ let mut pred = * pred;
134+ pred. constness = hir:: Constness :: NotConst ;
135+ * p = kind. rebind ( ty:: PredicateKind :: Trait ( pred) ) . to_predicate ( self . tcx ) ;
136+ }
137+ }
138+ }
139+
140+ pub ( super ) fn register_predicate ( & self , mut obligation : traits:: PredicateObligation < ' tcx > ) {
116141 debug ! ( "register_predicate({:?})" , obligation) ;
142+ self . transform_predicate ( & mut obligation. predicate ) ;
117143 if obligation. has_escaping_bound_vars ( ) {
118144 span_bug ! ( obligation. cause. span, "escaping bound vars in predicate {:?}" , obligation) ;
119145 }
0 commit comments