Skip to content

Remove unnecessary Option wrapping around Crate.module #83415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
@@ -231,7 +231,7 @@ impl Clean<Item> for doctree::Module<'_> {

let what_rustc_thinks = Item::from_hir_id_and_parts(
self.id,
self.name,
Some(self.name),
ModuleItem(Module { is_crate: self.is_crate, items }),
cx,
);
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ thread_local!(crate static MAX_DEF_IDX: RefCell<FxHashMap<CrateNum, DefIndex>> =
crate struct Crate {
crate name: Symbol,
crate src: FileName,
crate module: Option<Item>,
crate module: Item,
crate externs: Vec<(CrateNum, ExternalCrate)>,
crate primitives: Vec<(DefId, PrimitiveType)>,
// These are later on moved into `CACHEKEY`, leaving the map empty.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
Crate {
name,
src,
module: Some(module),
module,
externs,
primitives,
external_traits: cx.external_traits.clone(),
28 changes: 13 additions & 15 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
@@ -474,21 +474,19 @@ crate fn run_global_ctxt(

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

if let Some(ref m) = krate.module {
if m.doc_value().map(|d| d.is_empty()).unwrap_or(true) {
let help = "The following guide may be of use:\n\
if krate.module.doc_value().map(|d| d.is_empty()).unwrap_or(true) {
let help = "The following guide may be of use:\n\
https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html";
tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS,
DocContext::as_local_hir_id(tcx, m.def_id).unwrap(),
|lint| {
let mut diag =
lint.build("no documentation found for this crate's top-level module");
diag.help(help);
diag.emit();
},
);
}
tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS,
DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(),
|lint| {
let mut diag =
lint.build("no documentation found for this crate's top-level module");
diag.help(help);
diag.emit();
},
);
}

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

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

let name = attr.name_or_empty();
4 changes: 2 additions & 2 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use rustc_span::{self, Span, Symbol};
use rustc_hir as hir;

crate struct Module<'hir> {
crate name: Option<Symbol>,
crate name: Symbol,
crate where_outer: Span,
crate where_inner: Span,
crate mods: Vec<Module<'hir>>,
@@ -18,7 +18,7 @@ crate struct Module<'hir> {
}

impl Module<'hir> {
crate fn new(name: Option<Symbol>) -> Module<'hir> {
crate fn new(name: Symbol) -> Module<'hir> {
Module {
name,
id: hir::CRATE_HIR_ID,
2 changes: 1 addition & 1 deletion src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ crate trait DocFolder: Sized {
}

fn fold_crate(&mut self, mut c: Crate) -> Crate {
c.module = c.module.take().and_then(|module| self.fold_item(module));
c.module = self.fold_item(c.module).unwrap();
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there ever a case where this unwrap will fail? Why does fold_item return an Option anyway?

Copy link
Member

Choose a reason for hiding this comment

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

fold_item returns None if the itme was stripped. I don't think the whole crate can be stripped so this should be fine.


{
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
2 changes: 0 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
@@ -182,8 +182,6 @@ impl Cache {
self.primitive_locations.insert(prim, def_id);
}

self.stack.push(krate.name.to_string());

krate = CacheBuilder { tcx, cache: self, empty_cache: Cache::default() }.fold_crate(krate);

for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
20 changes: 7 additions & 13 deletions src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_middle::ty::TyCtxt;
use rustc_span::edition::Edition;
use rustc_span::{edition::Edition, Symbol};

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

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

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

let mut item = match krate.module.take() {
Some(i) => i,
None => return Ok(()),
};

item.name = Some(krate.name);

// Render the crate documentation
let mut work = vec![(format_renderer.make_child_renderer(), item)];
let crate_name = krate.name;
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];

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

Choose a reason for hiding this comment

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

It took me a second to realize: the reason this changed is because you partially moved out of krate when adding krate.module to the work list.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I mentioned that in the PR description:

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.

Copy link
Member

Choose a reason for hiding this comment

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

Right - that explains that it's possible, the krate.module thing explains why it's necessary. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I should've included more information :)

}
7 changes: 2 additions & 5 deletions src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
@@ -127,11 +127,8 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
crate_items.push(&*item);
}

let crate_doc = krate
.module
.as_ref()
.map(|module| module.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)))
.unwrap_or_default();
let crate_doc =
krate.module.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s));

struct CrateData<'a> {
doc: String,
53 changes: 25 additions & 28 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::source_map::FileName;
use rustc_span::symbol::sym;
use rustc_span::{symbol::sym, Symbol};

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

