Skip to content

Commit 3a8bb38

Browse files
authored
Rollup merge of #84880 - jyn514:cleanup-itemkind, r=GuillaumeGomez
Make match in `register_res` easier to read - Don't duplicate DefKind -> ItemType handling; that's a good way to get bugs - Use exhaustive match - Add comments This found that register_res is very wrong in at least one way: if it registers a Res for `Variant`, it should also register one for `Field`. But I don't know whether the one for Variant should be removed or Field added. Maybe someone has ideas? Found while reviewing #84176.
2 parents 74c744e + 4029a03 commit 3a8bb38

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/librustdoc/clean/utils.rs

+33-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::clean::auto_trait::AutoTraitFinder;
22
use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
44
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
5-
MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
5+
Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
66
};
77
use crate::core::DocContext;
88
use crate::formats::item_type::ItemType;
@@ -451,35 +451,48 @@ crate fn get_auto_trait_and_blanket_impls(
451451
auto_impls.into_iter().chain(blanket_impls)
452452
}
453453

454+
/// If `res` has a documentation page associated, store it in the cache.
455+
///
456+
/// This is later used by [`href()`] to determine the HTML link for the item.
457+
///
458+
/// [`href()`]: crate::html::format::href
454459
crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
460+
use DefKind::*;
455461
debug!("register_res({:?})", res);
456462

457463
let (did, kind) = match res {
458-
Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
459-
Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
460-
Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
461-
Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
462464
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
465+
// associated items are documented, but on the page of their parent
463466
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
464467
}
465-
Res::Def(DefKind::Struct, i) => (i, ItemType::Struct),
466-
Res::Def(DefKind::Union, i) => (i, ItemType::Union),
467-
Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
468-
Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
469-
Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
470-
Res::Def(DefKind::Static, i) => (i, ItemType::Static),
471468
Res::Def(DefKind::Variant, i) => {
469+
// variant items are documented, but on the page of their parent
472470
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
473471
}
474-
Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
475-
MacroKind::Bang => (i, ItemType::Macro),
476-
MacroKind::Attr => (i, ItemType::ProcAttribute),
477-
MacroKind::Derive => (i, ItemType::ProcDerive),
478-
},
479-
Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
480-
Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
481-
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
482-
_ => return res.def_id(),
472+
// Each of these have their own page.
473+
Res::Def(
474+
kind
475+
@
476+
(Fn | TyAlias | Enum | Trait | Struct | Union | Mod | ForeignTy | Const | Static
477+
| Macro(..) | TraitAlias),
478+
i,
479+
) => (i, kind.into()),
480+
// This is part of a trait definition; document the trait.
481+
Res::SelfTy(Some(trait_def_id), _) => (trait_def_id, ItemType::Trait),
482+
// This is an inherent impl; it doesn't have its own page.
483+
Res::SelfTy(None, Some((impl_def_id, _))) => return impl_def_id,
484+
Res::SelfTy(None, None)
485+
| Res::PrimTy(_)
486+
| Res::ToolMod
487+
| Res::SelfCtor(_)
488+
| Res::Local(_)
489+
| Res::NonMacroAttr(_)
490+
| Res::Err => return res.def_id(),
491+
Res::Def(
492+
TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst | OpaqueTy
493+
| Field | LifetimeParam | GlobalAsm | Impl | Closure | Generator,
494+
id,
495+
) => return id,
483496
};
484497
if did.is_local() {
485498
return did;

src/test/rustdoc/intra-doc/field.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @has field/index.html '//a[@href="https://doc.rust-lang.org/nightly/core/ops/range/struct.Range.html#structfield.start"]' 'start'
2+
// @has field/index.html '//a[@href="https://doc.rust-lang.org/nightly/std/io/error/enum.ErrorKind.html#variant.NotFound"]' 'not_found'
3+
//! [start][std::ops::Range::start]
4+
//! [not_found][std::io::ErrorKind::NotFound]

0 commit comments

Comments
 (0)