Skip to content
19 changes: 8 additions & 11 deletions src/librustdoc/calculate_doc_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ use rustc_span::{FileName, RemapPathScopeComponents};
use serde::Serialize;
use tracing::debug;

use crate::clean::{self, ItemKind};
use crate::config::{OutputFormat, RenderOptions};
use crate::core::DocContext;
use crate::docfs::PathError;
use crate::error::Error;
use crate::html::markdown::{ErrorCodes, find_testable_code};
use crate::passes::check_doc_test_visibility::{Tests, should_have_doc_example};
use crate::try_err;
use crate::visit::DocVisitor;
use crate::{clean, try_err};

pub(crate) fn run(
krate: &clean::Crate,
Expand Down Expand Up @@ -219,21 +220,17 @@ impl DocVisitor<'_> for CoverageCalculator<'_, '_> {
}

match i.kind {
clean::StrippedItem(..) => {
// don't count items in stripped modules
return;
}
clean::PlaceholderImplItem => {
// The "real" impl items are handled below.
return;
}
// Don't count items in stripped modules.
ItemKind::Stripped(..) => return,
// The "real" impl items are handled below.
ItemKind::PlaceholderImpl => return,
// docs on `use` and `extern crate` statements are not displayed, so they're not
// worth counting
clean::ImportItem(..) | clean::ExternCrateItem { .. } => {}
ItemKind::Import(..) | ItemKind::ExternCrate { .. } => {}
// Don't count trait impls, the missing-docs lint doesn't so we shouldn't either.
// Inherent impls *can* be documented, and those docs show up, but in most cases it
// doesn't make sense, as all methods on a type are in one single impl block
clean::ImplItem(_) => {}
ItemKind::Impl(_) => {}
_ => {
let has_docs = !i.attrs.doc_strings.is_empty();
let mut tests = Tests { found_tests: 0 };
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn synthesize_auto_trait_impl<'tcx>(
name: None,
attrs: Default::default(),
stability: None,
kind: clean::ImplItem(Box::new(clean::Impl {
kind: clean::ItemKind::Impl(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(crate) fn synthesize_blanket_impls(
item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
attrs: Default::default(),
stability: None,
kind: clean::ImplItem(Box::new(clean::Impl {
kind: clean::ItemKind::Impl(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics: clean_ty_generics(cx, impl_def_id),
// FIXME(eddyb) compute both `trait_` and `for_` from
Expand Down
73 changes: 34 additions & 39 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tracing::{debug, instrument, trace};

use super::{Item, extract_cfg_from_attrs};
use crate::clean::{
self, Attributes, CfgInfo, ImplKind, ItemId, Type, clean_bound_vars, clean_generics,
self, Attributes, CfgInfo, ImplKind, ItemId, ItemKind, Type, clean_bound_vars, clean_generics,
clean_impl_item, clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
clean_poly_fn_sig, clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type,
clean_ty_generics, clean_variant_def, utils,
Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) fn try_inline(
) -> Option<Vec<clean::Item>> {
fn try_inline_inner(
cx: &mut DocContext<'_>,
kind: clean::ItemKind,
kind: ItemKind,
did: DefId,
name: Symbol,
import_def_id: Option<LocalDefId>,
Expand Down Expand Up @@ -88,52 +88,55 @@ pub(crate) fn try_inline(
record_extern_fqn(cx, did, ItemType::Trait);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::TraitItem(Box::new(build_trait(cx, did)))
ItemKind::Trait(Box::new(build_trait(cx, did)))
})
}
Res::Def(DefKind::TraitAlias, did) => {
record_extern_fqn(cx, did, ItemType::TraitAlias);
cx.with_param_env(did, |cx| clean::TraitAliasItem(build_trait_alias(cx, did)))
cx.with_param_env(did, |cx| ItemKind::TraitAlias(build_trait_alias(cx, did)))
}
Res::Def(DefKind::Fn, did) => {
record_extern_fqn(cx, did, ItemType::Function);
cx.with_param_env(did, |cx| {
clean::enter_impl_trait(cx, |cx| clean::FunctionItem(build_function(cx, did)))
clean::enter_impl_trait(cx, |cx| {
let (generics, decl) = build_function(cx, did);
ItemKind::Fn(Box::new(clean::Function { generics, decl }))
})
})
}
Res::Def(DefKind::Struct, did) => {
record_extern_fqn(cx, did, ItemType::Struct);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::StructItem(build_struct(cx, did))
ItemKind::Struct(build_struct(cx, did))
})
}
Res::Def(DefKind::Union, did) => {
record_extern_fqn(cx, did, ItemType::Union);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::UnionItem(build_union(cx, did))
ItemKind::Union(build_union(cx, did))
})
}
Res::Def(DefKind::TyAlias, did) => {
record_extern_fqn(cx, did, ItemType::TypeAlias);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::TypeAliasItem(build_type_alias(cx, did, &mut ret))
ItemKind::TyAlias(build_type_alias(cx, did, &mut ret))
})
}
Res::Def(DefKind::Enum, did) => {
record_extern_fqn(cx, did, ItemType::Enum);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::EnumItem(build_enum(cx, did))
ItemKind::Enum(build_enum(cx, did))
})
}
Res::Def(DefKind::ForeignTy, did) => {
record_extern_fqn(cx, did, ItemType::ForeignType);
cx.with_param_env(did, |cx| {
build_impls(cx, did, attrs_without_docs, &mut ret);
clean::ForeignTypeItem
ItemKind::ForeignTy
})
}
// Never inline enum variants but leave them shown as re-exports.
Expand All @@ -143,19 +146,19 @@ pub(crate) fn try_inline(
Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
Res::Def(DefKind::Mod, did) => {
record_extern_fqn(cx, did, ItemType::Module);
clean::ModuleItem(build_module(cx, did, name, visited))
ItemKind::Module(build_module(cx, did, name, visited))
}
Res::Def(DefKind::Static { .. }, did) => {
record_extern_fqn(cx, did, ItemType::Static);
cx.with_param_env(did, |cx| {
clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
ItemKind::Static(build_static(cx, did, cx.tcx.is_mutable_static(did)))
})
}
Res::Def(DefKind::Const, did) => {
record_extern_fqn(cx, did, ItemType::Constant);
cx.with_param_env(did, |cx| {
let ct = build_const_item(cx, did);
clean::ConstantItem(Box::new(ct))
ItemKind::Const(Box::new(ct))
})
}
Res::Def(DefKind::Macro(kinds), did) => {
Expand Down Expand Up @@ -325,7 +328,10 @@ fn build_trait_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::TraitAlias {
clean::TraitAlias { generics, bounds }
}

pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clean::Function> {
pub(super) fn build_function(
cx: &mut DocContext<'_>,
def_id: DefId,
) -> (clean::Generics, clean::FnDecl) {
let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
// The generics need to be cleaned before the signature.
let mut generics = clean_ty_generics(cx, def_id);
Expand Down Expand Up @@ -353,7 +359,7 @@ pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clea

let decl = clean_poly_fn_sig(cx, Some(def_id), sig);

Box::new(clean::Function { decl, generics })
(generics, decl)
}

fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
Expand Down Expand Up @@ -385,17 +391,12 @@ fn build_type_alias(
cx: &mut DocContext<'_>,
did: DefId,
ret: &mut Vec<Item>,
) -> Box<clean::TypeAlias> {
) -> Box<clean::TyAlias> {
let ty = cx.tcx.type_of(did).instantiate_identity().skip_norm_wip();
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);

Box::new(clean::TypeAlias {
type_,
generics: clean_ty_generics(cx, did),
inner_type,
item_type: None,
})
Box::new(clean::TyAlias { ty: type_, generics: clean_ty_generics(cx, did), inner_type })
}

/// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
Expand Down Expand Up @@ -652,7 +653,7 @@ pub(crate) fn build_impl(
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
did,
None,
clean::ImplItem(Box::new(clean::Impl {
ItemKind::Impl(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_,
Expand Down Expand Up @@ -758,7 +759,7 @@ fn build_module_items(
item_id: ItemId::DefId(module_def_id),
attrs: Default::default(),
stability: None,
kind: clean::ImportItem(clean::Import::new_simple(
kind: ItemKind::Import(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
Expand Down Expand Up @@ -797,7 +798,7 @@ fn build_module_items(
let item = Item::from_def_id_and_parts(
module_def_id,
None,
clean::ImportItem(clean::Import::new_simple(
ItemKind::Import(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
Expand Down Expand Up @@ -852,7 +853,7 @@ fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
None,
None,
);
clean::Constant { generics, type_: ty, kind: clean::ConstantKind::Extern { def_id } }
clean::Constant { generics, ty, rhs: clean::ConstantKind::Extern { def_id } }
}

fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
Expand All @@ -868,23 +869,17 @@ fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::St
}
}

fn build_macro(
tcx: TyCtxt<'_>,
def_id: DefId,
name: Symbol,
macro_kinds: MacroKinds,
) -> clean::ItemKind {
fn build_macro(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol, macro_kinds: MacroKinds) -> ItemKind {
match CStore::from_tcx(tcx).load_macro_untracked(tcx, def_id) {
LoadedMacro::MacroDef { def, .. } => match macro_kinds {
MacroKinds::DERIVE => clean::ProcMacroItem(clean::ProcMacro {
MacroKinds::DERIVE => ItemKind::ProcMacro(clean::ProcMacro {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wish we could unify ProcMacro and DeclMacro, because as-is, ProcMacro sometimes represents a decl macro. still unsure if a macro_rules derive (that isn't hybrid) should render identically to a proc-macro derive. We do at least want the url to render identically, so crates can ideally migrate between the two without breaking links.

@fmease fmease Oct 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree, we should do that at some point but I didn't really think about macros during this refactoring, I made this drive-by change just because I was already renaming other variants and it seemed trivial.

kind: MacroKind::Derive,
helpers: Vec::new(),
}),
MacroKinds::ATTR => clean::ProcMacroItem(clean::ProcMacro {
kind: MacroKind::Attr,
helpers: Vec::new(),
}),
_ => clean::MacroItem(
MacroKinds::ATTR => {
ItemKind::ProcMacro(clean::ProcMacro { kind: MacroKind::Attr, helpers: Vec::new() })
}
_ => ItemKind::DeclMacro(
clean::Macro {
source: utils::display_macro_source(tcx, name, &def),
macro_rules: def.macro_rules,
Expand All @@ -900,7 +895,7 @@ fn build_macro(
MacroKinds::DERIVE => MacroKind::Derive,
_ => unreachable!(),
};
clean::ProcMacroItem(clean::ProcMacro { kind, helpers: ext.helper_attrs })
ItemKind::ProcMacro(clean::ProcMacro { kind, helpers: ext.helper_attrs })
}
}
}
Expand Down
Loading
Loading