Skip to content

Commit d49e7e7

Browse files
committed
Auto merge of rust-lang#103279 - compiler-errors:normalize-hack-back, r=lcnr
Add eval hack in `super_relate_consts` back Partially reverts 01adb7e. This extra eval call *still* needs to happen, for example, in `normalize_param_env_or_error` when a param-env predicate has an unnormalized constant, since the param-env candidates never get normalized during candidate assembly (everywhere else we can assume that they are normalized fully). r? `@lcnr,` though I feel like I've assigned quite a few PRs to you in the last few days, so feel free to reassign to someone else familiar with this code if you're busy! cc rust-lang#103243 (fixes the issue, but don't want to auto-close that until a backport is performed).
2 parents a5406fe + 6e6fe30 commit d49e7e7

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
554554
val: &mir::ConstantKind<'tcx>,
555555
layout: Option<TyAndLayout<'tcx>>,
556556
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
557+
// FIXME(const_prop): normalization needed b/c const prop lint in
558+
// `mir_drops_elaborated_and_const_checked`, which happens before
559+
// optimized MIR. Only after optimizing the MIR can we guarantee
560+
// that the `RevealAll` pass has happened and that the body's consts
561+
// are normalized, so any call to resolve before that needs to be
562+
// manually normalized.
563+
let val = self.tcx.normalize_erasing_regions(self.param_env, *val);
557564
match val {
558565
mir::ConstantKind::Ty(ct) => {
559566
match ct.kind() {
@@ -585,7 +592,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
585592
}
586593
}
587594
}
588-
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, *ty, layout),
595+
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(val, ty, layout),
589596
mir::ConstantKind::Unevaluated(uv, _) => {
590597
let instance = self.resolve(uv.def, uv.substs)?;
591598
Ok(self.eval_to_allocation(GlobalId { instance, promoted: uv.promoted })?.into())

compiler/rustc_middle/src/ty/relate.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
574574
/// it.
575575
pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
576576
relation: &mut R,
577-
a: ty::Const<'tcx>,
578-
b: ty::Const<'tcx>,
577+
mut a: ty::Const<'tcx>,
578+
mut b: ty::Const<'tcx>,
579579
) -> RelateResult<'tcx, ty::Const<'tcx>> {
580580
debug!("{}.super_relate_consts(a = {:?}, b = {:?})", relation.tag(), a, b);
581581
let tcx = relation.tcx();
@@ -596,6 +596,17 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
596596
);
597597
}
598598

599+
// HACK(const_generics): We still need to eagerly evaluate consts when
600+
// relating them because during `normalize_param_env_or_error`,
601+
// we may relate an evaluated constant in a obligation against
602+
// an unnormalized (i.e. unevaluated) const in the param-env.
603+
// FIXME(generic_const_exprs): Once we always lazily unify unevaluated constants
604+
// these `eval` calls can be removed.
605+
if !relation.tcx().features().generic_const_exprs {
606+
a = a.eval(tcx, relation.param_env());
607+
b = b.eval(tcx, relation.param_env());
608+
}
609+
599610
// Currently, the values that can be unified are primitive types,
600611
// and those that derive both `PartialEq` and `Eq`, corresponding
601612
// to structural-match types.

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
483483
// Use `Reveal::All` here because patterns are always monomorphic even if their function
484484
// isn't.
485485
let param_env_reveal_all = self.param_env.with_reveal_all_normalized(self.tcx);
486-
let substs = self.typeck_results.node_substs(id);
486+
// N.B. There is no guarantee that substs collected in typeck results are fully normalized,
487+
// so they need to be normalized in order to pass to `Instance::resolve`, which will ICE
488+
// if given unnormalized types.
489+
let substs = self
490+
.tcx
491+
.normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_substs(id));
487492
let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, substs) {
488493
Ok(Some(i)) => i,
489494
Ok(None) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// check-pass
2+
3+
pub trait CSpace<const N: usize> {
4+
type Traj;
5+
}
6+
7+
pub struct Const<const R: usize>;
8+
9+
pub trait Obstacle<CS, const N: usize> {
10+
fn trajectory_free<FT, S1>(&self, t: &FT)
11+
where
12+
CS::Traj: Sized,
13+
CS: CSpace<N>;
14+
}
15+
16+
// -----
17+
18+
const N: usize = 4;
19+
20+
struct ObstacleSpace2df32;
21+
22+
impl<CS> Obstacle<CS, N> for ObstacleSpace2df32 {
23+
fn trajectory_free<TF, S1>(&self, t: &TF)
24+
where
25+
CS::Traj: Sized,
26+
CS: CSpace<N>,
27+
{
28+
}
29+
}
30+
31+
fn main() {}

0 commit comments

Comments
 (0)