Skip to content

Commit a616db3

Browse files
committed
rustdoc: Avoid ICE on unevaluated type const projections in array lengths
1 parent 3daae5e commit a616db3

4 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/librustdoc/clean/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,10 @@ pub(crate) fn clean_middle_ty<'tcx>(
21522152
format!("{pat:?}").into_boxed_str(),
21532153
),
21542154
ty::Array(ty, n) => {
2155-
let n = cx.tcx.normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(n));
2155+
let n = cx
2156+
.tcx
2157+
.try_normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(n))
2158+
.unwrap_or(n);
21562159
let n = print_const(cx.tcx, n);
21572160
Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into())
21582161
}

src/librustdoc/clean/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,16 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
351351
pub(crate) fn print_const(tcx: TyCtxt<'_>, n: ty::Const<'_>) -> String {
352352
match n.kind() {
353353
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { kind, .. }) => match kind {
354-
ty::UnevaluatedConstKind::Projection { def_id }
355-
| ty::UnevaluatedConstKind::Inherent { def_id }
354+
ty::UnevaluatedConstKind::Projection { def_id } => {
355+
if let Some(local_def_id) = def_id.as_local()
356+
&& let Some(body_id) = tcx.hir_maybe_body_owned_by(local_def_id)
357+
{
358+
rendered_const(tcx, body_id, local_def_id)
359+
} else {
360+
n.to_string()
361+
}
362+
}
363+
ty::UnevaluatedConstKind::Inherent { def_id }
356364
| ty::UnevaluatedConstKind::Free { def_id }
357365
| ty::UnevaluatedConstKind::Anon { def_id } => {
358366
if let Some(local_def_id) = def_id.as_local()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/149287>
2+
//! and <https://github.com/rust-lang/rust/issues/158155>
3+
4+
#![crate_name = "foo"]
5+
#![feature(min_generic_const_args)]
6+
#![expect(incomplete_features)]
7+
8+
pub trait Tr {
9+
type const SIZE: usize;
10+
}
11+
12+
//@ has 'foo/fn.mk_array.html'
13+
//@ has - '//pre[@class="rust item-decl"]/code' '[(); <T as Tr>::SIZE]'
14+
pub fn mk_array<T: Tr>() -> [(); <T as Tr>::SIZE] {
15+
[(); T::SIZE]
16+
}
17+
18+
//@ has 'foo/type.Arr.html'
19+
//@ has - '//pre[@class="rust item-decl"]/code' '[(); <T as Tr>::SIZE]'
20+
pub type Arr<T> = [(); <T as Tr>::SIZE];

tests/rustdoc-ui/type-const-associated-const-no-body.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)