Skip to content

Commit c6e77b3

Browse files
committed
Reduce uses of hir_crate.
1 parent 75e7cf5 commit c6e77b3

File tree

10 files changed

+27
-34
lines changed

10 files changed

+27
-34
lines changed

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
292292
}
293293
HirTree => {
294294
debug!("pretty printing HIR tree");
295-
format!("{:#?}", ex.tcx().hir_crate(()))
295+
ex.tcx()
296+
.hir_crate_items(())
297+
.owners()
298+
.map(|owner| format!("{:#?} => {:#?}\n", owner, ex.tcx().hir_owner_nodes(owner)))
299+
.collect()
296300
}
297301
Mir => {
298302
let mut out = Vec::new();

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10111011
// Prefetch this to prevent multiple threads from blocking on it later.
10121012
// This is needed since the `hir_id_validator::check_crate` call above is not guaranteed
10131013
// to use `hir_crate`.
1014-
tcx.ensure_done().hir_crate(());
1014+
tcx.ensure_done().hir_crate_items(());
10151015

10161016
let sess = tcx.sess;
10171017
sess.time("misc_checking_1", || {

compiler/rustc_lint/src/expect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_hir::CRATE_OWNER_ID;
32
use rustc_middle::lint::LintExpectation;
43
use rustc_middle::query::Providers;
54
use rustc_middle::ty::TyCtxt;
@@ -18,7 +17,7 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
1817

1918
let mut expectations = Vec::new();
2019

21-
for owner in std::iter::once(CRATE_OWNER_ID).chain(krate.owners()) {
20+
for owner in krate.owners() {
2221
let lints = tcx.shallow_lint_levels_on(owner);
2322
expectations.extend_from_slice(&lints.expectations);
2423
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ impl<'tcx> TyCtxt<'tcx> {
328328
}
329329

330330
/// Returns an iterator of the `DefId`s for all body-owners in this
331-
/// crate. If you would prefer to iterate over the bodies
332-
/// themselves, you can do `self.hir_crate(()).body_ids.iter()`.
331+
/// crate.
333332
#[inline]
334333
pub fn hir_body_owners(self) -> impl Iterator<Item = LocalDefId> {
335334
self.hir_crate_items(()).body_owners.iter().copied()
@@ -396,12 +395,11 @@ impl<'tcx> TyCtxt<'tcx> {
396395
where
397396
V: Visitor<'tcx>,
398397
{
399-
let krate = self.hir_crate(());
400-
for info in krate.owners.iter() {
401-
if let MaybeOwner::Owner(info) = info {
402-
for attrs in info.attrs.map.values() {
403-
walk_list!(visitor, visit_attribute, *attrs);
404-
}
398+
let krate = self.hir_crate_items(());
399+
for owner in krate.owners() {
400+
let attrs = self.hir_attr_map(owner);
401+
for attrs in attrs.map.values() {
402+
walk_list!(visitor, visit_attribute, *attrs);
405403
}
406404
}
407405
V::Result::output()
@@ -1225,6 +1223,7 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> Mod
12251223
..
12261224
} = collector;
12271225
ModuleItems {
1226+
add_root: false,
12281227
submodules: submodules.into_boxed_slice(),
12291228
free_items: items.into_boxed_slice(),
12301229
trait_items: trait_items.into_boxed_slice(),
@@ -1258,6 +1257,7 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
12581257
} = collector;
12591258

12601259
ModuleItems {
1260+
add_root: true,
12611261
submodules: submodules.into_boxed_slice(),
12621262
free_items: items.into_boxed_slice(),
12631263
trait_items: trait_items.into_boxed_slice(),

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use crate::ty::{EarlyBinder, ImplSubject, TyCtxt};
2424
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
2525
#[derive(Debug, HashStable, Encodable, Decodable)]
2626
pub struct ModuleItems {
27+
/// Whether this represents the whole crate, in which case we need to add `CRATE_OWNER_ID` to
28+
/// the iterators if we want to account for the crate root.
29+
add_root: bool,
2730
submodules: Box<[OwnerId]>,
2831
free_items: Box<[ItemId]>,
2932
trait_items: Box<[TraitItemId]>,
@@ -60,9 +63,10 @@ impl ModuleItems {
6063
}
6164

6265
pub fn owners(&self) -> impl Iterator<Item = OwnerId> {
63-
self.free_items
64-
.iter()
65-
.map(|id| id.owner_id)
66+
self.add_root
67+
.then_some(CRATE_OWNER_ID)
68+
.into_iter()
69+
.chain(self.free_items.iter().map(|id| id.owner_id))
6670
.chain(self.trait_items.iter().map(|id| id.owner_id))
6771
.chain(self.impl_items.iter().map(|id| id.owner_id))
6872
.chain(self.foreign_items.iter().map(|id| id.owner_id))

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ impl<'tcx> TyCtxt<'tcx> {
21182118
) -> &'tcx rustc_hir::def_path_hash_map::DefPathHashMap {
21192119
// Create a dependency to the crate to be sure we re-execute this when the amount of
21202120
// definitions change.
2121-
self.ensure_ok().hir_crate(());
2121+
self.ensure_ok().hir_crate_items(());
21222122
// Freeze definitions once we start iterating on them, to prevent adding new ones
21232123
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
21242124
self.untracked.definitions.freeze().def_path_hash_to_def_index_map()

src/librustdoc/core.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ pub(crate) fn run_global_ctxt(
387387
ctxt.external_traits.insert(sized_trait_did, sized_trait);
388388
}
389389

390-
debug!("crate: {:?}", tcx.hir_crate(()));
391-
392390
let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt));
393391

394392
if krate.module.doc_value().is_empty() {

tests/ui/consts/const-unstable-intrinsic.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ error: `size_of_val` is not yet stable as a const intrinsic
2424
LL | unstable_intrinsic::size_of_val(&x);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
|
27-
help: add `#![feature(unstable)]` to the crate attributes to enable
28-
|
29-
LL + #![feature(unstable)]
30-
|
27+
= help: add `#![feature(unstable)]` to the crate attributes to enable
3128

3229
error: `align_of_val` is not yet stable as a const intrinsic
3330
--> $DIR/const-unstable-intrinsic.rs:20:9
3431
|
3532
LL | unstable_intrinsic::align_of_val(&x);
3633
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3734
|
38-
help: add `#![feature(unstable)]` to the crate attributes to enable
39-
|
40-
LL + #![feature(unstable)]
41-
|
35+
= help: add `#![feature(unstable)]` to the crate attributes to enable
4236

4337
error: const function that might be (indirectly) exposed to stable cannot use `#[feature(local)]`
4438
--> $DIR/const-unstable-intrinsic.rs:24:9

tests/ui/stability-attribute/const-stability-attribute-implies-no-feature.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ error: `foobar` is not yet stable as a const fn
44
LL | foobar();
55
| ^^^^^^^^
66
|
7-
help: add `#![feature(const_foobar)]` to the crate attributes to enable
8-
|
9-
LL + #![feature(const_foobar)]
10-
|
7+
= help: add `#![feature(const_foobar)]` to the crate attributes to enable
118

129
error: aborting due to 1 previous error
1310

tests/ui/traits/const-traits/staged-api-user-crate.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ error: `staged_api::MyTrait` is not yet stable as a const trait
1515
LL | Unstable::func();
1616
| ^^^^^^^^^^^^^^^^
1717
|
18-
help: add `#![feature(unstable)]` to the crate attributes to enable
19-
|
20-
LL + #![feature(unstable)]
21-
|
18+
= help: add `#![feature(unstable)]` to the crate attributes to enable
2219

2320
error: aborting due to 2 previous errors
2421

0 commit comments

Comments
 (0)