Skip to content
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

Make fold_item_recur non-nullable #79310

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
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
13 changes: 5 additions & 8 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl StripItem {

crate trait DocFolder: Sized {
fn fold_item(&mut self, item: Item) -> Option<Item> {
self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}

/// don't override!
Expand Down Expand Up @@ -71,15 +71,12 @@ crate trait DocFolder: Sized {
}

/// don't override!
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
let Item { attrs, name, source, visibility, def_id, kind, stability, deprecation } = item;

let kind = match kind {
fn fold_item_recur(&mut self, mut item: Item) -> Item {
item.kind = match item.kind {
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
_ => self.fold_inner_recur(kind),
_ => self.fold_inner_recur(item.kind),
};

Some(Item { attrs, name, source, kind, visibility, stability, deprecation, def_id })
item
}

fn fold_mod(&mut self, m: Module) -> Module {
Expand Down
77 changes: 37 additions & 40 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,55 +421,52 @@ impl DocFolder for Cache {

// Once we've recursively found all the generics, hoard off all the
// implementations elsewhere.
let ret = self.fold_item_recur(item).and_then(|item| {
if let clean::Item { kind: clean::ImplItem(_), .. } = item {
// Figure out the id of this impl. This may map to a
// primitive rather than always to a struct/enum.
// Note: matching twice to restrict the lifetime of the `i` borrow.
let mut dids = FxHashSet::default();
if let clean::Item { kind: clean::ImplItem(ref i), .. } = item {
match i.for_ {
clean::ResolvedPath { did, .. }
| clean::BorrowedRef {
type_: box clean::ResolvedPath { did, .. }, ..
} => {
dids.insert(did);
}
ref t => {
let did = t
.primitive_type()
.and_then(|t| self.primitive_locations.get(&t).cloned());
let item = self.fold_item_recur(item);
let ret = if let clean::Item { kind: clean::ImplItem(_), .. } = item {
// Figure out the id of this impl. This may map to a
// primitive rather than always to a struct/enum.
// Note: matching twice to restrict the lifetime of the `i` borrow.
let mut dids = FxHashSet::default();
if let clean::Item { kind: clean::ImplItem(ref i), .. } = item {
match i.for_ {
clean::ResolvedPath { did, .. }
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
dids.insert(did);
}
ref t => {
let did = t
.primitive_type()
.and_then(|t| self.primitive_locations.get(&t).cloned());

if let Some(did) = did {
dids.insert(did);
}
if let Some(did) = did {
dids.insert(did);
}
}
}

if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
for bound in generics {
if let Some(did) = bound.def_id() {
dids.insert(did);
}
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
for bound in generics {
if let Some(did) = bound.def_id() {
dids.insert(did);
}
}
} else {
unreachable!()
};
let impl_item = Impl { impl_item: item };
if impl_item.trait_did().map_or(true, |d| self.traits.contains_key(&d)) {
for did in dids {
self.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
}
} else {
let trait_did = impl_item.trait_did().expect("no trait did");
self.orphan_trait_impls.push((trait_did, dids, impl_item));
}
None
} else {
Some(item)
unreachable!()
};
let impl_item = Impl { impl_item: item };
if impl_item.trait_did().map_or(true, |d| self.traits.contains_key(&d)) {
for did in dids {
self.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
}
} else {
let trait_did = impl_item.trait_did().expect("no trait did");
self.orphan_trait_impls.push((trait_did, dids, impl_item));
}
});
None
} else {
Some(item)
};

if pushed {
self.stack.pop().expect("stack already empty");
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> DocFolder for SourceCollector<'a> {
}
};
}
self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/calculate_doc_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,6 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
}
}

self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}
2 changes: 1 addition & 1 deletion src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
}
}

self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/collapse_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Collapser;
impl fold::DocFolder for Collapser {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
i.attrs.collapse_doc_comments();
self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
// we don't display docs on `extern crate` items anyway, so don't process them.
clean::ExternCrateItem(..) => {
debug!("ignoring extern crate item {:?}", item.def_id);
return self.fold_item_recur(item);
return Some(self.fold_item_recur(item));
}
clean::ImportItem(Import { kind: clean::ImportKind::Simple(ref name, ..), .. }) => {
Some(name.clone())
Expand Down Expand Up @@ -958,7 +958,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
}
}

if item.is_mod() {
Some(if item.is_mod() {
if !item.attrs.inner_docs {
self.mod_ids.push(item.def_id);
}
Expand All @@ -968,7 +968,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
ret
} else {
self.fold_item_recur(item)
}
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
}
}

self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}

