Skip to content

Commit a825e37

Browse files
author
Lukas Markeffsky
committed
layout_of: put back not-so-unreachable case
1 parent 71e06b9 commit a825e37

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

compiler/rustc_ty_utils/src/layout.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,25 @@ fn extract_const_value<'tcx>(
147147
) -> Result<ty::Value<'tcx>, &'tcx LayoutError<'tcx>> {
148148
match ct.kind() {
149149
ty::ConstKind::Value(cv) => Ok(cv),
150-
ty::ConstKind::Param(_) | ty::ConstKind::Expr(_) | ty::ConstKind::Unevaluated(_) => {
150+
ty::ConstKind::Param(_) | ty::ConstKind::Expr(_) => {
151151
if !ct.has_param() {
152152
bug!("failed to normalize const, but it is not generic: {ct:?}");
153153
}
154154
Err(error(cx, LayoutError::TooGeneric(ty)))
155155
}
156+
ty::ConstKind::Unevaluated(_) => {
157+
let err = if ct.has_param() {
158+
LayoutError::TooGeneric(ty)
159+
} else {
160+
// This case is reachable with unsatisfiable predicates and GCE (which will
161+
// cause anon consts to inherit the unsatisfiable predicates). For example
162+
// if we have an unsatisfiable `u8: Trait` bound, then it's not a compile
163+
// error to mention `[u8; <u8 as Trait>::CONST]`, but we can't compute its
164+
// layout.
165+
LayoutError::Unknown(ty)
166+
};
167+
Err(error(cx, err))
168+
}
156169
ty::ConstKind::Infer(_)
157170
| ty::ConstKind::Bound(..)
158171
| ty::ConstKind::Placeholder(_)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! With `feature(generic_const_exprs)`, anon consts (e.g. length in array types) will
2+
//! inherit their parent's predicates. When combined with `feature(trivial_bounds)`, it
3+
//! is possible to have an unevaluated constant that is rigid, but not generic.
4+
//!
5+
//! This is what happens below: `u8: A` does not hold in the global environment, but
6+
//! with trivial bounds + GCE it it possible that `<u8 as A>::B` can appear in an array
7+
//! length without causing a compile error. This constant is *rigid* (i.e. it cannot be
8+
//! normalized further), but it is *not generic* (i.e. it does not depend on any generic
9+
//! parameters).
10+
//!
11+
//! This test ensures that we do not ICE in layout computation when encountering such a
12+
//! constant.
13+
14+
#![feature(rustc_attrs)]
15+
#![feature(generic_const_exprs)] //~ WARNING: the feature `generic_const_exprs` is incomplete
16+
#![feature(trivial_bounds)]
17+
18+
#![crate_type = "lib"]
19+
20+
trait A {
21+
const B: usize;
22+
}
23+
24+
#[rustc_layout(debug)]
25+
struct S([u8; <u8 as A>::B]) //~ ERROR: the type `[u8; <u8 as A>::B]` has an unknown layout
26+
where
27+
u8: A;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/gce-rigid-const-in-array-len.rs:15:12
3+
|
4+
LL | #![feature(generic_const_exprs)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error: the type `[u8; <u8 as A>::B]` has an unknown layout
11+
--> $DIR/gce-rigid-const-in-array-len.rs:25:1
12+
|
13+
LL | struct S([u8; <u8 as A>::B])
14+
| ^^^^^^^^
15+
16+
error: aborting due to 1 previous error; 1 warning emitted
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/137308>.
2+
//!
3+
//! This used to ICE in layout computation, because `<u8 as A>::B` fails to normalize
4+
//! due to the unconstrained param on the impl.
5+
6+
#![feature(rustc_attrs)]
7+
#![crate_type = "lib"]
8+
9+
trait A {
10+
const B: usize;
11+
}
12+
13+
impl<C: ?Sized> A for u8 { //~ ERROR: the type parameter `C` is not constrained
14+
const B: usize = 42;
15+
}
16+
17+
#[rustc_layout(debug)]
18+
struct S([u8; <u8 as A>::B]); //~ ERROR: the type `[u8; <u8 as A>::B]` has an unknown layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0207]: the type parameter `C` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/unconstrained-param-ice-137308.rs:13:6
3+
|
4+
LL | impl<C: ?Sized> A for u8 {
5+
| ^ unconstrained type parameter
6+
7+
error: the type `[u8; <u8 as A>::B]` has an unknown layout
8+
--> $DIR/unconstrained-param-ice-137308.rs:18:1
9+
|
10+
LL | struct S([u8; <u8 as A>::B]);
11+
| ^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)