Skip to content

Commit

Permalink
Merge pull request #1 from Infineon/render_tests
Browse files Browse the repository at this point in the history
Render tests
  • Loading branch information
lucagladiator authored Jul 3, 2024
2 parents 489f41c + 860f1a7 commit 7259832
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,11 @@ fn clean_fn_or_proc_macro<'tcx>(
None => {
let mut func = clean_function(cx, sig, generics, FunctionArgs::Body(body_id));
clean_fn_decl_legacy_const_generics(&mut func, attrs);
FunctionItem(func)
if cx.cache.document_tests && cx.cache.tests.contains(&item.owner_id.to_def_id()) {
TestItem(func)
} else {
FunctionItem(func)
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,10 @@ impl Item {
asyncness: hir::IsAsync::NotAsync,
}
}
ItemKind::FunctionItem(_) | ItemKind::MethodItem(_, _) | ItemKind::TyMethodItem(_) => {
ItemKind::FunctionItem(_)
| ItemKind::MethodItem(_, _)
| ItemKind::TyMethodItem(_)
| ItemKind::TestItem(_) => {
let def_id = self.def_id().unwrap();
build_fn_header(def_id, tcx, tcx.asyncness(def_id))
}
Expand Down Expand Up @@ -826,6 +829,7 @@ pub(crate) enum ItemKind {
UnionItem(Union),
EnumItem(Enum),
FunctionItem(Box<Function>),
TestItem(Box<Function>),
ModuleItem(Module),
TypeAliasItem(Box<TypeAlias>),
OpaqueTyItem(OpaqueTy),
Expand Down Expand Up @@ -885,6 +889,7 @@ impl ItemKind {
ExternCrateItem { .. }
| ImportItem(_)
| FunctionItem(_)
| TestItem(_)
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
Expand Down
8 changes: 3 additions & 5 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ pub(crate) struct Options {
/// the compiler will scrape examples and not generate documentation.
pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,

/// Whether to generate documentation for tests.
pub(crate) document_tests: bool,

/// Note: this field is duplicated in `RenderOptions` because it's useful
/// to have it in both places.
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
Expand Down Expand Up @@ -216,7 +213,6 @@ impl fmt::Debug for Options {
.field("test_builder_wrappers", &self.test_builder_wrappers)
.field("nocapture", &self.nocapture)
.field("scrape_examples_options", &self.scrape_examples_options)
.field("document_tests", &self.document_tests)
.field("unstable_features", &self.unstable_features)
.finish()
}
Expand Down Expand Up @@ -276,6 +272,8 @@ pub(crate) struct RenderOptions {
pub(crate) document_private: bool,
/// Document items that have `doc(hidden)`.
pub(crate) document_hidden: bool,
/// Document tests.
pub(crate) document_tests: bool,
/// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
pub(crate) generate_redirect_map: bool,
/// Show the memory layout of types in the docs.
Expand Down Expand Up @@ -782,7 +780,6 @@ impl Options {
output_format,
json_unused_externs,
scrape_examples_options,
document_tests,
unstable_features,
expanded_args: args,
};
Expand All @@ -806,6 +803,7 @@ impl Options {
markdown_playground_url,
document_private,
document_hidden,
document_tests,
generate_redirect_map,
show_type_layout,
unstable_features,
Expand Down
11 changes: 7 additions & 4 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ pub(crate) fn create_config(
describe_lints,
lint_cap,
scrape_examples_options,
document_tests,
expanded_args,
..
}: RustdocOptions,
RenderOptions { document_private, .. }: &RenderOptions,
RenderOptions { document_private, document_tests, .. }: &RenderOptions,
using_internal_features: Arc<AtomicBool>,
) -> rustc_interface::Config {
// Add the doc cfg into the doc build.
Expand Down Expand Up @@ -229,7 +228,7 @@ pub(crate) fn create_config(
let resolve_doc_links =
if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported };
let test =
scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false) || document_tests;
scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false) || *document_tests;
// plays with error output here!
let sessopts = config::Options {
maybe_sysroot,
Expand Down Expand Up @@ -342,7 +341,11 @@ pub(crate) fn run_global_ctxt(
impl_trait_bounds: Default::default(),
generated_synthetics: Default::default(),
auto_traits,
cache: Cache::new(render_options.document_private, render_options.document_hidden),
cache: Cache::new(
render_options.document_private,
render_options.document_hidden,
render_options.document_tests,
),
inlined: FxHashSet::default(),
output_format,
render_options,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub(crate) trait DocFolder: Sized {
ExternCrateItem { src: _ }
| ImportItem(_)
| FunctionItem(_)
| TestItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
| ConstantItem(_)
Expand Down
10 changes: 8 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ pub(crate) struct Cache {
/// Whether to document hidden items.
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
pub(crate) document_hidden: bool,
/// Whether to document tests.
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
pub(crate) document_tests: bool,
/// DefIds of all functions which are tests.
pub(crate) tests: FxHashSet<DefId>,

/// Crates marked with [`#[doc(masked)]`][doc_masked].
///
Expand Down Expand Up @@ -140,8 +145,8 @@ struct CacheBuilder<'a, 'tcx> {
}

impl Cache {
pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
Cache { document_private, document_hidden, ..Cache::default() }
pub(crate) fn new(document_private: bool, document_hidden: bool, document_tests: bool) -> Self {
Cache { document_private, document_hidden, document_tests, ..Cache::default() }
}

/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
Expand Down Expand Up @@ -424,6 +429,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
| clean::TraitItem(..)
| clean::TraitAliasItem(..)
| clean::FunctionItem(..)
| clean::TestItem(..)
| clean::ModuleItem(..)
| clean::ForeignFunctionItem(..)
| clean::ForeignStaticItem(..)
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub(crate) enum ItemType {
TraitAlias = 25,
// This number is reserved for use in JavaScript
// Generic = 26,
Test = 27,
}

impl Serialize for ItemType {
Expand All @@ -84,6 +85,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
clean::UnionItem(..) => ItemType::Union,
clean::EnumItem(..) => ItemType::Enum,
clean::FunctionItem(..) => ItemType::Function,
clean::TestItem(..) => ItemType::Test,
clean::TypeAliasItem(..) => ItemType::TypeAlias,
clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
clean::StaticItem(..) => ItemType::Static,
Expand Down Expand Up @@ -177,6 +179,7 @@ impl ItemType {
ItemType::Union => "union",
ItemType::Enum => "enum",
ItemType::Function => "fn",
ItemType::Test => "test",
ItemType::TypeAlias => "type",
ItemType::Static => "static",
ItemType::Trait => "trait",
Expand Down
12 changes: 12 additions & 0 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ struct AllTypes {
traits: FxHashSet<ItemEntry>,
macros: FxHashSet<ItemEntry>,
functions: FxHashSet<ItemEntry>,
tests: FxHashSet<ItemEntry>,
type_aliases: FxHashSet<ItemEntry>,
opaque_tys: FxHashSet<ItemEntry>,
statics: FxHashSet<ItemEntry>,
Expand All @@ -357,6 +358,7 @@ impl AllTypes {
traits: new_set(100),
macros: new_set(100),
functions: new_set(100),
tests: new_set(100),
type_aliases: new_set(100),
opaque_tys: new_set(100),
statics: new_set(100),
Expand All @@ -381,6 +383,7 @@ impl AllTypes {
ItemType::Trait => self.traits.insert(ItemEntry::new(new_url, name)),
ItemType::Macro => self.macros.insert(ItemEntry::new(new_url, name)),
ItemType::Function => self.functions.insert(ItemEntry::new(new_url, name)),
ItemType::Test => self.tests.insert(ItemEntry::new(new_url, name)),
ItemType::TypeAlias => self.type_aliases.insert(ItemEntry::new(new_url, name)),
ItemType::OpaqueTy => self.opaque_tys.insert(ItemEntry::new(new_url, name)),
ItemType::Static => self.statics.insert(ItemEntry::new(new_url, name)),
Expand Down Expand Up @@ -419,6 +422,9 @@ impl AllTypes {
if !self.functions.is_empty() {
sections.insert(ItemSection::Functions);
}
if !self.tests.is_empty() {
sections.insert(ItemSection::Tests);
}
if !self.type_aliases.is_empty() {
sections.insert(ItemSection::TypeAliases);
}
Expand Down Expand Up @@ -476,6 +482,7 @@ impl AllTypes {
print_entries(f, &self.attribute_macros, ItemSection::AttributeMacros);
print_entries(f, &self.derive_macros, ItemSection::DeriveMacros);
print_entries(f, &self.functions, ItemSection::Functions);
print_entries(f, &self.tests, ItemSection::Tests);
print_entries(f, &self.type_aliases, ItemSection::TypeAliases);
print_entries(f, &self.trait_aliases, ItemSection::TraitAliases);
print_entries(f, &self.opaque_tys, ItemSection::OpaqueTypes);
Expand Down Expand Up @@ -2149,6 +2156,7 @@ pub(crate) enum ItemSection {
Statics,
Traits,
Functions,
Tests,
TypeAliases,
Unions,
Implementations,
Expand Down Expand Up @@ -2182,6 +2190,7 @@ impl ItemSection {
Statics,
Traits,
Functions,
Tests,
TypeAliases,
Unions,
Implementations,
Expand All @@ -2208,6 +2217,7 @@ impl ItemSection {
Self::Unions => "unions",
Self::Enums => "enums",
Self::Functions => "functions",
Self::Tests => "tests",
Self::TypeAliases => "types",
Self::Statics => "statics",
Self::Constants => "constants",
Expand Down Expand Up @@ -2238,6 +2248,7 @@ impl ItemSection {
Self::Unions => "Unions",
Self::Enums => "Enums",
Self::Functions => "Functions",
Self::Tests => "Tests",
Self::TypeAliases => "Type Aliases",
Self::Statics => "Statics",
Self::Constants => "Constants",
Expand Down Expand Up @@ -2269,6 +2280,7 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
ItemType::Union => ItemSection::Unions,
ItemType::Enum => ItemSection::Enums,
ItemType::Function => ItemSection::Functions,
ItemType::Test => ItemSection::Tests,
ItemType::TypeAlias => ItemSection::TypeAliases,
ItemType::Static => ItemSection::Statics,
ItemType::Constant => ItemSection::Constants,
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
}
}
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
clean::TestItem(..) => "Test ",
clean::TraitItem(..) => "Trait ",
clean::StructItem(..) => "Struct ",
clean::UnionItem(..) => "Union ",
Expand Down Expand Up @@ -254,7 +255,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf

match &*item.kind {
clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => {
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) | clean::TestItem(ref f) => {
item_function(buf, cx, item, f)
}
clean::TraitItem(ref t) => item_trait(buf, cx, item, t),
Expand Down Expand Up @@ -331,6 +332,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
ItemType::Static => 8,
ItemType::Trait => 9,
ItemType::Function => 10,
ItemType::Test => 11,
ItemType::TypeAlias => 12,
ItemType::Union => 13,
_ => 14 + ty as u8,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ function preLoadCss(cssUrl) {
block("static", "static", "Statics");
block("trait", "traits", "Traits");
block("fn", "functions", "Functions");
block("test", "tests", "Tests");
block("type", "types", "Type Aliases");
block("union", "unions", "Unions");
// No point, because these items don't appear in modules
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
StructFieldItem(f) => ItemEnum::StructField(f.into_tcx(tcx)),
EnumItem(e) => ItemEnum::Enum(e.into_tcx(tcx)),
VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
FunctionItem(f) => ItemEnum::Function(from_function(f, true, header.unwrap(), tcx)),
FunctionItem(f) | TestItem(f) => {
ItemEnum::Function(from_function(f, true, header.unwrap(), tcx))
}
ForeignFunctionItem(f) => ItemEnum::Function(from_function(f, false, header.unwrap(), tcx)),
TraitItem(t) => ItemEnum::Trait((*t).into_tcx(tcx)),
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
Expand Down Expand Up @@ -844,7 +846,7 @@ impl FromWithTcx<ItemType> for ItemKind {
Struct => ItemKind::Struct,
Union => ItemKind::Union,
Enum => ItemKind::Enum,
Function | TyMethod | Method => ItemKind::Function,
Function | Test | TyMethod | Method => ItemKind::Function,
TypeAlias => ItemKind::TypeAlias,
OpaqueTy => ItemKind::OpaqueTy,
Static => ItemKind::Static,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/passes/stripper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
| clean::EnumItem(..)
| clean::TraitItem(..)
| clean::FunctionItem(..)
| clean::TestItem(..)
| clean::VariantItem(..)
| clean::ForeignFunctionItem(..)
| clean::ForeignStaticItem(..)
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) trait DocVisitor: Sized {
ExternCrateItem { src: _ }
| ImportItem(_)
| FunctionItem(_)
| TestItem(_)
| TypeAliasItem(_)
| OpaqueTyItem(_)
| StaticItem(_)
Expand Down
Loading

0 comments on commit 7259832

Please sign in to comment.