Skip to content

Commit 4029a03

Browse files
committed
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?
1 parent 716394d commit 4029a03

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;
@@ -431,35 +431,48 @@ crate fn get_auto_trait_and_blanket_impls(
431431
auto_impls.into_iter().chain(blanket_impls)
432432
}
433433

434+
/// If `res` has a documentation page associated, store it in the cache.
435+
///
436+
/// This is later used by [`href()`] to determine the HTML link for the item.
437+
///
438+
/// [`href()`]: crate::html::format::href
434439
crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
440+
use DefKind::*;
435441
debug!("register_res({:?})", res);
436442

437443
let (did, kind) = match res {
438-
Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
439-
Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
440-
Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
441-
Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
442444
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
445+
// associated items are documented, but on the page of their parent
443446
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
444447
}
445-
Res::Def(DefKind::Struct, i) => (i, ItemType::Struct),
446-
Res::Def(DefKind::Union, i) => (i, ItemType::Union),
447-
Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
448-
Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
449-
Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
450-
Res::Def(DefKind::Static, i) => (i, ItemType::Static),
451448
Res::Def(DefKind::Variant, i) => {
449+
// variant items are documented, but on the page of their parent
452450
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
453451
}
454-
Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
455-
MacroKind::Bang => (i, ItemType::Macro),
456-
MacroKind::Attr => (i, ItemType::ProcAttribute),
457-
MacroKind::Derive => (i, ItemType::ProcDerive),
458-
},
459-
Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
460-
Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
461-
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
462-
_ => return res.def_id(),
452+
// Each of these have their own page.
453+
Res::Def(
454+
kind
455+
@
456+
(Fn | TyAlias | Enum | Trait | Struct | Union | Mod | ForeignTy | Const | Static
457+
| Macro(..) | TraitAlias),
458+
i,
459+
) => (i, kind.into()),
460+
// This is part of a trait definition; document the trait.
461+
Res::SelfTy(Some(trait_def_id), _) => (trait_def_id, ItemType::Trait),
462+
// This is an inherent impl; it doesn't have its own page.
463+
Res::SelfTy(None, Some((impl_def_id, _))) => return impl_def_id,
464+
Res::SelfTy(None, None)
465+
| Res::PrimTy(_)
466+
| Res::ToolMod
467+
| Res::SelfCtor(_)
468+
| Res::Local(_)
469+
| Res::NonMacroAttr(_)
470+
| Res::Err => return res.def_id(),
471+
Res::Def(
472+
TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst | OpaqueTy
473+
| Field | LifetimeParam | GlobalAsm | Impl | Closure | Generator,
474+
id,
475+
) => return id,
463476
};
464477
if did.is_local() {
465478
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)