Skip to content

Commit 37ce332

Browse files
authored
Rollup merge of #84811 - scottmcm:rustdoc-trait-alias-fix, r=jyn514
RustDoc: Fix bounds linking trait.Foo instead of traitalias.Foo Fixes #84782 The code was assuming `Trait` when adding bounds to the cache, so add a check on the DefId to see what its kind really is. r? `@jyn514` Before: ![image](https://user-images.githubusercontent.com/18526288/116775611-6a751e80-aa53-11eb-84d0-ed6b7782be3c.png) After: ![image](https://user-images.githubusercontent.com/18526288/116802227-d19cdc80-ab00-11eb-8133-7b34dd750da2.png)
2 parents 0133af5 + 40ffa94 commit 37ce332

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType)
188188
if did.is_local() {
189189
cx.cache.exact_paths.insert(did, fqn);
190190
} else {
191-
cx.cache.external_paths.insert(did, (fqn, ItemType::from(kind)));
191+
cx.cache.external_paths.insert(did, (fqn, kind));
192192
}
193193
}
194194

src/librustdoc/clean/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
1717
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1818
use rustc_index::vec::{Idx, IndexVec};
1919
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
20-
use rustc_middle::bug;
2120
use rustc_middle::middle::resolve_lifetime as rl;
2221
use rustc_middle::ty::fold::TypeFolder;
2322
use rustc_middle::ty::subst::{InternalSubsts, Subst};
2423
use rustc_middle::ty::{self, AdtKind, Lift, Ty, TyCtxt};
24+
use rustc_middle::{bug, span_bug};
2525
use rustc_mir::const_eval::{is_const_fn, is_unstable_const_fn};
2626
use rustc_span::hygiene::{AstPass, MacroKind};
2727
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -158,7 +158,15 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
158158
impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
159159
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
160160
let (trait_ref, bounds) = *self;
161-
inline::record_extern_fqn(cx, trait_ref.def_id, ItemType::Trait);
161+
let kind = cx.tcx.def_kind(trait_ref.def_id).into();
162+
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
163+
span_bug!(
164+
cx.tcx.def_span(trait_ref.def_id),
165+
"`TraitRef` had unexpected kind {:?}",
166+
kind
167+
);
168+
}
169+
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
162170
let path = external_path(
163171
cx,
164172
cx.tcx.item_name(trait_ref.def_id),

src/librustdoc/formats/cache.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
327327
| clean::EnumItem(..)
328328
| clean::TypedefItem(..)
329329
| clean::TraitItem(..)
330+
| clean::TraitAliasItem(..)
330331
| clean::FunctionItem(..)
331332
| clean::ModuleItem(..)
332333
| clean::ForeignFunctionItem(..)
@@ -337,26 +338,43 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
337338
| clean::ForeignTypeItem
338339
| clean::MacroItem(..)
339340
| clean::ProcMacroItem(..)
340-
| clean::VariantItem(..)
341-
if !self.cache.stripped_mod =>
342-
{
343-
// Re-exported items mean that the same id can show up twice
344-
// in the rustdoc ast that we're looking at. We know,
345-
// however, that a re-exported item doesn't show up in the
346-
// `public_items` map, so we can skip inserting into the
347-
// paths map if there was already an entry present and we're
348-
// not a public item.
349-
if !self.cache.paths.contains_key(&item.def_id)
350-
|| self.cache.access_levels.is_public(item.def_id)
351-
{
352-
self.cache.paths.insert(item.def_id, (self.cache.stack.clone(), item.type_()));
341+
| clean::VariantItem(..) => {
342+
if !self.cache.stripped_mod {
343+
// Re-exported items mean that the same id can show up twice
344+
// in the rustdoc ast that we're looking at. We know,
345+
// however, that a re-exported item doesn't show up in the
346+
// `public_items` map, so we can skip inserting into the
347+
// paths map if there was already an entry present and we're
348+
// not a public item.
349+
if !self.cache.paths.contains_key(&item.def_id)
350+
|| self.cache.access_levels.is_public(item.def_id)
351+
{
352+
self.cache
353+
.paths
354+
.insert(item.def_id, (self.cache.stack.clone(), item.type_()));
355+
}
353356
}
354357
}
355358
clean::PrimitiveItem(..) => {
356359
self.cache.paths.insert(item.def_id, (self.cache.stack.clone(), item.type_()));
357360
}
358361

359-
_ => {}
362+
clean::ExternCrateItem { .. }
363+
| clean::ImportItem(..)
364+
| clean::OpaqueTyItem(..)
365+
| clean::ImplItem(..)
366+
| clean::TyMethodItem(..)
367+
| clean::MethodItem(..)
368+
| clean::StructFieldItem(..)
369+
| clean::AssocConstItem(..)
370+
| clean::AssocTypeItem(..)
371+
| clean::StrippedItem(..)
372+
| clean::KeywordItem(..) => {
373+
// FIXME: Do these need handling?
374+
// The person writing this comment doesn't know.
375+
// So would rather leave them to an expert,
376+
// as at least the list is better than `_ => {}`.
377+
}
360378
}
361379

362380
// Maintain the parent stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![feature(trait_alias)]
2+
3+
pub trait SomeAlias = std::fmt::Debug + std::marker::Copy;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// aux-build:trait-alias-mention.rs
2+
// build-aux-docs
3+
4+
#![crate_name = "foo"]
5+
6+
extern crate trait_alias_mention;
7+
8+
// @has foo/fn.mention_alias_in_bounds.html '//a[@href="../trait_alias_mention/traitalias.SomeAlias.html"]' 'SomeAlias'
9+
pub fn mention_alias_in_bounds<T: trait_alias_mention::SomeAlias>() {
10+
}

src/test/rustdoc/trait_alias.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ pub trait CopyAlias = Copy;
1919
pub trait Alias2 = Copy + Debug;
2020
// @has foo/traitalias.Foo.html '//section[@id="main"]/pre' 'trait Foo<T> = Into<T> + Debug;'
2121
pub trait Foo<T> = Into<T> + Debug;
22+
// @has foo/fn.bar.html '//a[@href="traitalias.Alias2.html"]' 'Alias2'
23+
pub fn bar<T>() where T: Alias2 {}

0 commit comments

Comments
 (0)