Skip to content

Fix rustdoc ICE when a const is actually a type #119739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_metadata::rendered_const;
use rustc_middle::mir;
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, TyCtxt};
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, GenericParamDef, TyCtxt};
use rustc_middle::ty::{TypeVisitable, TypeVisitableExt};
use rustc_span::symbol::{kw, sym, Symbol};
use std::fmt::Write as _;
Expand Down Expand Up @@ -75,30 +75,23 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
Crate { module, external_traits: cx.external_traits.clone() }
}

pub(crate) fn ty_args_to_args<'tcx>(
fn ty_arg_to_arg<'tcx>(
cx: &mut DocContext<'tcx>,
ty_args: ty::Binder<'tcx, &'tcx [ty::GenericArg<'tcx>]>,
has_self: bool,
owner: DefId,
) -> Vec<GenericArg> {
if ty_args.skip_binder().is_empty() {
// Fast path which avoids executing the query `generics_of`.
return Vec::new();
}

let params = &cx.tcx.generics_of(owner).params;
let mut elision_has_failed_once_before = false;

let offset = if has_self { 1 } else { 0 };
let mut args = Vec::with_capacity(ty_args.skip_binder().len().saturating_sub(offset));

let ty_arg_to_arg = |(index, arg): (usize, &ty::GenericArg<'tcx>)| match arg.unpack() {
params: &[GenericParamDef],
index: usize,
arg: &ty::GenericArg<'tcx>,
elision_has_failed_once_before: &mut bool,
) -> Option<GenericArg> {
match arg.unpack() {
GenericArgKind::Lifetime(lt) => {
Some(GenericArg::Lifetime(clean_middle_region(lt).unwrap_or(Lifetime::elided())))
}
GenericArgKind::Type(_) if has_self && index == 0 => None,
GenericArgKind::Type(ty) => {
if !elision_has_failed_once_before
if !*elision_has_failed_once_before
&& let Some(default) = params[index].default_value(cx.tcx)
{
let default =
Expand All @@ -108,7 +101,7 @@ pub(crate) fn ty_args_to_args<'tcx>(
return None;
}

elision_has_failed_once_before = true;
*elision_has_failed_once_before = true;
}

Some(GenericArg::Type(clean_middle_ty(
Expand All @@ -129,24 +122,68 @@ pub(crate) fn ty_args_to_args<'tcx>(
return None;
}

if !elision_has_failed_once_before
if !*elision_has_failed_once_before
&& let Some(default) = params[index].default_value(cx.tcx)
{
match default.skip_binder().unpack() {
GenericArgKind::Const(_) => {}
GenericArgKind::Type(_) | GenericArgKind::Lifetime(_) => {
Comment on lines +128 to +130
Copy link
Member

@fmease fmease Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is treating symptoms not the root cause, we shouldn't have a mismatch between generics and ty_args in the first place. The root cause is an off-by-one error introduced by me since I forgot to handle trait object types correctly whose generics args don't contain a Self type.

I'm gonna submit an alternative fix shortly.

Your PR also doesn't fix (without double-checking):

pub trait Trait<const K: u32 = 0, T = ()> {}
pub type Alias<const K: u32> = dyn Trait<K>;
pub use dependency::*;
// ICE: expected a type, but found another kind

You'd need to update the outer GenericArgKind::Type branch, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll gladly take a look at your fix then. :)

Closing this PR then!

return ty_arg_to_arg(
cx,
ty_args,
has_self,
owner,
params,
index,
&default.skip_binder(),
elision_has_failed_once_before,
);
}
}
let default =
ty_args.map_bound(|args| default.instantiate(cx.tcx, args).expect_const());

if can_elide_generic_arg(ty_args.rebind(ct), default) {
return None;
}

elision_has_failed_once_before = true;
*elision_has_failed_once_before = true;
}

Some(GenericArg::Const(Box::new(clean_middle_const(ty_args.rebind(ct), cx))))
}
};
}
}

args.extend(ty_args.skip_binder().iter().enumerate().rev().filter_map(ty_arg_to_arg));
pub(crate) fn ty_args_to_args<'tcx>(
cx: &mut DocContext<'tcx>,
ty_args: ty::Binder<'tcx, &'tcx [ty::GenericArg<'tcx>]>,
has_self: bool,
owner: DefId,
) -> Vec<GenericArg> {
if ty_args.skip_binder().is_empty() {
// Fast path which avoids executing the query `generics_of`.
return Vec::new();
}

let params = &cx.tcx.generics_of(owner).params;
let mut elision_has_failed_once_before = false;

let offset = if has_self { 1 } else { 0 };
let mut args = Vec::with_capacity(ty_args.skip_binder().len().saturating_sub(offset));

args.extend(ty_args.skip_binder().iter().enumerate().rev().filter_map(|(index, arg)| {
ty_arg_to_arg(
cx,
ty_args,
has_self,
owner,
params,
index,
arg,
&mut elision_has_failed_once_before,
)
}));
args.reverse();
args
}
Expand Down
9 changes: 9 additions & 0 deletions tests/rustdoc/issue-119529-middle-const-ty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This is a regression test for <https://github.com/rust-lang/rust/issues/119529>.

#![crate_name = "foo"]

use std::cmp::PartialEq;

// @has 'foo/type.Dyn.html'
// @has - '//*[@class="rust item-decl"]' 'pub type Dyn<Rhs> = dyn PartialEq<Rhs>;'
pub type Dyn<Rhs> = dyn PartialEq<Rhs>;