Skip to content

Commit 5bd0f32

Browse files
authored
Rollup merge of rust-lang#136464 - nnethercote:rm-TyCtxtAt-for-hooks, r=oli-obk
Remove hook calling via `TyCtxtAt`. All hooks receive a `TyCtxtAt` argument. Currently hooks can be called through `TyCtxtAt` or `TyCtxt`. In the latter case, a `TyCtxtAt` is constructed with a dummy span and passed to the hook. However, in practice hooks are never called through `TyCtxtAt`, and always receive a dummy span. (I confirmed this via code inspection, and double-checked it by temporarily making the `TyCtxtAt` code path panic and running all the tests.) This commit removes all the `TyCtxtAt` machinery for hooks. All hooks now receive `TyCtxt` instead of `TyCtxtAt`. There are two existing hooks that use `TyCtxtAt::span`: `const_caller_location_provider` and `try_destructure_mir_constant_for_user_output`. For both hooks the span is always a dummy span, probably unintentionally. This dummy span use is now explicit. If a non-dummy span is needed for these two hooks it would be easy to add it as an extra argument because hooks are less constrained than queries. r? `@oli-obk`
2 parents 40d1cb4 + e661514 commit 5bd0f32

File tree

8 files changed

+21
-38
lines changed

8 files changed

+21
-38
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Not in interpret to make sure we do not use private implementation details
22

33
use rustc_abi::VariantIdx;
4-
use rustc_middle::query::{Key, TyCtxtAt};
4+
use rustc_middle::query::Key;
55
use rustc_middle::ty::layout::LayoutOf;
66
use rustc_middle::ty::{self, Ty, TyCtxt};
77
use rustc_middle::{bug, mir};
@@ -35,16 +35,17 @@ pub(crate) type ValTreeCreationResult<'tcx> = Result<ty::ValTree<'tcx>, ValTreeC
3535

3636
#[instrument(skip(tcx), level = "debug")]
3737
pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
38-
tcx: TyCtxtAt<'tcx>,
38+
tcx: TyCtxt<'tcx>,
3939
val: mir::ConstValue<'tcx>,
4040
ty: Ty<'tcx>,
4141
) -> Option<mir::DestructuredConstant<'tcx>> {
4242
let typing_env = ty::TypingEnv::fully_monomorphized();
43-
let (ecx, op) = mk_eval_cx_for_const_val(tcx, typing_env, val, ty)?;
43+
// FIXME: use a proper span here?
44+
let (ecx, op) = mk_eval_cx_for_const_val(tcx.at(rustc_span::DUMMY_SP), typing_env, val, ty)?;
4445

4546
// We go to `usize` as we cannot allocate anything bigger anyway.
4647
let (field_count, variant, down) = match ty.kind() {
47-
ty::Array(_, len) => (len.try_to_target_usize(tcx.tcx)? as usize, None, op),
48+
ty::Array(_, len) => (len.try_to_target_usize(tcx)? as usize, None, op),
4849
ty::Adt(def, _) if def.variants().is_empty() => {
4950
return None;
5051
}

compiler/rustc_const_eval/src/util/caller_location.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_hir::LangItem;
2-
use rustc_middle::query::TyCtxtAt;
32
use rustc_middle::ty::layout::LayoutOf;
4-
use rustc_middle::ty::{self};
3+
use rustc_middle::ty::{self, TyCtxt};
54
use rustc_middle::{bug, mir};
65
use rustc_span::Symbol;
76
use tracing::trace;
@@ -48,15 +47,15 @@ fn alloc_caller_location<'tcx>(
4847
}
4948

5049
pub(crate) fn const_caller_location_provider(
51-
tcx: TyCtxtAt<'_>,
50+
tcx: TyCtxt<'_>,
5251
file: Symbol,
5352
line: u32,
5453
col: u32,
5554
) -> mir::ConstValue<'_> {
5655
trace!("const_caller_location: {}:{}:{}", file, line, col);
5756
let mut ecx = mk_eval_cx_to_read_const_val(
58-
tcx.tcx,
59-
tcx.span,
57+
tcx,
58+
rustc_span::DUMMY_SP, // FIXME: use a proper span here?
6059
ty::TypingEnv::fully_monomorphized(),
6160
CanAccessMutGlobal::No,
6261
);

compiler/rustc_incremental/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::util::Providers;
2424
#[allow(missing_docs)]
2525
pub fn provide(providers: &mut Providers) {
2626
providers.hooks.save_dep_graph =
27-
|tcx| tcx.sess.time("serialize_dep_graph", || persist::save_dep_graph(tcx.tcx));
27+
|tcx| tcx.sess.time("serialize_dep_graph", || persist::save_dep_graph(tcx));
2828
}
2929

