Skip to content

Commit 73162aa

Browse files
authored
Rollup merge of #88573 - camelid:rustdoc-assoc-panic, r=GuillaumeGomez
rustdoc: Don't panic on ambiguous inherent associated types Instead, return `Type::Infer` since compilation should fail anyway. That's how rustdoc handles `hir::TyKind::Err`s, so this just extends that behavior to `ty::Err`s when analyzing associated types. For some reason, the error is printed twice with rustdoc (though only once with rustc). I'm not sure why that is, but it's better than panicking. This commit also makes rustdoc fail early in the non-projection, non-error case, instead of returning a `Res::Err` that would likely cause rustdoc to panic later on. This change is originally from #88379. r? `@GuillaumeGomez`
2 parents f419334 + 50983ba commit 73162aa

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/librustdoc/clean/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1311,10 +1311,11 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13111311
}
13121312
hir::QPath::TypeRelative(ref qself, ref segment) => {
13131313
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
1314-
let res = if let ty::Projection(proj) = ty.kind() {
1315-
Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id)
1316-
} else {
1317-
Res::Err
1314+
let res = match ty.kind() {
1315+
ty::Projection(proj) => Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id),
1316+
// Rustdoc handles `ty::Error`s by turning them into `Type::Infer`s.
1317+
ty::Error(_) => return Type::Infer,
1318+
_ => bug!("clean: expected associated type, found `{:?}`", ty),
13181319
};
13191320
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
13201321
Type::QPath {
@@ -1379,6 +1380,7 @@ impl Clean<Type> for hir::Ty<'_> {
13791380
DynTrait(bounds, lifetime)
13801381
}
13811382
TyKind::BareFn(ref barefn) => BareFunction(Box::new(barefn.clean(cx))),
1383+
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
13821384
TyKind::Infer | TyKind::Err => Infer,
13831385
TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.kind),
13841386
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test ensures that rustdoc does not panic on inherented associated types
2+
// that are referred to without fully-qualified syntax.
3+
4+
#![feature(inherent_associated_types)]
5+
#![allow(incomplete_features)]
6+
7+
pub struct Struct;
8+
9+
impl Struct {
10+
pub type AssocTy = usize;
11+
pub const AssocConst: Self::AssocTy = 42;
12+
//~^ ERROR ambiguous associated type
13+
//~| HELP use fully-qualified syntax
14+
// FIXME: for some reason, the error is shown twice with rustdoc but only once with rustc
15+
//~| ERROR ambiguous associated type
16+
//~| HELP use fully-qualified syntax
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0223]: ambiguous associated type
2+
--> $DIR/ambiguous-inherent-assoc-ty.rs:11:27
3+
|
4+
LL | pub const AssocConst: Self::AssocTy = 42;
5+
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Struct as Trait>::AssocTy`
6+
7+
error[E0223]: ambiguous associated type
8+
--> $DIR/ambiguous-inherent-assoc-ty.rs:11:27
9+
|
10+
LL | pub const AssocConst: Self::AssocTy = 42;
11+
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Struct as Trait>::AssocTy`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0223`.

0 commit comments

Comments
 (0)