Skip to content

Commit 20ce2f6

Browse files
committed
Move the krate method to Hir and remove the Krate dep node
1 parent 513e326 commit 20ce2f6

File tree

16 files changed

+42
-61
lines changed

16 files changed

+42
-61
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! "infer" some properties for each kind of `DepNode`:
3636
//!
3737
//! * Whether a `DepNode` of a given kind has any parameters at all. Some
38-
//! `DepNode`s, like `Krate`, represent global concepts with only one value.
38+
//! `DepNode`s, like `AllLocalTraitImpls`, represent global concepts with only one value.
3939
//! * Whether it is possible, in principle, to reconstruct a query key from a
4040
//! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
4141
//! in which case it is possible to map the node's fingerprint back to the
@@ -400,19 +400,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
400400
// We use this for most things when incr. comp. is turned off.
401401
[] Null,
402402

403-
// Represents the `Krate` as a whole (the `hir::Krate` value) (as
404-
// distinct from the krate module). This is basically a hash of
405-
// the entire krate, so if you read from `Krate` (e.g., by calling
406-
// `tcx.hir().krate()`), we will have to assume that any change
407-
// means that you need to be recompiled. This is because the
408-
// `Krate` value gives you access to all other items. To avoid
409-
// this fate, do not call `tcx.hir().krate()`; instead, prefer
410-
// wrappers like `tcx.visit_all_items_in_krate()`. If there is no
411-
// suitable wrapper, you can use `tcx.dep_graph.ignore()` to gain
412-
// access to the krate, but you must remember to add suitable
413-
// edges yourself for the individual items that you read.
414-
[eval_always] Krate,
415-
416403
// Represents the body of a function or method. The def-id is that of the
417404
// function/method.
418405
[eval_always] HirBody(DefId),

src/librustc/hir/map/collector.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
223223
(commandline_args_hash, crate_disambiguator.to_fingerprint()),
224224
);
225225

226-
let (_, crate_hash) = input_dep_node_and_hash(
227-
self.dep_graph,
228-
&mut self.hcx,
229-
DepNode::new_no_params(DepKind::Krate),
230-
crate_hash_input,
231-
);
226+
let mut stable_hasher = StableHasher::new();
227+
crate_hash_input.hash_stable(&mut self.hcx, &mut stable_hasher);
228+
let crate_hash: Fingerprint = stable_hasher.finish();
232229

