Skip to content

Commit 6da9e3c

Browse files
committed
Take &mut DocContext in passes
This should hopefully allow for less interior mutability.
1 parent 8fe989d commit 6da9e3c

14 files changed

+49
-83
lines changed

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ crate fn run_global_ctxt(
627627
};
628628
if run {
629629
debug!("running pass {}", p.pass.name);
630-
krate = ctxt.tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &ctxt));
630+
krate = ctxt.tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &mut ctxt));
631631
}
632632
}
633633

src/librustdoc/passes/calculate_doc_coverage.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ crate const CALCULATE_DOC_COVERAGE: Pass = Pass {
2020
description: "counts the number of items with and without documentation",
2121
};
2222

23-
fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::Crate {
24-
let mut calc = CoverageCalculator::new(ctx);
23+
fn calculate_doc_coverage(krate: clean::Crate, ctx: &mut DocContext<'_>) -> clean::Crate {
24+
let mut calc = CoverageCalculator { items: Default::default(), ctx };
2525
let krate = calc.fold_crate(krate);
2626

2727
calc.print_results();
@@ -101,7 +101,7 @@ impl ops::AddAssign for ItemCount {
101101

102102
struct CoverageCalculator<'a, 'b> {
103103
items: BTreeMap<FileName, ItemCount>,
104-
ctx: &'a DocContext<'b>,
104+
ctx: &'a mut DocContext<'b>,
105105
}
106106

107107
fn limit_filename_len(filename: String) -> String {
@@ -115,10 +115,6 @@ fn limit_filename_len(filename: String) -> String {
115115
}
116116

117117
impl<'a, 'b> CoverageCalculator<'a, 'b> {
118-
fn new(ctx: &'a DocContext<'b>) -> CoverageCalculator<'a, 'b> {
119-
CoverageCalculator { items: Default::default(), ctx }
120-
}
121-
122118
fn to_json(&self) -> String {
123119
serde_json::to_string(
124120
&self

src/librustdoc/passes/check_code_block_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ crate const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
1717
description: "validates syntax inside Rust code blocks",
1818
};
1919

20-
crate fn check_code_block_syntax(krate: clean::Crate, cx: &DocContext<'_>) -> clean::Crate {
20+
crate fn check_code_block_syntax(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
2121
SyntaxChecker { cx }.fold_crate(krate)
2222
}
2323

src/librustdoc/passes/collect_intra_doc_links.rs

+26-33
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
4747
description: "resolves intra-doc links",
4848
};
4949

50-
crate fn collect_intra_doc_links(krate: Crate, cx: &DocContext<'_>) -> Crate {
51-
LinkCollector::new(cx).fold_crate(krate)
50+
crate fn collect_intra_doc_links(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
51+
LinkCollector {
52+
cx,
53+
mod_ids: Vec::new(),
54+
kind_side_channel: Cell::new(None),
55+
visited_links: FxHashMap::default(),
56+
}.fold_crate(krate)
5257
}
5358

5459
/// Top-level errors emitted by this pass.
@@ -257,7 +262,7 @@ struct CachedLink {
257262
}
258263

259264
struct LinkCollector<'a, 'tcx> {
260-
cx: &'a DocContext<'tcx>,
265+
cx: &'a mut DocContext<'tcx>,
261266
/// A stack of modules used to decide what scope to resolve in.
262267
///
263268
/// The last module will be used if the parent scope of the current item is
@@ -273,15 +278,6 @@ struct LinkCollector<'a, 'tcx> {
273278
}
274279

275280
impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
276-
fn new(cx: &'a DocContext<'tcx>) -> Self {
277-
LinkCollector {
278-
cx,
279-
mod_ids: Vec::new(),
280-
kind_side_channel: Cell::new(None),
281-
visited_links: FxHashMap::default(),
282-
}
283-
}
284-
285281
/// Given a full link, parse it as an [enum struct variant].
286282
///
287283
/// In particular, this will return an error whenever there aren't three
@@ -293,7 +289,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
293289
path_str: &'path str,
294290
module_id: DefId,
295291
) -> Result<(Res, Option<String>), ErrorKind<'path>> {
296-
let cx = self.cx;
292+
let tcx = self.cx.tcx;
297293
let no_res = || ResolutionFailure::NotResolved {
298294
module_id,
299295
partial_res: None,
@@ -317,7 +313,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
317313
// If there's no third component, we saw `[a::b]` before and it failed to resolve.
318314
// So there's no partial res.
319315
.ok_or_else(no_res)?;
320-
let ty_res = cx
316+
let ty_res = self.cx
321317
.enter_resolver(|resolver| {
322318
resolver.resolve_str_path_error(DUMMY_SP, &path, TypeNS, module_id)
323319
})
@@ -326,18 +322,17 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
326322

327323
match ty_res {
328324
Res::Def(DefKind::Enum, did) => {
329-
if cx
330-
.tcx
325+
if tcx
331326
.inherent_impls(did)
332327
.iter()
333-
.flat_map(|imp| cx.tcx.associated_items(*imp).in_definition_order())
328+
.flat_map(|imp| tcx.associated_items(*imp).in_definition_order())
334329
.any(|item| item.ident.name == variant_name)
335330
{
336331
// This is just to let `fold_item` know that this shouldn't be considered;
337332
// it's a bug for the error to make it to the user
338333
return Err(ResolutionFailure::Dummy.into());
339334
}
340-
match cx.tcx.type_of(did).kind() {
335+
match tcx.type_of(did).kind() {
341336
ty::Adt(def, _) if def.is_enum() => {
342337
if def.all_fields().any(|item| item.ident.name == variant_field_name) {
343338
Ok((
@@ -380,16 +375,16 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
380375
item_name: Symbol,
381376
item_str: &'path str,
382377
) -> Result<(Res, Option<String>), ErrorKind<'path>> {
383-
let cx = self.cx;
378+
let tcx = self.cx.tcx;
384379

385380
prim_ty
386-
.impls(cx.tcx)
381+
.impls(tcx)
387382
.into_iter()
388383
.find_map(|&impl_| {
389-
cx.tcx
384+
tcx
390385
.associated_items(impl_)
391386
.find_by_name_and_namespace(
392-
cx.tcx,
387+
tcx,
393388
Ident::with_dummy_span(item_name),
394389
ns,
395390
impl_,
@@ -434,9 +429,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
434429
path_str: &'a str,
435430
module_id: DefId,
436431
) -> Result<Res, ResolutionFailure<'a>> {
437-
let cx = self.cx;
438432
let path = ast::Path::from_ident(Ident::from_str(path_str));
439-
cx.enter_resolver(|resolver| {
433+
self.cx.enter_resolver(|resolver| {
440434
// FIXME(jynelson): does this really need 3 separate lookups?
441435
if let Ok((Some(ext), res)) = resolver.resolve_macro_path(
442436
&path,
@@ -498,7 +492,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
498492
module_id: DefId,
499493
extra_fragment: &Option<String>,
500494
) -> Result<(Res, Option<String>), ErrorKind<'path>> {
501-
let cx = self.cx;
495+
let cx = &self.cx;
502496

503497
if let Some(res) = self.resolve_path(path_str, ns, module_id) {
504498
match res {
@@ -948,12 +942,11 @@ impl LinkCollector<'_, '_> {
948942
return None;
949943
}
950944

951-
let cx = self.cx;
952945
let link = ori_link.link.replace("`", "");
953946
let parts = link.split('#').collect::<Vec<_>>();
954947
let (link, extra_fragment) = if parts.len() > 2 {
955948
// A valid link can't have multiple #'s
956-
anchor_failure(cx, &item, &link, dox, ori_link.range, AnchorFailure::MultipleAnchors);
949+
anchor_failure(self.cx, &item, &link, dox, ori_link.range, AnchorFailure::MultipleAnchors);
957950
return None;
958951
} else if parts.len() == 2 {
959952
if parts[0].trim().is_empty() {
@@ -1105,7 +1098,7 @@ impl LinkCollector<'_, '_> {
11051098
if matches!(disambiguator, Some(Disambiguator::Primitive)) {
11061099
if fragment.is_some() {
11071100
anchor_failure(
1108-
cx,
1101+
self.cx,
11091102
&item,
11101103
path_str,
11111104
dox,
@@ -1119,7 +1112,7 @@ impl LinkCollector<'_, '_> {
11191112
} else {
11201113
// `[char]` when a `char` module is in scope
11211114
let candidates = vec![res, prim];
1122-
ambiguity_error(cx, &item, path_str, dox, ori_link.range, candidates);
1115+
ambiguity_error(self.cx, &item, path_str, dox, ori_link.range, candidates);
11231116
return None;
11241117
}
11251118
}
@@ -1140,7 +1133,7 @@ impl LinkCollector<'_, '_> {
11401133
suggest_disambiguator(resolved, diag, path_str, dox, sp, &ori_link.range);
11411134
};
11421135
report_diagnostic(
1143-
cx,
1136+
self.cx,
11441137
BROKEN_INTRA_DOC_LINKS,
11451138
&msg,
11461139
&item,
@@ -1187,7 +1180,7 @@ impl LinkCollector<'_, '_> {
11871180
if self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_src)
11881181
&& !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_dst)
11891182
{
1190-
privacy_error(cx, &item, &path_str, dox, &ori_link);
1183+
privacy_error(self.cx, &item, &path_str, dox, &ori_link);
11911184
}
11921185
}
11931186

@@ -1211,7 +1204,7 @@ impl LinkCollector<'_, '_> {
12111204
&& !self.cx.tcx.features().intra_doc_pointers
12121205
{
12131206
let span = super::source_span_for_markdown_range(
1214-
cx,
1207+
self.cx,
12151208
dox,
12161209
&ori_link.range,
12171210
&item.attrs,
@@ -1243,7 +1236,7 @@ impl LinkCollector<'_, '_> {
12431236
}
12441237
Res::Def(kind, id) => {
12451238
verify(kind, id)?;
1246-
let id = clean::register_res(cx, rustc_hir::def::Res::Def(kind, id));
1239+
let id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id));
12471240
Some(ItemLink { link: ori_link.link, link_text, did: Some(id), fragment })
12481241
}
12491242
}

src/librustdoc/passes/collect_trait_impls.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ crate const COLLECT_TRAIT_IMPLS: Pass = Pass {
1414
description: "retrieves trait impls for items in the crate",
1515
};
1616

17-
crate fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
18-
let mut synth = SyntheticImplCollector::new(cx);
17+
crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
18+
let mut synth = SyntheticImplCollector { cx, impls: Vec::new() };
1919
let mut krate = cx.sess().time("collect_synthetic_impls", || synth.fold_crate(krate));
2020

2121
let prims: FxHashSet<PrimitiveType> = krate.primitives.iter().map(|p| p.1).collect();
@@ -164,12 +164,6 @@ struct SyntheticImplCollector<'a, 'tcx> {
164164
impls: Vec<Item>,
165165
}
166166

167-
impl<'a, 'tcx> SyntheticImplCollector<'a, 'tcx> {
168-
fn new(cx: &'a DocContext<'tcx>) -> Self {
169-
SyntheticImplCollector { cx, impls: Vec::new() }
170-
}
171-
}
172-
173167
impl<'a, 'tcx> DocFolder for SyntheticImplCollector<'a, 'tcx> {
174168
fn fold_item(&mut self, i: Item) -> Option<Item> {
175169
if i.is_struct() || i.is_enum() || i.is_union() {

src/librustdoc/passes/doc_test_lints.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,20 @@ crate const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
1919
};
2020

2121
struct PrivateItemDocTestLinter<'a, 'tcx> {
22-
cx: &'a DocContext<'tcx>,
22+
cx: &'a mut DocContext<'tcx>,
2323
}
2424

25-
impl<'a, 'tcx> PrivateItemDocTestLinter<'a, 'tcx> {
26-
fn new(cx: &'a DocContext<'tcx>) -> Self {
27-
PrivateItemDocTestLinter { cx }
28-
}
29-
}
30-
31-
crate fn check_private_items_doc_tests(krate: Crate, cx: &DocContext<'_>) -> Crate {
32-
let mut coll = PrivateItemDocTestLinter::new(cx);
25+
crate fn check_private_items_doc_tests(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
26+
let mut coll = PrivateItemDocTestLinter { cx };
3327

3428
coll.fold_crate(krate)
3529
}
3630

3731
impl<'a, 'tcx> DocFolder for PrivateItemDocTestLinter<'a, 'tcx> {
3832
fn fold_item(&mut self, item: Item) -> Option<Item> {
39-
let cx = self.cx;
4033
let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new);
4134

42-
look_for_tests(&cx, &dox, &item);
35+
look_for_tests(self.cx, &dox, &item);
4336

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

src/librustdoc/passes/html_tags.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@ crate const CHECK_INVALID_HTML_TAGS: Pass = Pass {
1616
};
1717

1818
struct InvalidHtmlTagsLinter<'a, 'tcx> {
19-
cx: &'a DocContext<'tcx>,
19+
cx: &'a mut DocContext<'tcx>,
2020
}
2121

22-
impl<'a, 'tcx> InvalidHtmlTagsLinter<'a, 'tcx> {
23-
fn new(cx: &'a DocContext<'tcx>) -> Self {
24-
InvalidHtmlTagsLinter { cx }
25-
}
26-
}
27-
28-
crate fn check_invalid_html_tags(krate: Crate, cx: &DocContext<'_>) -> Crate {
22+
crate fn check_invalid_html_tags(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
2923
if !cx.tcx.sess.is_nightly_build() {
3024
krate
3125
} else {
32-
let mut coll = InvalidHtmlTagsLinter::new(cx);
26+
let mut coll = InvalidHtmlTagsLinter { cx };
3327

3428
coll.fold_crate(krate)
3529
}

src/librustdoc/passes/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ crate use self::html_tags::CHECK_INVALID_HTML_TAGS;
5353
#[derive(Copy, Clone)]
5454
crate struct Pass {
5555
crate name: &'static str,
56-
crate run: fn(clean::Crate, &DocContext<'_>) -> clean::Crate,
56+
crate run: fn(clean::Crate, &mut DocContext<'_>) -> clean::Crate,
5757
crate description: &'static str,
5858
}
5959

src/librustdoc/passes/non_autolinks.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ const URL_REGEX: &str = concat!(
2323
);
2424

2525
struct NonAutolinksLinter<'a, 'tcx> {
26-
cx: &'a DocContext<'tcx>,
26+
cx: &'a mut DocContext<'tcx>,
2727
regex: Regex,
2828
}
2929

3030
impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> {
31-
fn new(cx: &'a DocContext<'tcx>) -> Self {
32-
Self { cx, regex: Regex::new(URL_REGEX).expect("failed to build regex") }
33-
}
34-
3531
fn find_raw_urls(
3632
&self,
3733
text: &str,
@@ -52,11 +48,11 @@ impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> {
5248
}
5349
}
5450

55-
crate fn check_non_autolinks(krate: Crate, cx: &DocContext<'_>) -> Crate {
51+
crate fn check_non_autolinks(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
5652
if !cx.tcx.sess.is_nightly_build() {
5753
krate
5854
} else {
59-
let mut coll = NonAutolinksLinter::new(cx);
55+
let mut coll = NonAutolinksLinter { cx, regex: Regex::new(URL_REGEX).expect("failed to build regex") };
6056

6157
coll.fold_crate(krate)
6258
}

src/librustdoc/passes/propagate_doc_cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate const PROPAGATE_DOC_CFG: Pass = Pass {
1212
description: "propagates `#[doc(cfg(...))]` to child items",
1313
};
1414

15-
crate fn propagate_doc_cfg(cr: Crate, _: &DocContext<'_>) -> Crate {
15+
crate fn propagate_doc_cfg(cr: Crate, _: &mut DocContext<'_>) -> Crate {
1616
CfgPropagator { parent_cfg: None }.fold_crate(cr)
1717
}
1818

src/librustdoc/passes/strip_hidden.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crate const STRIP_HIDDEN: Pass = Pass {
1515
};
1616

1717
/// Strip items marked `#[doc(hidden)]`
18-
crate fn strip_hidden(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
18+
crate fn strip_hidden(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Crate {
1919
let mut retained = DefIdSet::default();
2020

2121
// strip all #[doc(hidden)] items

src/librustdoc/passes/strip_priv_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ crate const STRIP_PRIV_IMPORTS: Pass = Pass {
99
description: "strips all private import statements (`use`, `extern crate`) from a crate",
1010
};
1111

12-
crate fn strip_priv_imports(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
12+
crate fn strip_priv_imports(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Crate {
1313
ImportStripper.fold_crate(krate)
1414
}

src/librustdoc/passes/strip_private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ crate const STRIP_PRIVATE: Pass = Pass {
1414

1515
/// Strip private items from the point of view of a crate or externally from a
1616
/// crate, specified by the `xcrate` flag.
17-
crate fn strip_private(mut krate: clean::Crate, cx: &DocContext<'_>) -> clean::Crate {
17+
crate fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1818
// This stripper collects all *retained* nodes.
1919
let mut retained = DefIdSet::default();
2020
let access_levels = cx.renderinfo.borrow().access_levels.clone();

0 commit comments

Comments
 (0)