@@ -2299,23 +2299,33 @@ impl<'tcx> Const<'tcx> {
2299
2299
assert_eq ! ( self . ty, ty) ;
2300
2300
// if `ty` does not depend on generic parameters, use an empty param_env
2301
2301
let size = tcx. layout_of ( param_env. with_reveal_all ( ) . and ( ty) ) . ok ( ) ?. size ;
2302
+ self . eval ( tcx, param_env) . val . try_to_bits ( size)
2303
+ }
2304
+
2305
+ #[ inline]
2306
+ pub fn eval (
2307
+ & self ,
2308
+ tcx : TyCtxt < ' tcx > ,
2309
+ param_env : ParamEnv < ' tcx > ,
2310
+ ) -> & Const < ' tcx > {
2311
+ // FIXME(const_generics): this doesn't work right now,
2312
+ // because it tries to relate an `Infer` to a `Param`.
2302
2313
match self . val {
2303
- // FIXME(const_generics): this doesn't work right now,
2304
- // because it tries to relate an `Infer` to a `Param`.
2305
2314
ConstValue :: Unevaluated ( did, substs) => {
2306
2315
// if `substs` has no unresolved components, use and empty param_env
2307
2316
let ( param_env, substs) = param_env. with_reveal_all ( ) . and ( substs) . into_parts ( ) ;
2308
2317
// try to resolve e.g. associated constants to their definition on an impl
2309
- let instance = ty:: Instance :: resolve ( tcx, param_env, did, substs) ?;
2318
+ let instance = match ty:: Instance :: resolve ( tcx, param_env, did, substs) {
2319
+ Some ( instance) => instance,
2320
+ None => return self ,
2321
+ } ;
2310
2322
let gid = GlobalId {
2311
2323
instance,
2312
2324
promoted : None ,
2313
2325
} ;
2314
- let evaluated = tcx. const_eval ( param_env. and ( gid) ) . ok ( ) ?;
2315
- evaluated. val . try_to_bits ( size)
2326
+ tcx. const_eval ( param_env. and ( gid) ) . unwrap_or ( self )
2316
2327
} ,
2317
- // otherwise just extract a `ConstValue`'s bits if possible
2318
- _ => self . val . try_to_bits ( size) ,
2328
+ _ => self ,
2319
2329
}
2320
2330
}
2321
2331
0 commit comments