Skip to content

Commit 2a00dda

Browse files
committed
miri: correctly deal with ConstKind::Bound
1 parent 255a4c5 commit 2a00dda

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

compiler/rustc_mir/src/interpret/operand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
549549
};
550550
// Early-return cases.
551551
let val_val = match val.val {
552-
ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
552+
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
553553
ty::ConstKind::Error(_) => throw_inval!(TypeckError(ErrorReported)),
554554
ty::ConstKind::Unevaluated(def, substs, promoted) => {
555555
let instance = self.resolve(def.did, substs)?;
@@ -561,9 +561,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
561561
// happening.
562562
return Ok(self.const_eval(GlobalId { instance, promoted }, val.ty)?);
563563
}
564-
ty::ConstKind::Infer(..)
565-
| ty::ConstKind::Bound(..)
566-
| ty::ConstKind::Placeholder(..) => {
564+
ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => {
567565
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val)
568566
}
569567
ty::ConstKind::Value(val_val) => val_val,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -Zsave-analysis
2+
3+
#![feature(const_generics)]
4+
#![allow(incomplete_features)]
5+
struct Arr<const N: usize>
6+
where Assert::<{N < usize::max_value() / 2}>: IsTrue, //~ ERROR constant expression
7+
{
8+
}
9+
10+
enum Assert<const CHECK: bool> {}
11+
12+
trait IsTrue {}
13+
14+
impl IsTrue for Assert<true> {}
15+
16+
fn main() {
17+
let x: Arr<{usize::max_value()}> = Arr {};
18+
//~^ ERROR mismatched types
19+
//~| ERROR mismatched types
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-73260.rs:6:47
3+
|
4+
LL | where Assert::<{N < usize::max_value() / 2}>: IsTrue,
5+
| ^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/issue-73260.rs:17:12
11+
|
12+
LL | let x: Arr<{usize::max_value()}> = Arr {};
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `false`, found `true`
14+
|
15+
= note: expected type `false`
16+
found type `true`
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/issue-73260.rs:17:40
20+
|
21+
LL | let x: Arr<{usize::max_value()}> = Arr {};
22+
| ^^^ expected `false`, found `true`
23+
|
24+
= note: expected type `false`
25+
found type `true`
26+
27+
error: aborting due to 3 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
trait If<const COND: bool> {}
5+
impl If<true> for () {}
6+
7+
trait IsZero<const N: u8> {
8+
type Answer;
9+
}
10+
11+
struct True;
12+
struct False;
13+
14+
impl<const N: u8> IsZero<N> for ()
15+
where (): If<{N == 0}> { //~ERROR constant expression
16+
type Answer = True;
17+
}
18+
19+
trait Foobar<const N: u8> {}
20+
21+
impl<const N: u8> Foobar<N> for ()
22+
where (): IsZero<N, Answer = True> {}
23+
24+
impl<const N: u8> Foobar<N> for ()
25+
where (): IsZero<N, Answer = False> {}
26+
27+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-74634.rs:15:11
3+
|
4+
LL | where (): If<{N == 0}> {
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)