Skip to content

Commit e2b6f4c

Browse files
Move top-level Clean impl to function
This allows us to pass it a `&mut DocContext` which will allow removal of RefCells, etc. in the following commits. It's also somewhat a unique Clean impl in that it previously ignored `self` (re-retriveing hir::Crate), which it no longer needs to do.
1 parent a77247a commit e2b6f4c

File tree

2 files changed

+83
-87
lines changed

2 files changed

+83
-87
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 80 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -136,94 +136,90 @@ pub struct Crate {
136136
pub collapsed: bool,
137137
}
138138

139-
impl Clean<Crate> for hir::Crate {
140-
// note that self here is ignored in favor of `cx.tcx.hir().krate()` since
141-
// that gets around tying self's lifetime to the '_ in cx.
142-
fn clean(&self, cx: &DocContext<'_>) -> Crate {
143-
use crate::visit_lib::LibEmbargoVisitor;
144-
145-
let v = crate::visit_ast::RustdocVisitor::new(&cx);
146-
let module = v.visit(cx.tcx.hir().krate());
147-
148-
{
149-
let mut r = cx.renderinfo.borrow_mut();
150-
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
151-
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
152-
r.owned_box_did = cx.tcx.lang_items().owned_box();
153-
}
154-
155-
let mut externs = Vec::new();
156-
for &cnum in cx.tcx.crates().iter() {
157-
externs.push((cnum, cnum.clean(cx)));
158-
// Analyze doc-reachability for extern items
159-
LibEmbargoVisitor::new(cx).visit_lib(cnum);
160-
}
161-
externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
162-
163-
// Clean the crate, translating the entire libsyntax AST to one that is
164-
// understood by rustdoc.
165-
let mut module = module.clean(cx);
166-
let mut masked_crates = FxHashSet::default();
167-
168-
match module.inner {
169-
ModuleItem(ref module) => {
170-
for it in &module.items {
171-
// `compiler_builtins` should be masked too, but we can't apply
172-
// `#[doc(masked)]` to the injected `extern crate` because it's unstable.
173-
if it.is_extern_crate()
174-
&& (it.attrs.has_doc_flag(sym::masked)
175-
|| cx.tcx.is_compiler_builtins(it.def_id.krate))
176-
{
177-
masked_crates.insert(it.def_id.krate);
178-
}
179-
}
180-
}
181-
_ => unreachable!(),
182-
}
139+
pub fn krate(cx: &mut DocContext<'a>) -> Crate {
140+
use crate::visit_lib::LibEmbargoVisitor;
183141

184-
let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
185-
{
186-
let m = match module.inner {
187-
ModuleItem(ref mut m) => m,
188-
_ => unreachable!(),
189-
};
190-
m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
191-
Item {
192-
source: Span::empty(),
193-
name: Some(prim.to_url_str().to_string()),
194-
attrs: attrs.clone(),
195-
visibility: Some(Public),
196-
stability: get_stability(cx, def_id),
197-
deprecation: get_deprecation(cx, def_id),
198-
def_id,
199-
inner: PrimitiveItem(prim),
200-
}
201-
}));
202-
m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
203-
Item {
204-
source: Span::empty(),
205-
name: Some(kw.clone()),
206-
attrs: attrs,
207-
visibility: Some(Public),
208-
stability: get_stability(cx, def_id),
209-
deprecation: get_deprecation(cx, def_id),
210-
def_id,
211-
inner: KeywordItem(kw),
142+
let v = crate::visit_ast::RustdocVisitor::new(&cx);
143+
let module = v.visit(cx.tcx.hir().krate());
144+
145+
{
146+
let mut r = cx.renderinfo.borrow_mut();
147+
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
148+
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
149+
r.owned_box_did = cx.tcx.lang_items().owned_box();
150+
}
151+
152+
let mut externs = Vec::new();
153+
for &cnum in cx.tcx.crates().iter() {
154+
externs.push((cnum, cnum.clean(cx)));
155+
// Analyze doc-reachability for extern items
156+
LibEmbargoVisitor::new(cx).visit_lib(cnum);
157+
}
158+
externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
159+
160+
// Clean the crate, translating the entire libsyntax AST to one that is
161+
// understood by rustdoc.
162+
let mut module = module.clean(cx);
163+
let mut masked_crates = FxHashSet::default();
164+
165+
match module.inner {
166+
ModuleItem(ref module) => {
167+
for it in &module.items {
168+
// `compiler_builtins` should be masked too, but we can't apply
169+
// `#[doc(masked)]` to the injected `extern crate` because it's unstable.
170+
if it.is_extern_crate()
171+
&& (it.attrs.has_doc_flag(sym::masked)
172+
|| cx.tcx.is_compiler_builtins(it.def_id.krate))
173+
{
174+
masked_crates.insert(it.def_id.krate);
212175
}
213-
}));
176+
}
214177
}
178+
_ => unreachable!(),
179+
}
215180

216-
Crate {
217-
name,
218-
version: None,
219-
src,
220-
module: Some(module),
221-
externs,
222-
primitives,
223-
external_traits: cx.external_traits.clone(),
224-
masked_crates,
225-
collapsed: false,
226-
}
181+
let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
182+
{
183+
let m = match module.inner {
184+
ModuleItem(ref mut m) => m,
185+
_ => unreachable!(),
186+
};
187+
m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| {
188+
Item {
189+
source: Span::empty(),
190+
name: Some(prim.to_url_str().to_string()),
191+
attrs: attrs.clone(),
192+
visibility: Some(Public),
193+
stability: get_stability(cx, def_id),
194+
deprecation: get_deprecation(cx, def_id),
195+
def_id,
196+
inner: PrimitiveItem(prim),
197+
}
198+
}));
199+
m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| {
200+
Item {
201+
source: Span::empty(),
202+
name: Some(kw.clone()),
203+
attrs: attrs,
204+
visibility: Some(Public),
205+
stability: get_stability(cx, def_id),
206+
deprecation: get_deprecation(cx, def_id),
207+
def_id,
208+
inner: KeywordItem(kw),
209+
}
210+
}));
211+
}
212+
213+
Crate {
214+
name,
215+
version: None,
216+
src,
217+
module: Some(module),
218+
externs,
219+
primitives,
220+
external_traits: cx.external_traits.clone(),
221+
masked_crates,
222+
collapsed: false,
227223
}
228224
}
229225

src/librustdoc/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::rc::Rc;
3030

3131
use crate::config::{Options as RustdocOptions, RenderOptions};
3232
use crate::clean;
33-
use crate::clean::{Clean, MAX_DEF_ID, AttributesExt};
33+
use crate::clean::{MAX_DEF_ID, AttributesExt};
3434
use crate::html::render::RenderInfo;
3535

3636
use crate::passes;
@@ -363,7 +363,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
363363
let mut renderinfo = RenderInfo::default();
364364
renderinfo.access_levels = access_levels;
365365

366-
let ctxt = DocContext {
366+
let mut ctxt = DocContext {
367367
tcx,
368368
resolver,
369369
cstore: compiler.cstore().clone(),
@@ -383,7 +383,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
383383
};
384384
debug!("crate: {:?}", tcx.hir().krate());
385385

386-
let mut krate = tcx.hir().krate().clean(&ctxt);
386+
let mut krate = clean::krate(&mut ctxt);
387387

388388
fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
389389
let mut msg = diag.struct_warn(&format!("the `#![doc({})]` attribute is \

0 commit comments

Comments
 (0)