Skip to content

Commit b25ec04

Browse files
authored
Rollup merge of #63766 - oli-obk:const_eval_dedup, r=zackmdavis
Remove some duplication when resolving constants
2 parents 0e8a1a4 + 7dbc4b9 commit b25ec04

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/librustc/ty/relate.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::hir::def_id::DefId;
88
use crate::ty::subst::{Kind, UnpackedKind, SubstsRef};
99
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
1010
use crate::ty::error::{ExpectedFound, TypeError};
11-
use crate::mir::interpret::{ConstValue, Scalar, GlobalId};
11+
use crate::mir::interpret::{ConstValue, Scalar};
1212
use std::rc::Rc;
1313
use std::iter;
1414
use rustc_target::spec::abi;
@@ -551,26 +551,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
551551
let tcx = relation.tcx();
552552

553553
let eagerly_eval = |x: &'tcx ty::Const<'tcx>| {
554-
if let ConstValue::Unevaluated(def_id, substs) = x.val {
555-
// FIXME(eddyb) get the right param_env.
556-
let param_env = ty::ParamEnv::empty();
557-
if !substs.has_local_value() {
558-
let instance = ty::Instance::resolve(
559-
tcx.global_tcx(),
560-
param_env,
561-
def_id,
562-
substs,
563-
);
564-
if let Some(instance) = instance {
565-
let cid = GlobalId {
566-
instance,
567-
promoted: None,
568-
};
569-
if let Ok(ct) = tcx.const_eval(param_env.and(cid)) {
570-
return ct.val;
571-
}
572-
}
573-
}
554+
if !x.val.has_local_value() {
555+
return x.eval(tcx, relation.param_env()).val;
574556
}
575557
x.val
576558
};

src/librustc/ty/sty.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -2299,23 +2299,33 @@ impl<'tcx> Const<'tcx> {
22992299
assert_eq!(self.ty, ty);
23002300
// if `ty` does not depend on generic parameters, use an empty param_env
23012301
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`.
23022313
match self.val {
2303-
// FIXME(const_generics): this doesn't work right now,
2304-
// because it tries to relate an `Infer` to a `Param`.
23052314
ConstValue::Unevaluated(did, substs) => {
23062315
// if `substs` has no unresolved components, use and empty param_env
23072316
let (param_env, substs) = param_env.with_reveal_all().and(substs).into_parts();
23082317
// 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+
};
23102322
let gid = GlobalId {
23112323
instance,
23122324
promoted: None,
23132325
};
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)
23162327
},
2317-
// otherwise just extract a `ConstValue`'s bits if possible
2318-
_ => self.val.try_to_bits(size),
2328+
_ => self,
23192329
}
23202330
}
23212331

0 commit comments

Comments
 (0)