Skip to content

Commit 30f1bab

Browse files
Rollup merge of #76581 - lcnr:bound-too-generic, r=eddyb
do not ICE on bound variables, return `TooGeneric` instead fixes #73260, fixes #74634, fixes #76595 r? @nikomatsakis
2 parents ef6c3a7 + 65b3419 commit 30f1bab

File tree

8 files changed

+128
-6
lines changed

8 files changed

+128
-6
lines changed

compiler/rustc_middle/src/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1259,11 +1259,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12591259
tcx.layout_raw(param_env.and(normalized))?
12601260
}
12611261

1262-
ty::Bound(..) | ty::Placeholder(..) | ty::GeneratorWitness(..) | ty::Infer(_) => {
1262+
ty::Placeholder(..) | ty::GeneratorWitness(..) | ty::Infer(_) => {
12631263
bug!("Layout::compute: unexpected type `{}`", ty)
12641264
}
12651265

1266-
ty::Param(_) | ty::Error(_) => {
1266+
ty::Bound(..) | ty::Param(_) | ty::Error(_) => {
12671267
return Err(LayoutError::Unknown(ty));
12681268
}
12691269
})

compiler/rustc_mir/src/interpret/operand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,13 @@ 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, substs)?;
556556
return Ok(self.eval_to_allocation(GlobalId { instance, promoted })?.into());
557557
}
558-
ty::ConstKind::Infer(..)
559-
| ty::ConstKind::Bound(..)
560-
| ty::ConstKind::Placeholder(..) => {
558+
ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => {
561559
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val)
562560
}
563561
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
struct Bool<const B: bool>;
5+
6+
trait True {}
7+
8+
impl True for Bool<true> {}
9+
10+
fn test<T, const P: usize>() where Bool<{core::mem::size_of::<T>() > 4}>: True {
11+
todo!()
12+
}
13+
14+
fn main() {
15+
test::<2>();
16+
//~^ ERROR wrong number of type
17+
//~| ERROR constant expression depends
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0107]: wrong number of type arguments: expected 1, found 0
2+
--> $DIR/issue-76595.rs:15:5
3+
|
4+
LL | test::<2>();
5+
| ^^^^^^^^^ expected 1 type argument
6+
7+
error: constant expression depends on a generic parameter
8+
--> $DIR/issue-76595.rs:15:5
9+
|
10+
LL | fn test<T, const P: usize>() where Bool<{core::mem::size_of::<T>() > 4}>: True {
11+
| ---- required by this bound in `test`
12+
...
13+
LL | test::<2>();
14+
| ^^^^^^^^^
15+
|
16+
= note: this may fail depending on what value the parameter takes
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)