Skip to content

Commit 94ed03a

Browse files
committed
Make typeck a tcx method which calls typeck_root query
1 parent c4db0e1 commit 94ed03a

5 files changed

Lines changed: 34 additions & 22 deletions

File tree

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(),

0 commit comments

Comments
 (0)