Skip to content

Commit 0c11b93

Browse files
committed
Auto merge of #79635 - lcnr:const-eval-idk, r=oli-obk
const_evaluatable_checked: fix occurs check fixes #79615 this is kind of a hack because we use `TypeRelation` for both the `Generalizer` and the `ConstInferUnifier` but i am not sure if there is a useful way to disentangle this without unnecessarily duplicating some code. The error in the added test is kind of unavoidable until we erase the unused substs of `ConstKind::Unevaluated`. We talked a bit about this in the cg lazy norm meeting (https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/lazy_normalization_consts)
2 parents c1d5843 + 806c728 commit 0c11b93

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Diff for: compiler/rustc_infer/src/infer/combine.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
543543
true
544544
}
545545

546+
fn visit_ct_substs(&self) -> bool {
547+
true
548+
}
549+
546550
fn binders<T>(
547551
&mut self,
548552
a: ty::Binder<T>,
@@ -716,7 +720,10 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
716720
let variable_table = &mut inner.const_unification_table();
717721
let var_value = variable_table.probe_value(vid);
718722
match var_value.val {
719-
ConstVariableValue::Known { value: u } => self.relate(u, u),
723+
ConstVariableValue::Known { value: u } => {
724+
drop(inner);
725+
self.relate(u, u)
726+
}
720727
ConstVariableValue::Unknown { universe } => {
721728
if self.for_universe.can_name(universe) {
722729
Ok(c)
@@ -815,6 +822,10 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
815822
true
816823
}
817824

825+
fn visit_ct_substs(&self) -> bool {
826+
true
827+
}
828+
818829
fn relate_with_variance<T: Relate<'tcx>>(
819830
&mut self,
820831
_variance: ty::Variance,
@@ -870,6 +881,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
870881
}
871882
}
872883
}
884+
ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => Ok(t),
873885
_ => relate::super_relate_tys(self, t, t),
874886
}
875887
}

Diff for: compiler/rustc_middle/src/ty/relate.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ pub trait TypeRelation<'tcx>: Sized {
3333
/// relation. Just affects error messages.
3434
fn a_is_expected(&self) -> bool;
3535

36+
/// Whether we should look into the substs of unevaluated constants
37+
/// even if `feature(const_evaluatable_checked)` is active.
38+
///
39+
/// This is needed in `combine` to prevent accidentially creating
40+
/// infinite types as we abuse `TypeRelation` to walk a type there.
41+
fn visit_ct_substs(&self) -> bool {
42+
false
43+
}
44+
3645
fn with_cause<F, R>(&mut self, _cause: Cause, f: F) -> R
3746
where
3847
F: FnOnce(&mut Self) -> R,
@@ -579,7 +588,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
579588
(
580589
ty::ConstKind::Unevaluated(a_def, a_substs, None),
581590
ty::ConstKind::Unevaluated(b_def, b_substs, None),
582-
) if tcx.features().const_evaluatable_checked => {
591+
) if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() => {
583592
if tcx.try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs))) {
584593
Ok(a.val)
585594
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
// `N + 1` also depends on `T` here even if it doesn't use it.
5+
fn q<T, const N: usize>(_: T) -> [u8; N + 1] {
6+
todo!()
7+
}
8+
9+
fn supplier<T>() -> T {
10+
todo!()
11+
}
12+
13+
fn catch_me<const N: usize>() where [u8; N + 1]: Default {
14+
let mut x = supplier();
15+
x = q::<_, N>(x); //~ ERROR mismatched types
16+
}
17+
18+
fn main() {
19+
catch_me::<3>();
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unused-substs-5.rs:15:9
3+
|
4+
LL | x = q::<_, N>(x);
5+
| ^^^^^^^^^^^^
6+
| |
7+
| cyclic type of infinite size
8+
| help: try using a conversion method: `q::<_, N>(x).to_vec()`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)