Skip to content

Commit a25435b

Browse files
committed
Auto merge of #154304 - zetanumbers:typeck_root, r=petrochenkov
Make typeck a tcx method which calls typeck_root query Currently typeck query itself calls `tcx.typeck(tcx.typeck_root_def_id(key))` if its key isn't a type-check root. I thought this might be an overhead and made typeck a tcx method which calls typeck_root query instead. This is a step to simplify `cache_on_disk_if` query modifier. @petrochenkov please run perf
2 parents cd14b73 + 1792232 commit a25435b

55 files changed

Lines changed: 347 additions & 335 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &UnordSet<LocalDef
8282
&tcx.typeck(def_id).used_trait_imports
8383
}
8484

85-
fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
85+
fn typeck_root<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
8686
typeck_with_inspect(tcx, def_id, None)
8787
}
8888

@@ -95,6 +95,13 @@ pub fn inspect_typeck<'tcx>(
9595
def_id: LocalDefId,
9696
inspect: ObligationInspector<'tcx>,
9797
) -> &'tcx ty::TypeckResults<'tcx> {
98+
// Closures' typeck results come from their outermost function,
99+
// as they are part of the same "inference environment".
100+
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
101+
if typeck_root_def_id != def_id {
102+
return tcx.typeck(typeck_root_def_id);
103+
}
104+
98105
typeck_with_inspect(tcx, def_id, Some(inspect))
99106
}
100107