3030
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ fn provide_cstore_hooks(providers: &mut Providers) {
689689
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
690690
// If this is a DefPathHash from an upstream crate, let the CrateStore map
691691
// it to a DefId.
692-
let cstore = CStore::from_tcx(tcx.tcx);
692+
let cstore = CStore::from_tcx(tcx);
693693
let cnum = *tcx
694694
.untracked()
695695
.stable_crate_ids
@@ -702,11 +702,11 @@ fn provide_cstore_hooks(providers: &mut Providers) {
702702
};
703703

704704
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
705-
let cstore = CStore::from_tcx(tcx.tcx);
705+
let cstore = CStore::from_tcx(tcx);
706706
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
707707
};
708708
providers.hooks.import_source_files = |tcx, cnum| {
709-
let cstore = CStore::from_tcx(tcx.tcx);
709+
let cstore = CStore::from_tcx(tcx);
710710
let cdata = cstore.get_crate_data(cnum);
711711
for file_index in 0..cdata.root.source_map.size() {
712712
cdata.imported_source_file(file_index as u32, tcx.sess);

compiler/rustc_middle/src/hooks/mod.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
use rustc_hir::def_id::{DefId, DefPathHash};
77
use rustc_session::StableCrateId;
88
use rustc_span::def_id::{CrateNum, LocalDefId};
9-
use rustc_span::{DUMMY_SP, ExpnHash, ExpnId};
10-
use tracing::instrument;
9+
use rustc_span::{ExpnHash, ExpnId};
1110

1211
use crate::mir;
13-
use crate::query::TyCtxtAt;
1412
use crate::ty::{Ty, TyCtxt};
1513

1614
macro_rules! declare_hooks {
@@ -22,26 +20,14 @@ macro_rules! declare_hooks {
2220
#[inline(always)]
2321
pub fn $name(self, $($arg: $K,)*) -> $V
2422
{
25-
self.at(DUMMY_SP).$name($($arg,)*)
26-
}
27-
)*
28-
}
29-
30-
impl<'tcx> TyCtxtAt<'tcx> {
31-
$(
32-
$(#[$attr])*
33-
#[inline(always)]
34-
#[instrument(level = "debug", skip(self), ret)]
35-
pub fn $name(self, $($arg: $K,)*) -> $V
36-
{
37-
(self.tcx.hooks.$name)(self, $($arg,)*)
23+
(self.hooks.$name)(self, $($arg,)*)
3824
}
3925
)*
4026
}
4127

4228
pub struct Providers {
4329
$(pub $name: for<'tcx> fn(
44-
TyCtxtAt<'tcx>,
30+
TyCtxt<'tcx>,
4531
$($arg: $K,)*
4632
) -> $V,)*
4733
}

compiler/rustc_mir_transform/src/coverage/query.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_middle::mir::coverage::{
66
FunctionCoverageInfo, MappingKind, Op,
77
};
88
use rustc_middle::mir::{Body, Statement, StatementKind};
9-
use rustc_middle::query::TyCtxtAt;
109
use rustc_middle::ty::{self, TyCtxt};
1110
use rustc_middle::util::Providers;
1211
use rustc_span::def_id::LocalDefId;
@@ -15,8 +14,7 @@ use tracing::trace;
1514

1615
/// Registers query/hook implementations related to coverage.
1716
pub(crate) fn provide(providers: &mut Providers) {
18-
providers.hooks.is_eligible_for_coverage =
19-
|TyCtxtAt { tcx, .. }, def_id| is_eligible_for_coverage(tcx, def_id);
17+
providers.hooks.is_eligible_for_coverage = is_eligible_for_coverage;
2018
providers.queries.coverage_attr_on = coverage_attr_on;
2119
providers.queries.coverage_ids_info = coverage_ids_info;
2220
}

compiler/rustc_monomorphize/src/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ fn visit_instance_use<'tcx>(
953953

954954
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
955955
/// can just link to the upstream crate and therefore don't need a mono item.
956-
fn should_codegen_locally<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -> bool {
956+
fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
957957
let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else {
958958
return true;
959959
};
@@ -976,7 +976,7 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -
976976
return true;
977977
}
978978

979-
if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(*tcx).is_some() {
979+
if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(tcx).is_some() {
980980
// We can link to the item in question, no instance needed in this crate.
981981
return false;
982982
}

compiler/rustc_query_impl/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ pub fn query_system<'a>(
224224
rustc_middle::rustc_query_append! { define_queries! }
225225

226226
pub fn provide(providers: &mut rustc_middle::util::Providers) {
227-
providers.hooks.alloc_self_profile_query_strings =
228-
|tcx| alloc_self_profile_query_strings(tcx.tcx);
229-
providers.hooks.query_key_hash_verify_all = |tcx| query_key_hash_verify_all(tcx.tcx);
227+
providers.hooks.alloc_self_profile_query_strings = alloc_self_profile_query_strings;
228+
providers.hooks.query_key_hash_verify_all = query_key_hash_verify_all;
230229
}

0 commit comments

Comments
 (0)