// Crawl the crate attributes looking for attributes which control how we're
// going to emit HTML
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
for attr in attrs.lists(sym::doc) {
match (attr.name_or_empty(), attr.value_str()) {
(sym::html_favicon_url, Some(s)) => {
layout.favicon = s.to_string();
}
(sym::html_logo_url, Some(s)) => {
layout.logo = s.to_string();
}
(sym::html_playground_url, Some(s)) => {
playground = Some(markdown::Playground {
crate_name: Some(krate.name.to_string()),
url: s.to_string(),
});
}
(sym::issue_tracker_base_url, Some(s)) => {
issue_tracker_base_url = Some(s.to_string());
}
(sym::html_no_source, None) if attr.is_word() => {
include_sources = false;
}
_ => {}
for attr in krate.module.attrs.lists(sym::doc) {
match (attr.name_or_empty(), attr.value_str()) {
(sym::html_favicon_url, Some(s)) => {
layout.favicon = s.to_string();
}
(sym::html_logo_url, Some(s)) => {
layout.logo = s.to_string();
}
(sym::html_playground_url, Some(s)) => {
playground = Some(markdown::Playground {
crate_name: Some(krate.name.to_string()),
url: s.to_string(),
});
}
(sym::issue_tracker_base_url, Some(s)) => {
issue_tracker_base_url = Some(s.to_string());
}
(sym::html_no_source, None) if attr.is_word() => {
include_sources = false;
}
_ => {}
}
}
let (sender, receiver) = channel();
@@ -447,12 +445,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {

fn after_krate(
&mut self,
krate: &clean::Crate,
crate_name: Symbol,
diag: &rustc_errors::Handler,
) -> Result<(), Error> {
let final_file = self.dst.join(&*krate.name.as_str()).join("all.html");
let final_file = self.dst.join(&*crate_name.as_str()).join("all.html");
let settings_file = self.dst.join("settings.html");
let crate_name = krate.name;

let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
if !root_path.ends_with('/') {
@@ -515,9 +512,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
if let Some(ref redirections) = self.shared.redirections {
if !redirections.borrow().is_empty() {
let redirect_map_path =
self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
self.dst.join(&*crate_name.as_str()).join("redirect-map.json");
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
self.shared.ensure_dir(&self.dst.join(&*krate.name.as_str()))?;
self.shared.ensure_dir(&self.dst.join(&*crate_name.as_str()))?;
self.shared.fs.write(&redirect_map_path, paths.as_bytes())?;
}
}
4 changes: 2 additions & 2 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ use std::rc::Rc;
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::{edition::Edition, Symbol};

use rustdoc_json_types as types;

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

fn after_krate(
&mut self,
_krate: &clean::Crate,
_crate_name: Symbol,
_diag: &rustc_errors::Handler,
) -> Result<(), Error> {
debug!("Done with crate");
8 changes: 2 additions & 6 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
@@ -131,12 +131,8 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
}
}

let items = if let Some(ref mut it) = krate.module {
if let ModuleItem(Module { ref mut items, .. }) = *it.kind {
items
} else {
panic!("collect-trait-impls can't run");
}
let items = if let ModuleItem(Module { ref mut items, .. }) = *krate.module.kind {
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems like a future enhancement could be to store a Module instead of an Item. Probably we would need to add a few more fields to Module (like DefId and name) to do that.

items
} else {
panic!("collect-trait-impls can't run");
};
14 changes: 4 additions & 10 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID,
&krate.item.module,
None,
self.cx.tcx.crate_name,
);
top_level_module.is_crate = true;
// Attach the crate's exported macros to the top-level module.
@@ -114,7 +114,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
_ => continue 'exported_macros,
};
// Descend into the child module that matches this path segment (if any).
match cur_mod.mods.iter_mut().find(|child| child.name == Some(path_segment_ty_ns)) {
match cur_mod.mods.iter_mut().find(|child| child.name == path_segment_ty_ns) {
Some(child_mod) => cur_mod = &mut *child_mod,
None => continue 'exported_macros,
}
@@ -133,7 +133,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
vis: &'tcx hir::Visibility<'_>,
id: hir::HirId,
m: &'tcx hir::Mod<'tcx>,
name: Option<Symbol>,
name: Symbol,
) -> Module<'tcx> {
let mut om = Module::new(name);
om.where_outer = span;
@@ -312,13 +312,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
om.items.push((item, renamed))
}
hir::ItemKind::Mod(ref m) => {
om.mods.push(self.visit_mod_contents(
item.span,
&item.vis,
item.hir_id(),
m,
Some(name),
));
om.mods.push(self.visit_mod_contents(item.span, &item.vis, item.hir_id(), m, name));
}
hir::ItemKind::Fn(..)
| hir::ItemKind::ExternCrate(..)
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/intra-doc/private-from-crate-level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// check-pass

//! [my_module]
//~^ WARN public documentation for `private_from_crate_level` links to private item `my_module`

mod my_module {}
11 changes: 11 additions & 0 deletions src/test/rustdoc-ui/intra-doc/private-from-crate-level.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: public documentation for `private_from_crate_level` links to private item `my_module`
--> $DIR/private-from-crate-level.rs:3:6
|
LL | //! [my_module]
| ^^^^^^^^^ this item is private
|
= note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
= note: this link will resolve properly if you pass `--document-private-items`

warning: 1 warning emitted