Skip to content

Commit 520c9a2

Browse files
authored
Rollup merge of #81351 - lcnr:big-money-big-prices, r=oli-obk
combine: stop eagerly evaluating consts `super_relate_consts` eagerly evaluates constants which doesn't seem too great. I now also finally understand why all of the unused substs test passed. The reason being that we just evaluated the constants in `super_relate_consts` 😆 While this change isn't strictly necessary as evaluating consts here doesn't hurt, it still feels a lot cleaner to do it this way r? `@oli-obk` `@nikomatsakis`
2 parents afaf33d + e461ddd commit 520c9a2

14 files changed

+109
-30
lines changed

compiler/rustc_infer/src/infer/combine.rs

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

546-
fn visit_ct_substs(&self) -> bool {
547-
true
548-
}
549-
550546
fn binders<T>(
551547
&mut self,
552548
a: ty::Binder<T>,
@@ -737,6 +733,16 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
737733
}
738734
}
739735
}
736+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
737+
if self.tcx().lazy_normalization() =>
738+
{
739+
assert_eq!(promoted, None);
740+
let substs = self.relate_with_variance(ty::Variance::Invariant, substs, substs)?;
741+
Ok(self.tcx().mk_const(ty::Const {
742+
ty: c.ty,
743+
val: ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }),
744+
}))
745+
}
740746
_ => relate::super_relate_consts(self, c, c),
741747
}
742748
}
@@ -822,10 +828,6 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
822828
true
823829
}
824830

825-
fn visit_ct_substs(&self) -> bool {
826-
true
827-
}
828-
829831
fn relate_with_variance<T: Relate<'tcx>>(
830832
&mut self,
831833
_variance: ty::Variance,
@@ -959,6 +961,16 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
959961
}
960962
}
961963
}
964+
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
965+
if self.tcx().lazy_normalization() =>
966+
{
967+
assert_eq!(promoted, None);
968+
let substs = self.relate_with_variance(ty::Variance::Invariant, substs, substs)?;
969+
Ok(self.tcx().mk_const(ty::Const {
970+
ty: c.ty,
971+
val: ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }),
972+
}))
973+
}
962974
_ => relate::super_relate_consts(self, c, c),
963975
}
964976
}

compiler/rustc_middle/src/ty/relate.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ 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-
4536
fn with_cause<F, R>(&mut self, _cause: Cause, f: F) -> R
4637
where
4738
F: FnOnce(&mut Self) -> R,
@@ -432,9 +423,9 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
432423
let sz_a = sz_a.try_eval_usize(tcx, relation.param_env());
433424
let sz_b = sz_b.try_eval_usize(tcx, relation.param_env());
434425
match (sz_a, sz_b) {
435-
(Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize(
436-
expected_found(relation, sz_a_val, sz_b_val),
437-
)),
426+
(Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => Err(
427+
TypeError::FixedArraySize(expected_found(relation, sz_a_val, sz_b_val)),
428+
),
438429
_ => Err(err),
439430
}
440431
}
@@ -532,7 +523,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
532523
}
533524

534525
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
535-
if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() =>
526+
if tcx.features().const_evaluatable_checked =>
536527
{
537528
tcx.try_unify_abstract_consts(((au.def, au.substs), (bu.def, bu.substs)))
538529
}

src/test/ui/const-generics/issues/issue-69654-run-pass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// run-pass
21
#![feature(const_generics)]
32
#![allow(incomplete_features, unused_braces)]
43

@@ -15,4 +14,5 @@ where
1514