233230
let svh = Svh::new(crate_hash.to_smaller_hash());
234231
(self.map, svh)

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn check_crate(hir_map: &Map<'_>) {
1212

1313
let errors = Lock::new(Vec::new());
1414

15-
par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| {
15+
par_iter(&hir_map.forest.krate.modules).for_each(|(module_id, _)| {
1616
let local_def_id = hir_map.local_def_id(*module_id);
1717
hir_map.visit_item_likes_in_module(
1818
local_def_id,

src/librustc/hir/map/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ impl Forest<'hir> {
140140
Forest { krate, dep_graph: dep_graph.clone() }
141141
}
142142

143-
pub fn krate(&self) -> &Crate<'hir> {
144-
self.dep_graph.read(DepNode::new_no_params(DepKind::Krate));
145-
&self.krate
146-
}
147-
148143
/// This is used internally in the dependency tracking system.
149144
/// Use the `krate` method to ensure your dependency on the
150145
/// crate is tracked.
@@ -401,10 +396,6 @@ impl<'hir> Map<'hir> {
401396
self.lookup(id).cloned()
402397
}
403398

404-
pub fn krate(&self) -> &'hir Crate<'hir> {
405-
self.forest.krate()
406-
}
407-
408399
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
409400
self.read(id);
410401

src/librustc/hir/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ pub mod map;
88

99
use crate::ty::query::Providers;
1010
use crate::ty::TyCtxt;
11+
use rustc_hir::def_id::LOCAL_CRATE;
1112
use rustc_hir::print;
13+
use rustc_hir::Crate;
1214
use std::ops::Deref;
1315

1416
/// A wrapper type which allows you to access HIR.
@@ -18,6 +20,12 @@ pub struct Hir<'tcx> {
1820
map: &'tcx map::Map<'tcx>,
1921
}
2022

23+
impl<'tcx> Hir<'tcx> {
24+
pub fn krate(&self) -> &'tcx Crate<'tcx> {
25+
self.tcx.hir_crate(LOCAL_CRATE)
26+
}
27+
}
28+
2129
impl<'tcx> Deref for Hir<'tcx> {
2230
type Target = &'tcx map::Map<'tcx>;
2331

src/librustc/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ rustc_queries! {
4343
}
4444

4545
Other {
46+
// Represents crate as a whole (as distinct from the to-level crate module).
47+
// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
48+
// we will have to assume that any change means that you need to be recompiled.
49+
// This is because the `hir_crate` query gives you access to all other items.
50+
// To avoid this fate, do not call `tcx.hir().krate()`; instead,
51+
// prefer wrappers like `tcx.visit_all_items_in_krate()`.
4652
query hir_crate(key: CrateNum) -> &'tcx Crate<'tcx> {
4753
eval_always
4854
no_hash

src/librustc/ty/query/plumbing.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,6 @@ pub fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool {
11771177
// These are inputs that are expected to be pre-allocated and that
11781178
// should therefore always be red or green already.
11791179
DepKind::AllLocalTraitImpls |
1180-
DepKind::Krate |
11811180
DepKind::CrateMetadata |
11821181
DepKind::HirBody |
11831182
DepKind::Hir |

src/librustc_driver/pretty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ where
6969
match *ppmode {
7070
PpmNormal => {
7171
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
72-
f(&annotation, tcx.hir().forest.krate())
72+
f(&annotation, tcx.hir().krate())
7373
}
7474

7575
PpmIdentified => {
7676
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
77-
f(&annotation, tcx.hir().forest.krate())
77+
f(&annotation, tcx.hir().krate())
7878
}
7979
PpmTyped => {
8080
abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
8181

8282
let empty_tables = ty::TypeckTables::empty(None);
8383
let annotation = TypedAnnotation { tcx, tables: Cell::new(&empty_tables) };
84-
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().forest.krate()))
84+
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
8585
}
8686
_ => panic!("Should use call_with_pp_support"),
8787
}

src/librustc_resolve/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
612612
let parent_id = self.tcx.hir().get_parent_node(hir_id);
613613
let parent_impl_id = hir::ImplItemId { hir_id: parent_id };
614614
let parent_trait_id = hir::TraitItemId { hir_id: parent_id };
615-
let krate = self.tcx.hir().forest.krate();
615+
let krate = self.tcx.hir().krate();
616616

617617
if !(krate.items.contains_key(&parent_id)
618618
|| krate.impl_items.contains_key(&parent_impl_id)

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn run(options: Options) -> i32 {
8787
compiler.enter(|queries| {
8888
let lower_to_hir = queries.lower_to_hir()?;
8989

90-
let mut opts = scrape_test_config(lower_to_hir.peek().0.krate());
90+
let mut opts = scrape_test_config(lower_to_hir.peek().0.untracked_krate());
9191
opts.display_warnings |= options.display_warnings;
9292
let enable_per_target_ignores = options.enable_per_target_ignores;
9393
let mut collector = Collector::new(

src/test/incremental/crate_hash_reorder.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
// Check that reordering otherwise identical items is not considered a
99
// change at all.
10-
#[rustc_clean(label="Krate", cfg="rpass2")]
11-
10+
#[rustc_clean(label = "hir_crate", cfg = "rpass2")]
1211
// But removing an item, naturally, is.
13-
#[rustc_dirty(label="Krate", cfg="rpass3")]
14-
12+
#[rustc_dirty(label = "hir_crate", cfg = "rpass3")]
1513
#[cfg(rpass1)]
1614
pub struct X {
1715
pub x: u32,
@@ -26,4 +24,4 @@ pub struct X {
2624
pub x: u32,
2725
}
2826

29-
pub fn main() { }
27+
pub fn main() {}

src/test/incremental/issue-38222.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
// Test that debuginfo does not introduce a dependency edge to the Krate
1+
// Test that debuginfo does not introduce a dependency edge to the hir_crate
22
// dep-node.
33

44
// revisions:rpass1 rpass2
55
// compile-flags: -Z query-dep-graph
66

7-
87
#![feature(rustc_attrs)]
9-
10-
11-
#![rustc_partition_reused(module="issue_38222-mod1", cfg="rpass2")]
12-
13-
// If codegen had added a dependency edge to the Krate dep-node, nothing would
8+
#![rustc_partition_reused(module = "issue_38222-mod1", cfg = "rpass2")]
9+
// If codegen had added a dependency edge to the hir_crate dep-node, nothing would
1410
// be re-used, so checking that this module was re-used is sufficient.
15-
#![rustc_partition_reused(module="issue_38222", cfg="rpass2")]
11+
#![rustc_partition_reused(module = "issue_38222", cfg = "rpass2")]
1612

1713
//[rpass1] compile-flags: -C debuginfo=1
1814
//[rpass2] compile-flags: -C debuginfo=1

src/test/incremental/krate-inherent.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
#![allow(warnings)]
66
#![feature(rustc_attrs)]
7-
#![rustc_partition_reused(module="krate_inherent-x", cfg="cfail2")]
7+
#![rustc_partition_reused(module = "krate_inherent-x", cfg = "cfail2")]
88
#![crate_type = "rlib"]
99

1010
pub mod x {
1111
pub struct Foo;
1212
impl Foo {
13-
pub fn foo(&self) { }
13+
pub fn foo(&self) {}
1414
}
1515

1616
pub fn method() {
1717
let x: Foo = Foo;
18-
x.foo(); // inherent methods used to add an edge from Krate
18+
x.foo(); // inherent methods used to add an edge from hir_crate
1919
}
2020
}
2121

2222
#[cfg(cfail1)]
23-
pub fn bar() { } // remove this unrelated fn in cfail2, which should not affect `x::method`
23+
pub fn bar() {} // remove this unrelated fn in cfail2, which should not affect `x::method`

src/test/incremental/krate-inlined.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Regr. test that using HIR inlined from another krate does *not* add
2-
// a dependency from the local Krate node. We can't easily test that
2+
// a dependency from the local hir_crate node. We can't easily test that
33
// directly anymore, so now we test that we get reuse.
44

55
// revisions: rpass1 rpass2
66
// compile-flags: -Z query-dep-graph
77

88
#![allow(warnings)]
99
#![feature(rustc_attrs)]
10-
#![rustc_partition_reused(module="krate_inlined-x", cfg="rpass2")]
10+
#![rustc_partition_reused(module = "krate_inlined-x", cfg = "rpass2")]
1111

1212
fn main() {
1313
x::method();

src/test/ui/dep-graph/dep-graph-variance-alias.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
#![feature(rustc_attrs)]
77
#![allow(dead_code)]
88
#![allow(unused_variables)]
9-
10-
fn main() { }
9+
#![rustc_if_this_changed(hir_crate)]
10+
fn main() {}
1111

1212
struct Foo<T> {
13-
f: T
13+
f: T,
1414
}
1515

16-
#[rustc_if_this_changed(Krate)]
1716
type TypeAlias<T> = Foo<T>;
1817

1918
#[rustc_then_this_would_need(variances_of)] //~ ERROR OK
2019
struct Use<T> {
21-
x: TypeAlias<T>
20+
x: TypeAlias<T>,
2221
}

src/test/ui/dep-graph/dep-graph-variance-alias.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: OK
2-
--> $DIR/dep-graph-variance-alias.rs:19:1
2+
--> $DIR/dep-graph-variance-alias.rs:18:1
33
|
44
LL | #[rustc_then_this_would_need(variances_of)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)