Expand All @@ -152,7 +152,7 @@ impl DocFolder for ItemCollector {
fn fold_item(&mut self, i: Item) -> Option<Item> {
self.items.insert(i.def_id);

self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/doc_test_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a, 'tcx> DocFolder for PrivateItemDocTestLinter<'a, 'tcx> {

look_for_tests(&cx, &dox, &item);

self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/html_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
return self.fold_item_recur(item);
return Some(self.fold_item_recur(item));
}
};
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
Expand Down Expand Up @@ -223,6 +223,6 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
}
}

self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}
}
4 changes: 2 additions & 2 deletions src/librustdoc/passes/non_autolinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
return self.fold_item_recur(item);
return Some(self.fold_item_recur(item));
}
};
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
Expand Down Expand Up @@ -133,6 +133,6 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
}
}

self.fold_item_recur(item)
Some(self.fold_item_recur(item))
}
}
2 changes: 1 addition & 1 deletion src/librustdoc/passes/propagate_doc_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ impl DocFolder for CfgPropagator {
let result = self.fold_item_recur(item);
self.parent_cfg = old_parent_cfg;

result
Some(result)
}
}
4 changes: 2 additions & 2 deletions src/librustdoc/passes/strip_hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a> DocFolder for Stripper<'a> {
// strip things like impl methods but when doing so
// we must not add any items to the `retained` set.
let old = mem::replace(&mut self.update_retained, false);
let ret = StripItem(self.fold_item_recur(i).unwrap()).strip();
let ret = StripItem(self.fold_item_recur(i)).strip();
self.update_retained = old;
return ret;
}
Expand All @@ -58,6 +58,6 @@ impl<'a> DocFolder for Stripper<'a> {
self.retained.insert(i.def_id);
}
}
self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}
16 changes: 7 additions & 9 deletions src/librustdoc/passes/stripper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> DocFolder for Stripper<'a> {
let old = mem::replace(&mut self.update_retained, false);
let ret = self.fold_item_recur(i);
self.update_retained = old;
return ret;
return Some(ret);
}
// These items can all get re-exported
clean::OpaqueTyItem(..)
Expand Down Expand Up @@ -59,7 +59,7 @@ impl<'a> DocFolder for Stripper<'a> {
if i.def_id.is_local() && !i.visibility.is_public() {
debug!("Stripper: stripping module {:?}", i.name);
let old = mem::replace(&mut self.update_retained, false);
let ret = StripItem(self.fold_item_recur(i).unwrap()).strip();
let ret = StripItem(self.fold_item_recur(i)).strip();
self.update_retained = old;
return ret;
}
Expand Down Expand Up @@ -107,12 +107,10 @@ impl<'a> DocFolder for Stripper<'a> {
self.fold_item_recur(i)
};

if let Some(ref i) = i {
if self.update_retained {
self.retained.insert(i.def_id);
}
if self.update_retained {
self.retained.insert(i.def_id);
}
i
Some(i)
}
}

Expand Down Expand Up @@ -153,7 +151,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
}
}
}
self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}

Expand All @@ -164,7 +162,7 @@ impl DocFolder for ImportStripper {
fn fold_item(&mut self, i: Item) -> Option<Item> {
match i.kind {
clean::ExternCrateItem(..) | clean::ImportItem(..) if !i.visibility.is_public() => None,
_ => self.fold_item_recur(i),
_ => Some(self.fold_item_recur(i)),
}
}
}
2 changes: 1 addition & 1 deletion src/librustdoc/passes/unindent_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct CommentCleaner;
impl fold::DocFolder for CommentCleaner {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
i.attrs.unindent_doc_comments();
self.fold_item_recur(i)
Some(self.fold_item_recur(i))
}
}

Expand Down