@@ -2,7 +2,7 @@ use super::Const;
22use crate :: mir;
33use crate :: ty:: abstract_const:: CastKind ;
44use crate :: ty:: GenericArgsRef ;
5- use crate :: ty:: { self , List , Ty } ;
5+ use crate :: ty:: { self , visit :: TypeVisitableExt as _ , List , Ty , TyCtxt } ;
66use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
77use rustc_hir:: def_id:: DefId ;
88use rustc_macros:: HashStable ;
@@ -26,6 +26,39 @@ impl<'tcx> UnevaluatedConst<'tcx> {
2626 pub fn expand ( self ) -> mir:: UnevaluatedConst < ' tcx > {
2727 mir:: UnevaluatedConst { def : self . def , args : self . args , promoted : None }
2828 }
29+
30+ /// FIXME(RalfJung): I cannot explain what this does or why it makes sense, but not doing this
31+ /// hurts performance.
32+ #[ inline]
33+ pub ( crate ) fn prepare_for_eval (
34+ self ,
35+ tcx : TyCtxt < ' tcx > ,
36+ param_env : ty:: ParamEnv < ' tcx > ,
37+ ) -> ( ty:: ParamEnv < ' tcx > , Self ) {
38+ // HACK(eddyb) this erases lifetimes even though `const_eval_resolve`
39+ // also does later, but we want to do it before checking for
40+ // inference variables.
41+ // Note that we erase regions *before* calling `with_reveal_all_normalized`,
42+ // so that we don't try to invoke this query with
43+ // any region variables.
44+
45+ // HACK(eddyb) when the query key would contain inference variables,
46+ // attempt using identity args and `ParamEnv` instead, that will succeed
47+ // when the expression doesn't depend on any parameters.
48+ // FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
49+ // we can call `infcx.const_eval_resolve` which handles inference variables.
50+ if ( param_env, self ) . has_non_region_infer ( ) {
51+ (
52+ tcx. param_env ( self . def ) ,
53+ ty:: UnevaluatedConst {
54+ def : self . def ,
55+ args : ty:: GenericArgs :: identity_for_item ( tcx, self . def ) ,
56+ } ,
57+ )
58+ } else {
59+ ( tcx. erase_regions ( param_env) . with_reveal_all_normalized ( tcx) , tcx. erase_regions ( self ) )
60+ }
61+ }
2962}
3063
3164impl < ' tcx > UnevaluatedConst < ' tcx > {
0 commit comments