Skip to content

Commit 19fb2c7

Browse files
committed
found another place where we can eval() a const, and go through valtrees
1 parent 292d5bb commit 19fb2c7

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

compiler/rustc_middle/src/mir/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,11 @@ pub struct Constant<'tcx> {
22972297
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
22982298
#[derive(Lift, TypeFoldable, TypeVisitable)]
22992299
pub enum ConstantKind<'tcx> {
2300-
/// This constant came from the type system
2300+
/// This constant came from the type system.
2301+
///
2302+
/// Any way of turning `ty::Const` into `ConstValue` should go through `valtree_to_const_val`;
2303+
/// this ensures that we consistently produce "clean" values without data in the padding or
2304+
/// anything like that.
23012305
Ty(ty::Const<'tcx>),
23022306

23032307
/// An unevaluated mir constant which is not part of the type system.

compiler/rustc_middle/src/ty/consts/kind.rs

-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ impl rustc_errors::IntoDiagnosticArg for UnevaluatedConst<'_> {
2222
}
2323

2424
impl<'tcx> UnevaluatedConst<'tcx> {
25-
#[inline]
26-
pub fn expand(self) -> mir::UnevaluatedConst<'tcx> {
27-
mir::UnevaluatedConst { def: self.def, args: self.args, promoted: None }
28-
}
29-
3025
/// FIXME(RalfJung): I cannot explain what this does or why it makes sense, but not doing this
3126
/// hurts performance.
3227
#[inline]

compiler/rustc_monomorphize/src/collector.rs

+9-33
Original file line numberDiff line numberDiff line change
@@ -749,39 +749,15 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
749749
#[instrument(skip(self), level = "debug")]
750750
fn visit_constant(&mut self, constant: &mir::Constant<'tcx>, location: Location) {
751751
let literal = self.monomorphize(constant.literal);
752-
let val = match literal {
753-
mir::ConstantKind::Val(val, _) => val,
754-
mir::ConstantKind::Ty(ct) => match ct.kind() {
755-
ty::ConstKind::Value(val) => self.tcx.valtree_to_const_val((ct.ty(), val)),
756-
ty::ConstKind::Unevaluated(ct) => {
757-
debug!(?ct);
758-
let param_env = ty::ParamEnv::reveal_all();
759-
match self.tcx.const_eval_resolve(param_env, ct.expand(), None) {
760-
// The `monomorphize` call should have evaluated that constant already.
761-
Ok(val) => val,
762-
Err(ErrorHandled::Reported(_)) => return,
763-
Err(ErrorHandled::TooGeneric) => span_bug!(
764-
self.body.source_info(location).span,
765-
"collection encountered polymorphic constant: {:?}",
766-
literal
767-
),
768-
}
769-
}
770-
_ => return,
771-
},
772-
mir::ConstantKind::Unevaluated(uv, _) => {
773-
let param_env = ty::ParamEnv::reveal_all();
774-
match self.tcx.const_eval_resolve(param_env, uv, None) {
775-
// The `monomorphize` call should have evaluated that constant already.
776-
Ok(val) => val,
777-
Err(ErrorHandled::Reported(_)) => return,
778-
Err(ErrorHandled::TooGeneric) => span_bug!(
779-
self.body.source_info(location).span,
780-
"collection encountered polymorphic constant: {:?}",
781-
literal
782-
),
783-
}
784-
}
752+
let param_env = ty::ParamEnv::reveal_all();
753+
let val = match literal.eval(self.tcx, param_env, None) {
754+
Ok(v) => v,
755+
Err(ErrorHandled::Reported(_)) => return,
756+
Err(ErrorHandled::TooGeneric) => span_bug!(
757+
self.body.source_info(location).span,
758+
"collection encountered polymorphic constant: {:?}",
759+
literal
760+
),
785761
};
786762
collect_const_value(self.tcx, val, self.output);
787763
MirVisitor::visit_ty(self, literal.ty(), TyContext::Location(location));

0 commit comments

Comments
 (0)