Skip to content

Commit 5c0d880

Browse files
authored
Rollup merge of #83415 - camelid:remove-crate-module-option, r=jyn514
Remove unnecessary `Option` wrapping around `Crate.module` I'm wondering if it was originally there so that we could `take` the module which enables `after_krate` to take an `&Crate`. However, the two impls of `after_krate` only use `Crate.name`, so we can pass just the name instead.
2 parents 7843771 + a7f902b commit 5c0d880

File tree

12 files changed

+61
-85
lines changed

12 files changed

+61
-85
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl Clean<Item> for doctree::Module<'_> {
231231

232232
let what_rustc_thinks = Item::from_hir_id_and_parts(
233233
self.id,
234-
self.name,
234+
Some(self.name),
235235
ModuleItem(Module { is_crate: self.is_crate, items }),
236236
cx,
237237
);

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ thread_local!(crate static MAX_DEF_IDX: RefCell<FxHashMap<CrateNum, DefIndex>> =
5151
crate struct Crate {
5252
crate name: Symbol,
5353
crate src: FileName,
54-
crate module: Option<Item>,
54+
crate module: Item,
5555
crate externs: Vec<(CrateNum, ExternalCrate)>,
5656
crate primitives: Vec<(DefId, PrimitiveType)>,
5757
// These are later on moved into `CACHEKEY`, leaving the map empty.

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
7676
Crate {
7777
name,
7878
src,
79-
module: Some(module),
79+
module,
8080
externs,
8181
primitives,
8282
external_traits: cx.external_traits.clone(),

src/librustdoc/core.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,19 @@ crate fn run_global_ctxt(
474474

475475
let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt));
476476

477-
if let Some(ref m) = krate.module {
478-
if m.doc_value().map(|d| d.is_empty()).unwrap_or(true) {
479-
let help = "The following guide may be of use:\n\
477+
if krate.module.doc_value().map(|d| d.is_empty()).unwrap_or(true) {
478+
let help = "The following guide may be of use:\n\
480479
https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html";
481-
tcx.struct_lint_node(
482-
crate::lint::MISSING_CRATE_LEVEL_DOCS,
483-
DocContext::as_local_hir_id(tcx, m.def_id).unwrap(),
484-
|lint| {
485-
let mut diag =
486-
lint.build("no documentation found for this crate's top-level module");
487-
diag.help(help);
488-
diag.emit();
489-
},
490-
);
491-
}
480+
tcx.struct_lint_node(
481+
crate::lint::MISSING_CRATE_LEVEL_DOCS,
482+
DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(),
483+
|lint| {
484+
let mut diag =
485+
lint.build("no documentation found for this crate's top-level module");
486+
diag.help(help);
487+
diag.emit();
488+
},
489+
);
492490
}
493491

494492
fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler, sp: Span) {
@@ -531,7 +529,7 @@ crate fn run_global_ctxt(
531529

532530
// Process all of the crate attributes, extracting plugin metadata along
533531
// with the passes which we are supposed to run.
534-
for attr in krate.module.as_ref().unwrap().attrs.lists(sym::doc) {
532+
for attr in krate.module.attrs.lists(sym::doc) {
535533
let diag = ctxt.sess().diagnostic();
536534

537535
let name = attr.name_or_empty();

src/librustdoc/doctree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_span::{self, Span, Symbol};
55
use rustc_hir as hir;
66

77
crate struct Module<'hir> {
8-
crate name: Option<Symbol>,
8+
crate name: Symbol,
99
crate where_outer: Span,
1010
crate where_inner: Span,
1111
crate mods: Vec<Module<'hir>>,
@@ -18,7 +18,7 @@ crate struct Module<'hir> {
1818
}
1919

2020
impl Module<'hir> {
21-
crate fn new(name: Option<Symbol>) -> Module<'hir> {
21+
crate fn new(name: Symbol) -> Module<'hir> {
2222
Module {
2323
name,
2424
id: hir::CRATE_HIR_ID,

src/librustdoc/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ crate trait DocFolder: Sized {
8787
}
8888

8989
fn fold_crate(&mut self, mut c: Crate) -> Crate {
90-
c.module = c.module.take().and_then(|module| self.fold_item(module));
90+
c.module = self.fold_item(c.module).unwrap();
9191

9292
{
9393
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };

src/librustdoc/formats/renderer.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::ty::TyCtxt;
2-
use rustc_span::edition::Edition;
2+
use rustc_span::{edition::Edition, Symbol};
33

44
use crate::clean;
55
use crate::config::RenderOptions;
@@ -40,7 +40,7 @@ crate trait FormatRenderer<'tcx>: Sized {
4040
/// A handler is available if the renderer wants to report errors.
4141
fn after_krate(
4242
&mut self,
43-
krate: &clean::Crate,
43+
crate_name: Symbol,
4444
diag: &rustc_errors::Handler,
4545
) -> Result<(), Error>;
4646

@@ -58,21 +58,15 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
5858
) -> Result<(), Error> {
5959
let prof = &tcx.sess.prof;
6060

61-
let (mut format_renderer, mut krate) = prof
61+
let (mut format_renderer, krate) = prof
6262
.extra_verbose_generic_activity("create_renderer", T::descr())
6363
.run(|| T::init(krate, options, edition, cache, tcx))?;
6464

65-
let mut item = match krate.module.take() {
66-
Some(i) => i,
67-
None => return Ok(()),
68-
};
69-
70-
item.name = Some(krate.name);
71-
7265
// Render the crate documentation
73-
let mut work = vec![(format_renderer.make_child_renderer(), item)];
66+
let crate_name = krate.name;
67+
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
7468

75-
let unknown = rustc_span::Symbol::intern("<unknown item>");
69+
let unknown = Symbol::intern("<unknown item>");
7670
while let Some((mut cx, item)) = work.pop() {
7771
if item.is_mod() {
7872
// modules are special because they add a namespace. We also need to
@@ -102,5 +96,5 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
10296
}
10397
}
10498
prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
105-
.run(|| format_renderer.after_krate(&krate, diag))
99+
.run(|| format_renderer.after_krate(crate_name, diag))
106100
}

src/librustdoc/html/render/cache.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,8 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
127127
crate_items.push(&*item);
128128
}
129129

130-
let crate_doc = krate
131-
.module
132-
.as_ref()
133-
.map(|module| module.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)))
134-
.unwrap_or_default();
130+
let crate_doc =
131+
krate.module.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s));
135132

136133
struct CrateData<'a> {
137134
doc: String,

src/librustdoc/html/render/context.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
1212
use rustc_span::edition::Edition;
1313
use rustc_span::source_map::FileName;
14-
use rustc_span::symbol::sym;
14+
use rustc_span::{symbol::sym, Symbol};
1515

1616
use super::cache::{build_index, ExternalLocation};
1717
use super::print_item::{full_path, item_path, print_item};
@@ -343,29 +343,27 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
343343

344344
// Crawl the crate attributes looking for attributes which control how we're
345345
// going to emit HTML
346-
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
347-
for attr in attrs.lists(sym::doc) {
348-
match (attr.name_or_empty(), attr.value_str()) {
349-
(sym::html_favicon_url, Some(s)) => {
350-
layout.favicon = s.to_string();
351-
}
352-
(sym::html_logo_url, Some(s)) => {
353-
layout.logo = s.to_string();
354-
}
355-
(sym::html_playground_url, Some(s)) => {
356-
playground = Some(markdown::Playground {
357-
crate_name: Some(krate.name.to_string()),
358-
url: s.to_string(),
359-
});
360-
}
361-
(sym::issue_tracker_base_url, Some(s)) => {
362-
issue_tracker_base_url = Some(s.to_string());
363-
}
364-
(sym::html_no_source, None) if attr.is_word() => {
365-
include_sources = false;
366-
}
367-
_ => {}
346+
for attr in krate.module.attrs.lists(sym::doc) {
347+
match (attr.name_or_empty(), attr.value_str()) {
348+
(sym::html_favicon_url, Some(s)) => {
349+
layout.favicon = s.to_string();
350+
}
351+
(sym::html_logo_url, Some(s)) => {
352+
layout.logo = s.to_string();
353+
}
354+
(sym::html_playground_url, Some(s)) => {
355+
playground = Some(markdown::Playground {
356+
crate_name: Some(krate.name.to_string()),
357+
url: s.to_string(),
358+
});
359+
}
360+
(sym::issue_tracker_base_url, Some(s)) => {
361+
issue_tracker_base_url = Some(s.to_string());
362+
}
363+
(sym::html_no_source, None) if attr.is_word() => {
364+
include_sources = false;
368365
}
366+
_ => {}
369367
}
370368
}
371369
let (sender, receiver) = channel();
@@ -447,12 +445,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
447445

448446
fn after_krate(
449447
&mut self,
450-
krate: &clean::Crate,
448+
crate_name: Symbol,
451449
diag: &rustc_errors::Handler,
452450
) -> Result<(), Error> {
453-
let final_file = self.dst.join(&*krate.name.as_str()).join("all.html");
451+
let final_file = self.dst.join(&*crate_name.as_str()).join("all.html");
454452
let settings_file = self.dst.join("settings.html");
455-
let crate_name = krate.name;
456453

457454
let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
458455
if !root_path.ends_with('/') {
@@ -515,9 +512,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
515512
if let Some(ref redirections) = self.shared.redirections {
516513
if !redirections.borrow().is_empty() {
517514
let redirect_map_path =
518-
self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
515+
self.dst.join(&*crate_name.as_str()).join("redirect-map.json");
519516
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
520-
self.shared.ensure_dir(&self.dst.join(&*krate.name.as_str()))?;
517+
self.shared.ensure_dir(&self.dst.join(&*crate_name.as_str()))?;
521518
self.shared.fs.write(&redirect_map_path, paths.as_bytes())?;
522519
}
523520
}

src/librustdoc/json/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::rc::Rc;
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_session::Session;
17-
use rustc_span::edition::Edition;
17+
use rustc_span::{edition::Edition, Symbol};
1818

1919
use rustdoc_json_types as types;
2020

@@ -202,7 +202,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
202202

203203
fn after_krate(
204204
&mut self,
205-
_krate: &clean::Crate,
205+
_crate_name: Symbol,
206206
_diag: &rustc_errors::Handler,
207207
) -> Result<(), Error> {
208208
debug!("Done with crate");

src/librustdoc/passes/collect_trait_impls.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,8 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
131131
}
132132
}
133133

134-
let items = if let Some(ref mut it) = krate.module {
135-
if let ModuleItem(Module { ref mut items, .. }) = *it.kind {
136-
items
137-
} else {
138-
panic!("collect-trait-impls can't run");
139-
}
134+
let items = if let ModuleItem(Module { ref mut items, .. }) = *krate.module.kind {
135+
items
140136
} else {
141137
panic!("collect-trait-impls can't run");
142138
};

src/librustdoc/visit_ast.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7676
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public },
7777
hir::CRATE_HIR_ID,
7878
&krate.item.module,
79-
Some(self.cx.tcx.crate_name),
79+
self.cx.tcx.crate_name,
8080
);
8181
top_level_module.is_crate = true;
8282
// Attach the crate's exported macros to the top-level module.
@@ -114,7 +114,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
114114
_ => continue 'exported_macros,
115115
};
116116
// Descend into the child module that matches this path segment (if any).
117-
match cur_mod.mods.iter_mut().find(|child| child.name == Some(path_segment_ty_ns)) {
117+
match cur_mod.mods.iter_mut().find(|child| child.name == path_segment_ty_ns) {
118118
Some(child_mod) => cur_mod = &mut *child_mod,
119119
None => continue 'exported_macros,
120120
}
@@ -133,7 +133,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
133133
vis: &'tcx hir::Visibility<'_>,
134134
id: hir::HirId,
135135
m: &'tcx hir::Mod<'tcx>,
136-
name: Option<Symbol>,
136+
name: Symbol,
137137
) -> Module<'tcx> {
138138
let mut om = Module::new(name);
139139
om.where_outer = span;
@@ -312,13 +312,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
312312
om.items.push((item, renamed))
313313
}
314314
hir::ItemKind::Mod(ref m) => {
315-
om.mods.push(self.visit_mod_contents(
316-
item.span,
317-
&item.vis,
318-
item.hir_id(),
319-
m,
320-
Some(name),
321-
));
315+
om.mods.push(self.visit_mod_contents(item.span, &item.vis, item.hir_id(), m, name));
322316
}
323317
hir::ItemKind::Fn(..)
324318
| hir::ItemKind::ExternCrate(..)

0 commit comments

Comments
 (0)