Skip to content

Commit 9f5dce7

Browse files
authored
Rollup merge of rust-lang#112018 - GuillaumeGomez:cleanup-tcx, r=notriddle
Clean up usage of `cx.tcx` when `tcx` is already set into a variable I discovered a few cases where `cx.tcx` (and equivalents) was used whereas `tcx` was already stored into a variable. In those cases, better to just use `tcx` directly. r? `@notriddle`
2 parents 9a4fce9 + b7db3de commit 9f5dce7

File tree

9 files changed

+39
-43
lines changed

9 files changed

+39
-43
lines changed

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ pub(crate) fn build_impl(
355355
return;
356356
}
357357

358-
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_impl");
359-
360358
let tcx = cx.tcx;
359+
let _prof_timer = tcx.sess.prof.generic_activity("build_impl");
360+
361361
let associated_trait = tcx.impl_trait_ref(did).map(ty::EarlyBinder::skip_binder);
362362

363363
// Only inline impl if the implemented trait is

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub(crate) fn build_deref_target_impls(
193193
};
194194

195195
if let Some(prim) = target.primitive_type() {
196-
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_primitive_inherent_impls");
196+
let _prof_timer = tcx.sess.prof.generic_activity("build_primitive_inherent_impls");
197197
for did in prim.impls(tcx).filter(|did| !did.is_local()) {
198198
inline::build_impl(cx, did, None, ret);
199199
}

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Cache {
147147

148148
// Cache where all our extern crates are located
149149
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
150-
for &crate_num in cx.tcx.crates(()) {
150+
for &crate_num in tcx.crates(()) {
151151
let e = ExternalCrate { crate_num };
152152

153153
let name = e.name(tcx);

src/librustdoc/html/render/print_item.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -405,18 +405,18 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
405405

406406
clean::ImportItem(ref import) => {
407407
let stab_tags = if let Some(import_def_id) = import.source.did {
408-
let ast_attrs = cx.tcx().get_attrs_unchecked(import_def_id);
408+
let ast_attrs = tcx.get_attrs_unchecked(import_def_id);
409409
let import_attrs = Box::new(clean::Attributes::from_ast(ast_attrs));
410410

411411
// Just need an item with the correct def_id and attrs
412412
let import_item = clean::Item {
413413
item_id: import_def_id.into(),
414414
attrs: import_attrs,
415-
cfg: ast_attrs.cfg(cx.tcx(), &cx.cache().hidden_cfg),
415+
cfg: ast_attrs.cfg(tcx, &cx.cache().hidden_cfg),
416416
..myitem.clone()
417417
};
418418

419-
let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx()).to_string());
419+
let stab_tags = Some(extra_info_tags(&import_item, item, tcx).to_string());
420420
stab_tags
421421
} else {
422422
None
@@ -454,8 +454,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
454454

455455
let unsafety_flag = match *myitem.kind {
456456
clean::FunctionItem(_) | clean::ForeignFunctionItem(_)
457-
if myitem.fn_header(cx.tcx()).unwrap().unsafety
458-
== hir::Unsafety::Unsafe =>
457+
if myitem.fn_header(tcx).unwrap().unsafety == hir::Unsafety::Unsafe =>
459458
{
460459
"<sup title=\"unsafe function\">⚠</sup>"
461460
}
@@ -488,7 +487,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
488487
{docs_before}{docs}{docs_after}",
489488
name = myitem.name.unwrap(),
490489
visibility_emoji = visibility_emoji,
491-
stab_tags = extra_info_tags(myitem, item, cx.tcx()),
490+
stab_tags = extra_info_tags(myitem, item, tcx),
492491
class = myitem.type_(),
493492
unsafety_flag = unsafety_flag,
494493
href = item_path(myitem.type_(), myitem.name.unwrap().as_str()),
@@ -935,7 +934,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
935934
write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
936935

937936
for implementor in foreign {
938-
let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx());
937+
let provided_methods = implementor.inner_impl().provided_trait_methods(tcx);
939938
let assoc_link =
940939
AssocItemLink::GotoSource(implementor.impl_item.item_id, &provided_methods);
941940
render_impl(
@@ -968,7 +967,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
968967
}
969968
w.write_str("</div>");
970969

971-
if t.is_auto(cx.tcx()) {
970+
if t.is_auto(tcx) {
972971
write_small_section_header(
973972
w,
974973
"synthetic-implementors",
@@ -997,7 +996,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
997996
"<div id=\"implementors-list\"></div>",
998997
);
999998

1000-
if t.is_auto(cx.tcx()) {
999+
if t.is_auto(tcx) {
10011000
write_small_section_header(
10021001
w,
10031002
"synthetic-implementors",

src/librustdoc/html/render/type_layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ pub(crate) fn document_type_layout<'a, 'cx: 'a>(
5454
} else if let Primitive::Int(i, _) = tag.primitive() {
5555
i.size().bytes()
5656
} else {
57-
span_bug!(cx.tcx().def_span(ty_def_id), "tag is neither niche nor int")
57+
span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int")
5858
};
5959
variants
6060
.iter_enumerated()
6161
.map(|(variant_idx, variant_layout)| {
6262
let Adt(adt, _) = type_layout.ty.kind() else {
63-
span_bug!(cx.tcx().def_span(ty_def_id), "not an adt")
63+
span_bug!(tcx.def_span(ty_def_id), "not an adt")
6464
};
6565
let name = adt.variant(variant_idx).name;
6666
let is_unsized = variant_layout.abi.is_unsized();

src/librustdoc/passes/collect_intra_doc_links.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ fn resolve_associated_trait_item<'a>(
723723
.iter()
724724
.flat_map(|&(impl_, trait_)| {
725725
filter_assoc_items_by_name_and_namespace(
726-
cx.tcx,
726+
tcx,
727727
trait_,
728728
Ident::with_dummy_span(item_name),
729729
ns,
@@ -1743,7 +1743,7 @@ fn resolution_failure(
17431743
if let Ok(v_res) = collector.resolve(start, ns, item_id, module_id) {
17441744
debug!("found partial_res={:?}", v_res);
17451745
if !v_res.is_empty() {
1746-
*partial_res = Some(full_res(collector.cx.tcx, v_res[0]));
1746+
*partial_res = Some(full_res(tcx, v_res[0]));
17471747
*unresolved = end.into();
17481748
break 'outer;
17491749
}

src/librustdoc/passes/collect_trait_impls.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ pub(crate) const COLLECT_TRAIT_IMPLS: Pass = Pass {
1919
};
2020

2121
pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate {
22+
let tcx = cx.tcx;
2223
// We need to check if there are errors before running this pass because it would crash when
2324
// we try to get auto and blanket implementations.
24-
if cx.tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() {
25+
if tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() {
2526
return krate;
2627
}
2728

@@ -32,8 +33,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
3233
});
3334

3435
let local_crate = ExternalCrate { crate_num: LOCAL_CRATE };
35-
let prims: FxHashSet<PrimitiveType> =
36-
local_crate.primitives(cx.tcx).iter().map(|p| p.1).collect();
36+
let prims: FxHashSet<PrimitiveType> = local_crate.primitives(tcx).iter().map(|p| p.1).collect();
3737

3838
let crate_items = {
3939
let mut coll = ItemCollector::new();
@@ -46,24 +46,23 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
4646

4747
// External trait impls.
4848
{
49-
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_extern_trait_impls");
50-
for &cnum in cx.tcx.crates(()) {
51-
for &impl_def_id in cx.tcx.trait_impls_in_crate(cnum) {
49+
let _prof_timer = tcx.sess.prof.generic_activity("build_extern_trait_impls");
50+
for &cnum in tcx.crates(()) {
51+
for &impl_def_id in tcx.trait_impls_in_crate(cnum) {
5252
inline::build_impl(cx, impl_def_id, None, &mut new_items_external);
5353
}
5454
}
5555
}
5656

5757
// Local trait impls.
5858
{
59-
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_local_trait_impls");
59+
let _prof_timer = tcx.sess.prof.generic_activity("build_local_trait_impls");
6060
let mut attr_buf = Vec::new();
61-
for &impl_def_id in cx.tcx.trait_impls_in_crate(LOCAL_CRATE) {
62-
let mut parent = Some(cx.tcx.parent(impl_def_id));
61+
for &impl_def_id in tcx.trait_impls_in_crate(LOCAL_CRATE) {
62+
let mut parent = Some(tcx.parent(impl_def_id));
6363
while let Some(did) = parent {
6464
attr_buf.extend(
65-
cx.tcx
66-
.get_attrs(did, sym::doc)
65+
tcx.get_attrs(did, sym::doc)
6766
.filter(|attr| {
6867
if let Some([attr]) = attr.meta_item_list().as_deref() {
6968
attr.has_name(sym::cfg)
@@ -73,25 +72,24 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
7372
})
7473
.cloned(),
7574
);
76-
parent = cx.tcx.opt_parent(did);
75+
parent = tcx.opt_parent(did);
7776
}
7877
inline::build_impl(cx, impl_def_id, Some((&attr_buf, None)), &mut new_items_local);
7978
attr_buf.clear();
8079
}
8180
}
8281

83-
cx.tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
84-
for def_id in PrimitiveType::all_impls(cx.tcx) {
82+
tcx.sess.prof.generic_activity("build_primitive_trait_impls").run(|| {
83+
for def_id in PrimitiveType::all_impls(tcx) {
8584
// Try to inline primitive impls from other crates.
8685
if !def_id.is_local() {
8786
inline::build_impl(cx, def_id, None, &mut new_items_external);
8887
}
8988
}
90-
for (prim, did) in PrimitiveType::primitive_locations(cx.tcx) {
89+
for (prim, did) in PrimitiveType::primitive_locations(tcx) {
9190
// Do not calculate blanket impl list for docs that are not going to be rendered.
9291
// While the `impl` blocks themselves are only in `libcore`, the module with `doc`
9392
// attached is directly included in `libstd` as well.
94-
let tcx = cx.tcx;
9593
if did.is_local() {
9694
for def_id in prim.impls(tcx).filter(|def_id| {
9795
// Avoid including impl blocks with filled-in generics.
@@ -157,7 +155,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
157155
// scan through included items ahead of time to splice in Deref targets to the "valid" sets
158156
for it in new_items_external.iter().chain(new_items_local.iter()) {
159157
if let ImplItem(box Impl { ref for_, ref trait_, ref items, .. }) = *it.kind &&
160-
trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait() &&
158+
trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() &&
161159
cleaner.keep_impl(for_, true)
162160
{
163161
let target = items
@@ -199,7 +197,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
199197
if let ImplItem(box Impl { ref for_, ref trait_, ref kind, .. }) = *it.kind {
200198
cleaner.keep_impl(
201199
for_,
202-
trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(),
200+
trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait(),
203201
) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
204202
|| kind.is_blanket()
205203
} else {

src/librustdoc/passes/lint/unescaped_backticks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item) {
5656
)
5757
.unwrap_or_else(|| item.attr_span(tcx));
5858

59-
cx.tcx.struct_span_lint_hir(crate::lint::UNESCAPED_BACKTICKS, hir_id, span, "unescaped backtick", |lint| {
59+
tcx.struct_span_lint_hir(crate::lint::UNESCAPED_BACKTICKS, hir_id, span, "unescaped backtick", |lint| {
6060
let mut help_emitted = false;
6161

6262
match element.prev_code_guess {

src/librustdoc/visit_ast.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -280,26 +280,25 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
280280
return false;
281281
};
282282

283-
let is_private =
284-
!self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, ori_res_did);
285-
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_did, None);
283+
let is_private = !self.cx.cache.effective_visibilities.is_directly_public(tcx, ori_res_did);
284+
let is_hidden = inherits_doc_hidden(tcx, res_did, None);
286285

287286
// Only inline if requested or if the item would otherwise be stripped.
288287
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
289288
return false;
290289
}
291290

292291
if !please_inline &&
293-
let Some(item_def_id) = reexport_chain(self.cx.tcx, def_id, res_did).iter()
292+
let Some(item_def_id) = reexport_chain(tcx, def_id, res_did).iter()
294293
.flat_map(|reexport| reexport.id()).map(|id| id.expect_local())
295294
.chain(iter::once(res_did)).nth(1) &&
296295
item_def_id != def_id &&
297296
self
298297
.cx
299298
.cache
300299
.effective_visibilities
301-
.is_directly_public(self.cx.tcx, item_def_id.to_def_id()) &&
302-
!inherits_doc_hidden(self.cx.tcx, item_def_id, None)
300+
.is_directly_public(tcx, item_def_id.to_def_id()) &&
301+
!inherits_doc_hidden(tcx, item_def_id, None)
303302
{
304303
// The imported item is public and not `doc(hidden)` so no need to inline it.
305304
return false;
@@ -329,7 +328,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
329328
Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => {
330329
let prev = mem::replace(&mut self.inlining, true);
331330
for &i in m.item_ids {
332-
let i = self.cx.tcx.hir().item(i);
331+
let i = tcx.hir().item(i);
333332
self.visit_item_inner(i, None, Some(def_id));
334333
}
335334
self.inlining = prev;

0 commit comments

Comments
 (0)