Skip to content

Commit 3587bf8

Browse files
committed
trait_selection: fix type-const eval ICE
1 parent d34f1f9 commit 3587bf8

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,25 @@ pub fn try_evaluate_const<'tcx>(
720720
}
721721
};
722722

723+
if tcx.is_type_const(uv.def) {
724+
match ty::Instance::try_resolve(tcx, typing_env, uv.def, args) {
725+
Ok(Some(instance)) => {
726+
let ct =
727+
tcx.const_of_item(instance.def_id()).instantiate(tcx, instance.args);
728+
return try_evaluate_const(infcx, ct, param_env);
729+
}
730+
Ok(None) => return Err(EvaluateConstErr::HasGenericsOrInfers),
731+
Err(err) => {
732+
return Err(EvaluateConstErr::EvaluationFailure(
733+
rustc_middle::mir::interpret::ReportedErrorInfo::non_const_eval_error(
734+
err,
735+
)
736+
.into(),
737+
));
738+
}
739+
}
740+
}
741+
723742
let uv = ty::UnevaluatedConst::new(uv.def, args);
724743
let erased_uv = tcx.erase_and_anonymize_regions(uv);
725744

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ compile-flags: -Znext-solver=globally
2+
#![feature(generic_const_items, min_generic_const_args)]
3+
#![feature(adt_const_params)]
4+
#![allow(incomplete_features)]
5+
#![allow(dead_code)]
6+
7+
trait Owner: NewTrait {
8+
#[type_const]
9+
const C<const N: u32>: u32;
10+
#[type_const]
11+
const K<const N: u32>: u32;
12+
}
13+
14+
trait NewTrait {}
15+
16+
impl NewTrait for () {}
17+
18+
impl Owner for () {
19+
#[type_const]
20+
const C<const N: u32>: u32 = N;
21+
#[type_const]
22+
const K<const N: u32>: u32 = const { 99 + 1 };
23+
}
24+
25+
fn take0<const N: u32>(_: impl Owner<C<N> = { N }>) {}
26+
fn take1(_: impl Owner<K<99> = 100>) {}
27+
28+
#[derive(PartialEq, Eq, std::marker::ConstParamTy)]
29+
enum Maybe<T> {
30+
Nothing,
31+
Just(T),
32+
}
33+
34+
fn main() {
35+
take0::<128>(());
36+
take1(());
37+
//~^ ERROR type mismatch resolving `const { 99 + 1 } == 100`
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0271]: type mismatch resolving `const { 99 + 1 } == 100`
2+
--> $DIR/type-const-ice-issue-151631.rs:36:11
3+
|
4+
LL | take1(());
5+
| ----- ^^ types differ
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
note: required by a bound in `take1`
10+
--> $DIR/type-const-ice-issue-151631.rs:26:24
11+
|
12+
LL | fn take1(_: impl Owner<K<99> = 100>) {}
13+
| ^^^^^^^^^^^ required by this bound in `take1`
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0271`.

0 commit comments

Comments
 (0)