Skip to content

Commit 7259832

Browse files
Merge pull request #1 from Infineon/render_tests
Render tests
2 parents 489f41c + 860f1a7 commit 7259832

File tree

14 files changed

+91
-19
lines changed

14 files changed

+91
-19
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,11 @@ fn clean_fn_or_proc_macro<'tcx>(
10461046
None => {
10471047
let mut func = clean_function(cx, sig, generics, FunctionArgs::Body(body_id));
10481048
clean_fn_decl_legacy_const_generics(&mut func, attrs);
1049-
FunctionItem(func)
1049+
if cx.cache.document_tests && cx.cache.tests.contains(&item.owner_id.to_def_id()) {
1050+
TestItem(func)
1051+
} else {
1052+
FunctionItem(func)
1053+
}
10501054
}
10511055
}
10521056
}

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,10 @@ impl Item {
659659
asyncness: hir::IsAsync::NotAsync,
660660
}
661661
}
662-
ItemKind::FunctionItem(_) | ItemKind::MethodItem(_, _) | ItemKind::TyMethodItem(_) => {
662+
ItemKind::FunctionItem(_)
663+
| ItemKind::MethodItem(_, _)
664+
| ItemKind::TyMethodItem(_)
665+
| ItemKind::TestItem(_) => {
663666
let def_id = self.def_id().unwrap();
664667
build_fn_header(def_id, tcx, tcx.asyncness(def_id))
665668
}
@@ -826,6 +829,7 @@ pub(crate) enum ItemKind {
826829
UnionItem(Union),
827830
EnumItem(Enum),
828831
FunctionItem(Box<Function>),
832+
TestItem(Box<Function>),
829833
ModuleItem(Module),
830834
TypeAliasItem(Box<TypeAlias>),
831835
OpaqueTyItem(OpaqueTy),
@@ -885,6 +889,7 @@ impl ItemKind {
885889
ExternCrateItem { .. }
886890
| ImportItem(_)
887891
| FunctionItem(_)
892+
| TestItem(_)
888893
| TypeAliasItem(_)
889894
| OpaqueTyItem(_)
890895
| StaticItem(_)

src/librustdoc/config.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ pub(crate) struct Options {
160160
/// the compiler will scrape examples and not generate documentation.
161161
pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,
162162

163-
/// Whether to generate documentation for tests.
164-
pub(crate) document_tests: bool,
165-
166163
/// Note: this field is duplicated in `RenderOptions` because it's useful
167164
/// to have it in both places.
168165
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
@@ -216,7 +213,6 @@ impl fmt::Debug for Options {
216213
.field("test_builder_wrappers", &self.test_builder_wrappers)
217214
.field("nocapture", &self.nocapture)
218215
.field("scrape_examples_options", &self.scrape_examples_options)
219-
.field("document_tests", &self.document_tests)
220216
.field("unstable_features", &self.unstable_features)
221217
.finish()
222218
}
@@ -276,6 +272,8 @@ pub(crate) struct RenderOptions {
276272
pub(crate) document_private: bool,
277273
/// Document items that have `doc(hidden)`.
278274
pub(crate) document_hidden: bool,
275+
/// Document tests.
276+
pub(crate) document_tests: bool,
279277
/// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
280278
pub(crate) generate_redirect_map: bool,
281279
/// Show the memory layout of types in the docs.
@@ -782,7 +780,6 @@ impl Options {
782780
output_format,
783781
json_unused_externs,
784782
scrape_examples_options,
785-
document_tests,
786783
unstable_features,
787784
expanded_args: args,
788785
};
@@ -806,6 +803,7 @@ impl Options {
806803
markdown_playground_url,
807804
document_private,
808805
document_hidden,
806+
document_tests,
809807
generate_redirect_map,
810808
show_type_layout,
811809
unstable_features,

src/librustdoc/core.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ pub(crate) fn create_config(
195195
describe_lints,
196196
lint_cap,
197197
scrape_examples_options,
198-
document_tests,
199198
expanded_args,
200199
..
201200
}: RustdocOptions,
202-
RenderOptions { document_private, .. }: &RenderOptions,
201+
RenderOptions { document_private, document_tests, .. }: &RenderOptions,
203202
using_internal_features: Arc<AtomicBool>,
204203
) -> rustc_interface::Config {
205204
// Add the doc cfg into the doc build.
@@ -229,7 +228,7 @@ pub(crate) fn create_config(
229228
let resolve_doc_links =
230229
if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported };
231230
let test =
232-
scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false) || document_tests;
231+
scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false) || *document_tests;
233232
// plays with error output here!
234233
let sessopts = config::Options {
235234
maybe_sysroot,
@@ -342,7 +341,11 @@ pub(crate) fn run_global_ctxt(
342341
impl_trait_bounds: Default::default(),
343342
generated_synthetics: Default::default(),
344343
auto_traits,
345-
cache: Cache::new(render_options.document_private, render_options.document_hidden),
344+
cache: Cache::new(
345+
render_options.document_private,
346+
render_options.document_hidden,
347+
render_options.document_tests,
348+
),
346349
inlined: FxHashSet::default(),
347350
output_format,
348351
render_options,

src/librustdoc/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) trait DocFolder: Sized {
7777
ExternCrateItem { src: _ }
7878
| ImportItem(_)
7979
| FunctionItem(_)
80+
| TestItem(_)
8081
| OpaqueTyItem(_)
8182
| StaticItem(_)
8283
| ConstantItem(_)

src/librustdoc/formats/cache.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ pub(crate) struct Cache {
8989
/// Whether to document hidden items.
9090
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
9191
pub(crate) document_hidden: bool,
92+
/// Whether to document tests.
93+
/// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
94+
pub(crate) document_tests: bool,
95+
/// DefIds of all functions which are tests.
96+
pub(crate) tests: FxHashSet<DefId>,
9297

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

142147
impl Cache {
143-
pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
144-
Cache { document_private, document_hidden, ..Cache::default() }
148+
pub(crate) fn new(document_private: bool, document_hidden: bool, document_tests: bool) -> Self {
149+
Cache { document_private, document_hidden, document_tests, ..Cache::default() }
145150
}
146151

147152
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
@@ -424,6 +429,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
424429
| clean::TraitItem(..)
425430
| clean::TraitAliasItem(..)
426431
| clean::FunctionItem(..)
432+
| clean::TestItem(..)
427433
| clean::ModuleItem(..)
428434
| clean::ForeignFunctionItem(..)
429435
| clean::ForeignStaticItem(..)

src/librustdoc/formats/item_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) enum ItemType {
5858
TraitAlias = 25,
5959
// This number is reserved for use in JavaScript
6060
// Generic = 26,
61+
Test = 27,
6162
}
6263

6364
impl Serialize for ItemType {
@@ -84,6 +85,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
8485
clean::UnionItem(..) => ItemType::Union,
8586
clean::EnumItem(..) => ItemType::Enum,
8687
clean::FunctionItem(..) => ItemType::Function,
88+
clean::TestItem(..) => ItemType::Test,
8789
clean::TypeAliasItem(..) => ItemType::TypeAlias,
8890
clean::OpaqueTyItem(..) => ItemType::OpaqueTy,
8991
clean::StaticItem(..) => ItemType::Static,
@@ -177,6 +179,7 @@ impl ItemType {
177179
ItemType::Union => "union",
178180
ItemType::Enum => "enum",
179181
ItemType::Function => "fn",
182+
ItemType::Test => "test",
180183
ItemType::TypeAlias => "type",
181184
ItemType::Static => "static",
182185
ItemType::Trait => "trait",

src/librustdoc/html/render/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ struct AllTypes {
337337
traits: FxHashSet<ItemEntry>,
338338
macros: FxHashSet<ItemEntry>,
339339
functions: FxHashSet<ItemEntry>,
340+
tests: FxHashSet<ItemEntry>,
340341
type_aliases: FxHashSet<ItemEntry>,
341342
opaque_tys: FxHashSet<ItemEntry>,
342343
statics: FxHashSet<ItemEntry>,
@@ -357,6 +358,7 @@ impl AllTypes {
357358
traits: new_set(100),
358359
macros: new_set(100),
359360
functions: new_set(100),
361+
tests: new_set(100),
360362
type_aliases: new_set(100),
361363
opaque_tys: new_set(100),
362364
statics: new_set(100),
@@ -381,6 +383,7 @@ impl AllTypes {
381383
ItemType::Trait => self.traits.insert(ItemEntry::new(new_url, name)),
382384
ItemType::Macro => self.macros.insert(ItemEntry::new(new_url, name)),
383385
ItemType::Function => self.functions.insert(ItemEntry::new(new_url, name)),
386+
ItemType::Test => self.tests.insert(ItemEntry::new(new_url, name)),
384387
ItemType::TypeAlias => self.type_aliases.insert(ItemEntry::new(new_url, name)),
385388
ItemType::OpaqueTy => self.opaque_tys.insert(ItemEntry::new(new_url, name)),
386389
ItemType::Static => self.statics.insert(ItemEntry::new(new_url, name)),
@@ -419,6 +422,9 @@ impl AllTypes {
419422
if !self.functions.is_empty() {
420423
sections.insert(ItemSection::Functions);
421424
}
425+
if !self.tests.is_empty() {
426+
sections.insert(ItemSection::Tests);
427+
}
422428
if !self.type_aliases.is_empty() {
423429
sections.insert(ItemSection::TypeAliases);
424430
}
@@ -476,6 +482,7 @@ impl AllTypes {
476482
print_entries(f, &self.attribute_macros, ItemSection::AttributeMacros);
477483
print_entries(f, &self.derive_macros, ItemSection::DeriveMacros);
478484
print_entries(f, &self.functions, ItemSection::Functions);
485+
print_entries(f, &self.tests, ItemSection::Tests);
479486
print_entries(f, &self.type_aliases, ItemSection::TypeAliases);
480487
print_entries(f, &self.trait_aliases, ItemSection::TraitAliases);
481488
print_entries(f, &self.opaque_tys, ItemSection::OpaqueTypes);
@@ -2149,6 +2156,7 @@ pub(crate) enum ItemSection {
21492156
Statics,
21502157
Traits,
21512158
Functions,
2159+
Tests,
21522160
TypeAliases,
21532161
Unions,
21542162
Implementations,
@@ -2182,6 +2190,7 @@ impl ItemSection {
21822190
Statics,
21832191
Traits,
21842192
Functions,
2193+
Tests,
21852194
TypeAliases,
21862195
Unions,
21872196
Implementations,
@@ -2208,6 +2217,7 @@ impl ItemSection {
22082217
Self::Unions => "unions",
22092218
Self::Enums => "enums",
22102219
Self::Functions => "functions",
2220+
Self::Tests => "tests",
22112221
Self::TypeAliases => "types",
22122222
Self::Statics => "statics",
22132223
Self::Constants => "constants",
@@ -2238,6 +2248,7 @@ impl ItemSection {
22382248
Self::Unions => "Unions",
22392249
Self::Enums => "Enums",
22402250
Self::Functions => "Functions",
2251+
Self::Tests => "Tests",
22412252
Self::TypeAliases => "Type Aliases",
22422253
Self::Statics => "Statics",
22432254
Self::Constants => "Constants",
@@ -2269,6 +2280,7 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
22692280
ItemType::Union => ItemSection::Unions,
22702281
ItemType::Enum => ItemSection::Enums,
22712282
ItemType::Function => ItemSection::Functions,
2283+
ItemType::Test => ItemSection::Tests,
22722284
ItemType::TypeAlias => ItemSection::TypeAliases,
22732285
ItemType::Static => ItemSection::Statics,
22742286
ItemType::Constant => ItemSection::Constants,

src/librustdoc/html/render/print_item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
186186
}
187187
}
188188
clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => "Function ",
189+
clean::TestItem(..) => "Test ",
189190
clean::TraitItem(..) => "Trait ",
190191
clean::StructItem(..) => "Struct ",
191192
clean::UnionItem(..) => "Union ",
@@ -254,7 +255,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
254255

255256
match &*item.kind {
256257
clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items),
257-
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => {
258+
clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) | clean::TestItem(ref f) => {
258259
item_function(buf, cx, item, f)
259260
}
260261
clean::TraitItem(ref t) => item_trait(buf, cx, item, t),
@@ -331,6 +332,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
331332
ItemType::Static => 8,
332333
ItemType::Trait => 9,
333334
ItemType::Function => 10,
335+
ItemType::Test => 11,
334336
ItemType::TypeAlias => 12,
335337
ItemType::Union => 13,
336338
_ => 14 + ty as u8,

src/librustdoc/html/static/js/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ function preLoadCss(cssUrl) {
554554
block("static", "static", "Statics");
555555
block("trait", "traits", "Traits");
556556
block("fn", "functions", "Functions");
557+
block("test", "tests", "Tests");
557558
block("type", "types", "Type Aliases");
558559
block("union", "unions", "Unions");
559560
// No point, because these items don't appear in modules

src/librustdoc/json/conversions.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
309309
StructFieldItem(f) => ItemEnum::StructField(f.into_tcx(tcx)),
310310
EnumItem(e) => ItemEnum::Enum(e.into_tcx(tcx)),
311311
VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
312-
FunctionItem(f) => ItemEnum::Function(from_function(f, true, header.unwrap(), tcx)),
312+
FunctionItem(f) | TestItem(f) => {
313+
ItemEnum::Function(from_function(f, true, header.unwrap(), tcx))
314+
}
313315
ForeignFunctionItem(f) => ItemEnum::Function(from_function(f, false, header.unwrap(), tcx)),
314316
TraitItem(t) => ItemEnum::Trait((*t).into_tcx(tcx)),
315317
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
@@ -844,7 +846,7 @@ impl FromWithTcx<ItemType> for ItemKind {
844846
Struct => ItemKind::Struct,
845847
Union => ItemKind::Union,
846848
Enum => ItemKind::Enum,
847-
Function | TyMethod | Method => ItemKind::Function,
849+
Function | Test | TyMethod | Method => ItemKind::Function,
848850
TypeAlias => ItemKind::TypeAlias,
849851
OpaqueTy => ItemKind::OpaqueTy,
850852
Static => ItemKind::Static,

src/librustdoc/passes/stripper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
5555
| clean::EnumItem(..)
5656
| clean::TraitItem(..)
5757
| clean::FunctionItem(..)
58+
| clean::TestItem(..)
5859
| clean::VariantItem(..)
5960
| clean::ForeignFunctionItem(..)
6061
| clean::ForeignStaticItem(..)

src/librustdoc/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) trait DocVisitor: Sized {
2525
ExternCrateItem { src: _ }
2626
| ImportItem(_)
2727
| FunctionItem(_)
28+
| TestItem(_)
2829
| TypeAliasItem(_)
2930
| OpaqueTyItem(_)
3031
| StaticItem(_)

0 commit comments

Comments
 (0)