1615
fn main() {
1716
Foo::foo();
17+
//~^ ERROR the function or associated item
1818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0599]: the function or associated item `foo` exists for struct `Foo<{_: usize}>`, but its trait bounds were not satisfied
2+
--> $DIR/issue-69654-run-pass.rs:16:10
3+
|
4+
LL | struct Foo<const N: usize> {}
5+
| -------------------------- function or associated item `foo` not found for this
6+
...
7+
LL | Foo::foo();
8+
| ^^^ function or associated item cannot be called on `Foo<{_: usize}>` due to unsatisfied trait bounds
9+
|
10+
= note: the following trait bounds were not satisfied:
11+
`[u8; _]: Bar<[(); _]>`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0599`.

src/test/ui/const-generics/issues/issue-69654.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ where
1515

1616
fn main() {
1717
Foo::foo();
18+
//~^ ERROR the function or associated item
1819
}

src/test/ui/const-generics/issues/issue-69654.stderr

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ error[E0423]: expected value, found type parameter `T`
44
LL | impl<T> Bar<T> for [u8; T] {}
55
| ^ not a value
66

7-
error: aborting due to previous error
7+
error[E0599]: the function or associated item `foo` exists for struct `Foo<{_: usize}>`, but its trait bounds were not satisfied
8+
--> $DIR/issue-69654.rs:17:10
9+
|
10+
LL | struct Foo<const N: usize> {}
11+
| -------------------------- function or associated item `foo` not found for this
12+
...
13+
LL | Foo::foo();
14+
| ^^^ function or associated item cannot be called on `Foo<{_: usize}>` due to unsatisfied trait bounds
15+
|
16+
= note: the following trait bounds were not satisfied:
17+
`[u8; _]: Bar<[(); _]>`
18+
19+
error: aborting due to 2 previous errors
820

9-
For more information about this error, try `rustc --explain E0423`.
21+
Some errors have detailed explanations: E0423, E0599.
22+
For more information about an error, try `rustc --explain E0423`.

src/test/ui/const-generics/occurs-check/unused-substs-1.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// build-pass
21
#![feature(const_generics)]
32
#![allow(incomplete_features)]
43

@@ -10,5 +9,5 @@ where
109
A<N>: Bar<N>;
1110

1211
fn main() {
13-
let _ = A;
12+
let _ = A; //~ERROR the trait bound
1413
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: the trait bound `A<{_: usize}>: Bar<{_: usize}>` is not satisfied
2+
--> $DIR/unused-substs-1.rs:12:13
3+
|
4+
LL | / struct A<const N: usize>
5+
LL | | where
6+
LL | | A<N>: Bar<N>;
7+
| |_________________- required by `A`
8+
...
9+
LL | let _ = A;
10+
| ^ the trait `Bar<{_: usize}>` is not implemented for `A<{_: usize}>`
11+
|
12+
= help: the following implementations were found:
13+
<A<7_usize> as Bar<N>>
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/const-generics/occurs-check/unused-substs-2.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// check-pass
21
#![feature(const_generics)]
32
#![allow(incomplete_features)]
43

@@ -24,4 +23,6 @@ fn main() {
2423
// `t` is `ty::Infer(TyVar(_#1t))`
2524
// `foo` contains `ty::Infer(TyVar(_#1t))` in its substs
2625
t = foo;
26+
//~^ ERROR mismatched types
27+
//~| NOTE cyclic type
2728
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unused-substs-2.rs:25:9
3+
|
4+
LL | t = foo;
5+
| ^^^ cyclic type of infinite size
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/const-generics/occurs-check/unused-substs-3.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// check-pass
21
#![feature(const_generics)]
32
#![allow(incomplete_features)]
43

@@ -15,4 +14,6 @@ fn main() {
1514
// `t` is `ty::Infer(TyVar(_#1t))`
1615
// `foo` contains `ty::Infer(TyVar(_#1t))` in its substs
1716
t = foo;
17+
//~^ ERROR mismatched types
18+
//~| NOTE cyclic type
1819
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unused-substs-3.rs:16:9
3+
|
4+
LL | t = foo;
5+
| ^^^
6+
| |
7+
| cyclic type of infinite size
8+
| help: try using a conversion method: `foo.to_vec()`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// build-pass
21
#![feature(const_generics)]
32
#![allow(incomplete_features)]
43

@@ -8,5 +7,5 @@ fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
87

98
fn main() {
109
let mut arr = Default::default();
11-
arr = bind(arr);
10+
arr = bind(arr); //~ ERROR mismatched type
1211
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unused-substs-4.rs:10:11
3+
|
4+
LL | arr = bind(arr);
5+
| ^^^^^^^^^ encountered a self-referencing constant
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)