Skip to content

rustdoc: Don't panic on ambiguous inherent associated types #88573

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

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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
10 changes: 6 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,10 +1311,11 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
}
hir::QPath::TypeRelative(ref qself, ref segment) => {
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
let res = if let ty::Projection(proj) = ty.kind() {
Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id)
} else {
Res::Err
let res = match ty.kind() {
ty::Projection(proj) => Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id),
// Rustdoc handles `ty::Error`s by turning them into `Type::Infer`s.
ty::Error(_) => return Type::Infer,
_ => bug!("clean: expected associated type, found `{:?}`", ty),
};
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
Type::QPath {
Expand Down Expand Up @@ -1379,6 +1380,7 @@ impl Clean<Type> for hir::Ty<'_> {
DynTrait(bounds, lifetime)
}
TyKind::BareFn(ref barefn) => BareFunction(Box::new(barefn.clean(cx))),
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
TyKind::Infer | TyKind::Err => Infer,
TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.kind),
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This test ensures that rustdoc does not panic on inherented associated types
// that are referred to without fully-qualified syntax.

#![feature(inherent_associated_types)]
#![allow(incomplete_features)]

pub struct Struct;

impl Struct {
pub type AssocTy = usize;
pub const AssocConst: Self::AssocTy = 42;
//~^ ERROR ambiguous associated type
//~| HELP use fully-qualified syntax
// FIXME: for some reason, the error is shown twice with rustdoc but only once with rustc
//~| ERROR ambiguous associated type
//~| HELP use fully-qualified syntax
}
15 changes: 15 additions & 0 deletions src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0223]: ambiguous associated type
--> $DIR/ambiguous-inherent-assoc-ty.rs:11:27
|
LL | pub const AssocConst: Self::AssocTy = 42;
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Struct as Trait>::AssocTy`

error[E0223]: ambiguous associated type
--> $DIR/ambiguous-inherent-assoc-ty.rs:11:27
|
LL | pub const AssocConst: Self::AssocTy = 42;
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Struct as Trait>::AssocTy`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0223`.