@@ -104,12 +111,7 @@ fn typeck_with_inspect<'tcx>(
104111
def_id: LocalDefId,
105112
inspector: Option<ObligationInspector<'tcx>>,
106113
) -> &'tcx ty::TypeckResults<'tcx> {
107-
// Closures' typeck results come from their outermost function,
108-
// as they are part of the same "inference environment".
109-
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
110-
if typeck_root_def_id != def_id {
111-
return tcx.typeck(typeck_root_def_id);
112-
}
114+
assert!(!tcx.is_typeck_child(def_id.to_def_id()));
113115

114116
let id = tcx.local_def_id_to_hir_id(def_id);
115117
let node = tcx.hir_node(id);
@@ -660,7 +662,7 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
660662
pub fn provide(providers: &mut Providers) {
661663
*providers = Providers {
662664
method_autoderef_steps: method::probe::method_autoderef_steps,
663-
typeck,
665+
typeck_root,
664666
used_trait_imports,
665667
check_transmutes: intrinsicck::check_transmutes,
666668
..*providers

compiler/rustc_incremental/src/persist/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const BASE_FN: &[&str] = &[
4949
label_strs::type_of,
5050
// And a big part of compilation (that we eventually want to cache) is type inference
5151
// information:
52-
label_strs::typeck,
52+
label_strs::typeck_root,
5353
];
5454

5555
/// DepNodes for Hir, which is pretty much everything

compiler/rustc_middle/src/queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,9 +1222,9 @@ rustc_queries! {
12221222
separate_provide_extern
12231223
}
12241224

1225-
query typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
1225+
query typeck_root(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
12261226
desc { "type-checking `{}`", tcx.def_path_str(key) }
1227-
cache_on_disk_if { !tcx.is_typeck_child(key.to_def_id()) }
1227+
cache_on_disk_if { true }
12281228
}
12291229

12301230
query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ use rustc_data_structures::hash_table::HashTable;
77
use rustc_data_structures::sharded::Sharded;
88
use rustc_data_structures::sync::{AtomicU64, Lock, WorkerLocal};
99
use rustc_errors::Diag;
10+
use rustc_hir::def_id::LocalDefId;
1011
use rustc_span::Span;
1112

1213
use crate::dep_graph::{DepKind, DepNodeIndex, QuerySideEffect, SerializedDepNodeIndex};
1314
use crate::ich::StableHashingContext;
1415
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
1516
use crate::query::on_disk_cache::OnDiskCache;
16-
use crate::query::{QueryCache, QueryJob, QueryStackFrame};
17-
use crate::ty::TyCtxt;
17+
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryStackFrame};
18+
use crate::ty::{self, TyCtxt};
1819

1920
/// For a particular query, keeps track of "active" keys, i.e. keys whose
2021
/// evaluation has started but has not yet finished successfully.
@@ -200,7 +201,21 @@ pub struct TyCtxtEnsureDone<'tcx> {
200201
pub tcx: TyCtxt<'tcx>,
201202
}
202203

204+
impl<'tcx> TyCtxtEnsureOk<'tcx> {
205+
pub fn typeck(self, def_id: impl IntoQueryKey<LocalDefId>) {
206+
self.typeck_root(
207+
self.tcx.typeck_root_def_id(def_id.into_query_key().to_def_id()).expect_local(),
208+
)
209+
}
210+
}
211+
203212
impl<'tcx> TyCtxt<'tcx> {
213+
pub fn typeck(self, def_id: impl IntoQueryKey<LocalDefId>) -> &'tcx ty::TypeckResults<'tcx> {
214+
self.typeck_root(
215+
self.typeck_root_def_id(def_id.into_query_key().to_def_id()).expect_local(),
216+
)
217+
}
218+
204219
/// Returns a transparent wrapper for `TyCtxt` which uses
205220
/// `span` as the location of queries performed through it.
206221
#[inline(always)]

src/librustdoc/core.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,14 @@ pub(crate) fn create_config(
309309
&EMPTY_SET
310310
};
311311
// In case typeck does end up being called, don't ICE in case there were name resolution errors
312-
providers.queries.typeck = move |tcx, def_id| {
313-
// Closures' tables come from their outermost function,
314-
// as they are part of the same "inference environment".
315-
// This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`)
316-
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local();
317-
if typeck_root_def_id != def_id {
318-
return tcx.typeck(typeck_root_def_id);
319-
}
312+
providers.queries.typeck_root = move |tcx, def_id| {
313+
// Panic before code below breaks in case of someone calls typeck_root directly
314+
assert!(!tcx.is_typeck_child(def_id.to_def_id()));
320315

321316
let body = tcx.hir_body_owned_by(def_id);
322317
debug!("visiting body for {def_id:?}");
323318
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
324-
(rustc_interface::DEFAULT_QUERY_PROVIDERS.queries.typeck)(tcx, def_id)
319+
(rustc_interface::DEFAULT_QUERY_PROVIDERS.queries.typeck_root)(tcx, def_id)
325320
};
326321
}),
327322
extra_symbols: Vec::new(),

tests/incremental/callee_caller_cross_crate/b.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern crate a;
99

10-
#[rustc_clean(except="typeck", cfg="rpass2")]
10+
#[rustc_clean(except="typeck_root", cfg="rpass2")]
1111
pub fn call_function0() {
1212
a::function0(77);
1313
}

tests/incremental/change_add_field/struct_point.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub mod point {
7070
pub mod fn_with_type_in_sig {
7171
use point::Point;
7272

73-
#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
73+
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
7474
pub fn boop(p: Option<&Point>) -> f32 {
7575
p.map(|p| p.total()).unwrap_or(0.0)
7676
}
@@ -86,7 +86,7 @@ pub mod fn_with_type_in_sig {
8686
pub mod call_fn_with_type_in_sig {
8787
use fn_with_type_in_sig;
8888

89-
#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
89+
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
9090
pub fn bip() -> f32 {
9191
fn_with_type_in_sig::boop(None)
9292
}
@@ -102,7 +102,7 @@ pub mod call_fn_with_type_in_sig {
102102
pub mod fn_with_type_in_body {
103103
use point::Point;
104104

105-
#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
105+
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
106106
pub fn boop() -> f32 {
107107
Point::origin().total()
108108
}
@@ -125,7 +125,7 @@ pub mod call_fn_with_type_in_body {
125125
pub mod fn_make_struct {
126126
use point::Point;
127127

128-
#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
128+
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
129129
pub fn make_origin(p: Point) -> Point {
130130
Point { ..p }
131131
}
@@ -135,7 +135,7 @@ pub mod fn_make_struct {
135135
pub mod fn_read_field {
136136
use point::Point;
137137

138-
#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
138+
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
139139
pub fn get_x(p: Point) -> f32 {
140140
p.x
141141
}
@@ -145,7 +145,7 @@ pub mod fn_read_field {
145145
pub mod fn_write_field {
146146
use point::Point;
147147

148-
#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
148+
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
149149
pub fn inc_x(p: &mut Point) {
150150
p.x += 1.0;
151151
}

tests/incremental/change_crate_order/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate a;
1919
use a::A;
2020
use b::B;
2121

22-
//? #[rustc_clean(label="typeck", cfg="rpass2")]
22+
//? #[rustc_clean(label="typeck_root", cfg="rpass2")]
2323
pub fn main() {
2424
A + B;
2525
}

tests/incremental/change_private_fn/struct_point.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub mod fn_calls_methods_in_same_impl {
5454
// The cached result should actually be loaded from disk
5555
// (not just marked green) - for example, `DeadVisitor`
5656
// always runs during compilation as a "pass", and loads
57-
// the typeck results for bodies.
58-
#[rustc_clean(cfg="cfail2", loaded_from_disk="typeck")]
57+
// the typeck_root results for bodies.
58+
#[rustc_clean(cfg="cfail2", loaded_from_disk="typeck_root")]
5959
pub fn check() {
6060
let x = Point { x: 2.0, y: 2.0 };
6161
x.distance_from_origin();

tests/incremental/change_pub_inherent_method_sig/struct_point.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub mod point {
5252
pub mod fn_calls_changed_method {
5353
use point::Point;
5454

55-
#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
55+
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
5656
pub fn check() {
5757
let p = Point { x: 2.0, y: 2.0 };
5858
p.distance_from_point(None);

0 commit comments

Comments
 (0)