Auto merge of #137162 - nnethercote:remove-Map-2, r=Zalathar

Move methods from `Map` to `TyCtxt`, part 2.

Continuing the work started in #136466.

Every method gains a `hir_` prefix, though for the ones that already have a `par_` or `try_par_` prefix I added the `hir_` after that.

r? Zalathar
This commit is contained in:
bors 2025-02-18 04:26:30 +00:00
commit aaa8614934
108 changed files with 314 additions and 346 deletions

View file

@ -2121,7 +2121,7 @@ impl Discriminant {
/// simplified
pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> Option<String> {
self.expr
.map(|body| rendered_const(tcx, tcx.hir_body(body), tcx.hir().body_owner_def_id(body)))
.map(|body| rendered_const(tcx, tcx.hir_body(body), tcx.hir_body_owner_def_id(body)))
}
pub(crate) fn value(&self, tcx: TyCtxt<'_>, with_underscores: bool) -> String {
print_evaluated_const(tcx, self.value, with_underscores, false).unwrap()
@ -2417,7 +2417,7 @@ impl ConstantKind {
ConstantKind::Path { ref path } => path.to_string(),
ConstantKind::Extern { def_id } => print_inlined_const(tcx, def_id),
ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
rendered_const(tcx, tcx.hir_body(body), tcx.hir().body_owner_def_id(body))
rendered_const(tcx, tcx.hir_body(body), tcx.hir_body_owner_def_id(body))
}
ConstantKind::Infer { .. } => "_".to_string(),
}

View file

@ -336,7 +336,7 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
match n.kind() {
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args: _ }) => {
let s = if let Some(def) = def.as_local() {
rendered_const(cx.tcx, cx.tcx.hir().body_owned_by(def), def)
rendered_const(cx.tcx, cx.tcx.hir_body_owned_by(def), def)
} else {
inline::print_inlined_const(cx.tcx, def)
};

View file

@ -304,8 +304,7 @@ pub(crate) fn create_config(
return tcx.typeck(typeck_root_def_id);
}
let hir = tcx.hir();
let body = hir.body_owned_by(def_id);
let body = tcx.hir_body_owned_by(def_id);
debug!("visiting body for {def_id:?}");
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
@ -335,14 +334,14 @@ pub(crate) fn run_global_ctxt(
// NOTE: These are copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
let _ = tcx.sess.time("wf_checking", || {
tcx.hir().try_par_for_each_module(|module| tcx.ensure_ok().check_mod_type_wf(module))
tcx.try_par_hir_for_each_module(|module| tcx.ensure_ok().check_mod_type_wf(module))
});
tcx.dcx().abort_if_errors();
tcx.sess.time("missing_docs", || rustc_lint::check_crate(tcx));
tcx.sess.time("check_mod_attrs", || {
tcx.hir().for_each_module(|module| tcx.ensure_ok().check_mod_attrs(module))
tcx.hir_for_each_module(|module| tcx.ensure_ok().check_mod_attrs(module))
});
rustc_passes::stability::check_unused_or_stable_features(tcx);

View file

@ -81,7 +81,7 @@ impl<'tcx> HirCollector<'tcx> {
pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> {
let tcx = self.tcx;
self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| {
tcx.hir().walk_toplevel_module(this)
tcx.hir_walk_toplevel_module(this)
});
self.collector.tests
}

View file

@ -51,7 +51,7 @@ pub(crate) fn collect_spans_and_sources(
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
if generate_link_to_definition {
tcx.hir().walk_toplevel_module(&mut visitor);
tcx.hir_walk_toplevel_module(&mut visitor);
}
let sources = sources::collect_local_sources(tcx, src_root, krate);
(sources, visitor.matches)
@ -173,18 +173,18 @@ impl SpanMapVisitor<'_> {
}
fn infer_id(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
let hir = self.tcx.hir();
let body_id = hir.enclosing_body_owner(hir_id);
let tcx = self.tcx;
let body_id = tcx.hir_enclosing_body_owner(hir_id);
// FIXME: this is showing error messages for parts of the code that are not
// compiled (because of cfg)!
//
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
let typeck_results = self.tcx.typeck_body(hir.body_owned_by(body_id).id());
let typeck_results = tcx.typeck_body(tcx.hir_body_owned_by(body_id).id());
// Interestingly enough, for method calls, we need the whole expression whereas for static
// method/function calls, we need the call expression specifically.
if let Some(def_id) = typeck_results.type_dependent_def_id(expr_hir_id.unwrap_or(hir_id)) {
let link = if def_id.as_local().is_some() {
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
LinkFromSrc::Local(rustc_span(def_id, tcx))
} else {
LinkFromSrc::External(def_id)
};

View file

@ -846,7 +846,7 @@ fn convert_static(
is_unsafe: safety.is_unsafe(),
expr: stat
.expr
.map(|e| rendered_const(tcx, tcx.hir_body(e), tcx.hir().body_owner_def_id(e)))
.map(|e| rendered_const(tcx, tcx.hir_body(e), tcx.hir_body_owner_def_id(e)))
.unwrap_or_default(),
}
}

View file

@ -135,8 +135,7 @@ where
// If we visit an item that contains an expression outside a function body,
// then we need to exit before calling typeck (which will panic). See
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
let hir = tcx.hir();
if hir.maybe_body_owned_by(ex.hir_id.owner.def_id).is_none() {
if tcx.hir_maybe_body_owned_by(ex.hir_id.owner.def_id).is_none() {
return;
}
@ -302,7 +301,7 @@ pub(crate) fn run(
// Run call-finder on all items
let mut calls = FxIndexMap::default();
let mut finder = FindCalls { calls: &mut calls, cx, target_crates, bin_crate };
tcx.hir().visit_all_item_likes_in_crate(&mut finder);
tcx.hir_visit_all_item_likes_in_crate(&mut finder);
// The visitor might have found a type error, which we need to
// promote to a fatal error

View file

@ -52,11 +52,10 @@ declare_lint_pass!(DefaultNumericFallback => [DEFAULT_NUMERIC_FALLBACK]);
impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
let hir = cx.tcx.hir();
// NOTE: this is different from `clippy_utils::is_inside_always_const_context`.
// Inline const supports type inference.
let is_parent_const = matches!(
hir.body_const_context(hir.body_owner_def_id(body.id())),
cx.tcx.hir_body_const_context(cx.tcx.hir_body_owner_def_id(body.id())),
Some(ConstContext::Const { inline: false } | ConstContext::Static(_))
);
let mut visitor = NumericFallbackVisitor::new(cx, is_parent_const);

View file

@ -41,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
if let ItemKind::Enum(def, _) = &item.kind {
for var in def.variants {
if let Some(anon_const) = &var.disr_expr {
let def_id = cx.tcx.hir().body_owner_def_id(anon_const.body);
let def_id = cx.tcx.hir_body_owner_def_id(anon_const.body);
let mut ty = cx.tcx.type_of(def_id.to_def_id()).instantiate_identity();
let constant = cx
.tcx

View file

@ -39,7 +39,7 @@ fn report(cx: &LateContext<'_>, param: &GenericParam<'_>, generics: &Generics<'_
pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) {
if let FnKind::ItemFn(_, generics, _) = kind
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
&& cx.tcx.visibility(cx.tcx.hir_body_owner_def_id(body.id())).is_public()
&& !is_in_test(cx.tcx, hir_id)
{
for param in generics.params {
@ -57,7 +57,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
&& let hir::Impl { of_trait, .. } = *impl_
&& of_trait.is_none()
&& let body = cx.tcx.hir_body(body_id)
&& cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public()
&& cx.tcx.visibility(cx.tcx.hir_body_owner_def_id(body.id())).is_public()
&& !is_in_test(cx.tcx, impl_item.hir_id())
{
for param in impl_item.generics.params {

View file

@ -22,7 +22,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &ImplItem<'_>, ignored
&& let Some(did) = trait_item_def_id_of_impl(items, item.owner_id)
&& !is_from_ignored_trait(trait_ref, ignored_traits)
{
let mut param_idents_iter = cx.tcx.hir().body_param_names(body_id);
let mut param_idents_iter = cx.tcx.hir_body_param_names(body_id);
let mut default_param_idents_iter = cx.tcx.fn_arg_names(did).iter().copied();
let renames = RenamedFnArgs::new(&mut default_param_idents_iter, &mut param_idents_iter);

View file

@ -143,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
&& exprs.iter_mut().partition_in_place(|i| path_to_local(i).is_some()) == 2
&& !expr.span.in_external_macro(cx.sess().source_map())
&& (
is_not_const(cx.tcx, cx.tcx.hir().enclosing_body_owner(expr.hir_id).into())
is_not_const(cx.tcx, cx.tcx.hir_enclosing_body_owner(expr.hir_id).into())
|| self.msrv.meets(msrvs::CONST_FLOAT_CLASSIFY)
)
&& let [first, second, const_1, const_2] = exprs

View file

@ -58,8 +58,7 @@ pub(super) fn check<'tcx>(
unwrap_or_span: unwrap_arg.span,
};
let map = cx.tcx.hir();
let body = map.body_owned_by(map.enclosing_body_owner(expr.hir_id));
let body = cx.tcx.hir_body_owned_by(cx.tcx.hir_enclosing_body_owner(expr.hir_id));
// Visit the body, and return if we've found a reference
if reference_visitor.visit_body(body).is_break() {

View file

@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
if self
.possible_borrowers
.last()
.is_some_and(|&(local_def_id, _)| local_def_id == cx.tcx.hir().body_owner_def_id(body.id()))
.is_some_and(|&(local_def_id, _)| local_def_id == cx.tcx.hir_body_owner_def_id(body.id()))
{
self.possible_borrowers.pop();
}
@ -359,7 +359,7 @@ fn referent_used_exactly_once<'tcx>(
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
&& !place.is_indirect_first_projection()
{
let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
let body_owner_local_def_id = cx.tcx.hir_enclosing_body_owner(reference.hir_id);
if possible_borrowers
.last()
.is_none_or(|&(local_def_id, _)| local_def_id != body_owner_local_def_id)

View file

@ -352,7 +352,7 @@ impl MutablyUsedVariablesCtxt<'_> {
fn is_in_unsafe_block(&self, item: HirId) -> bool {
let hir = self.tcx.hir();
for (parent, node) in hir.parent_iter(item) {
if let Some(fn_sig) = hir.fn_sig_by_hir_id(parent) {
if let Some(fn_sig) = self.tcx.hir_fn_sig_by_hir_id(parent) {
return fn_sig.header.is_unsafe();
} else if let Node::Block(block) = node {
if matches!(block.rules, BlockCheckMode::UnsafeBlock(_)) {

View file

@ -349,10 +349,10 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
}
fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_owner_def_id = cx.tcx.hir_body_owner_def_id(body.id());
let body_owner_kind = cx.tcx.hir().body_owner_kind(body_owner_def_id);
let body_owner_kind = cx.tcx.hir_body_owner_kind(body_owner_def_id);
if let hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_) = body_owner_kind {
let body_span = cx.tcx.hir().span_with_body(body_owner);
if let Some(span) = self.const_span
@ -365,7 +365,7 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
}
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_span = cx.tcx.hir().span(body_owner);
if let Some(span) = self.const_span
&& span.contains(body_span)

View file

@ -68,10 +68,10 @@ impl Context {
}
pub fn enter_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_owner_def_id = cx.tcx.hir_body_owner_def_id(body.id());
match cx.tcx.hir().body_owner_kind(body_owner_def_id) {
match cx.tcx.hir_body_owner_kind(body_owner_def_id) {
hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const { .. } => {
let body_span = cx.tcx.hir().span_with_body(body_owner);
@ -87,7 +87,7 @@ impl Context {
}
pub fn body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner = cx.tcx.hir_body_owner(body.id());
let body_span = cx.tcx.hir().span_with_body(body_owner);
if let Some(span) = self.const_span {

View file

@ -98,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
/// assert_static(closure);
/// ```
fn is_by_value_closure_capture(cx: &LateContext<'_>, redefinition: HirId, root_variable: HirId) -> bool {
let closure_def_id = cx.tcx.hir().enclosing_body_owner(redefinition);
let closure_def_id = cx.tcx.hir_enclosing_body_owner(redefinition);
cx.tcx.is_closure_like(closure_def_id.to_def_id())
&& cx.tcx.closure_captures(closure_def_id).iter().any(|c| {

View file

@ -149,17 +149,15 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
}
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
let owner_id = hir.body_owner_def_id(body.id());
if !matches!(hir.body_owner_kind(owner_id), BodyOwnerKind::Closure) {
let owner_id = cx.tcx.hir_body_owner_def_id(body.id());
if !matches!(cx.tcx.hir_body_owner_kind(owner_id), BodyOwnerKind::Closure) {
self.bindings.push((FxHashMap::default(), owner_id));
}
}
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
let hir = cx.tcx.hir();
if !matches!(
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
cx.tcx.hir_body_owner_kind(cx.tcx.hir_body_owner_def_id(body.id())),
BodyOwnerKind::Closure
) {
self.bindings.pop();

View file

@ -90,8 +90,7 @@ impl SingleCallFn {
|| fn_span.in_external_macro(cx.sess().source_map())
|| cx
.tcx
.hir()
.maybe_body_owned_by(fn_def_id)
.hir_maybe_body_owned_by(fn_def_id)
.is_none_or(|body| is_in_test_function(cx.tcx, body.value.hir_id))
|| match cx.tcx.hir_node(fn_hir_id) {
Node::Item(item) => is_from_proc_macro(cx, item),

View file

@ -28,8 +28,8 @@ fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId)
}
fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool {
let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id);
if let Some(body) = cx.tcx.hir().maybe_body_owned_by(def_id) {
let def_id = cx.tcx.hir_enclosing_body_owner(expr_hir_id);
if let Some(body) = cx.tcx.hir_maybe_body_owned_by(def_id) {
return body.value.peel_blocks().hir_id == expr_hir_id;
}
false

View file

@ -331,8 +331,8 @@ impl UnconditionalRecursion {
&& let [return_expr] = get_return_calls_in_body(body).as_slice()
&& let ExprKind::Call(call_expr, _) = return_expr.kind
// We need to use typeck here to infer the actual function being called.
&& let body_def_id = cx.tcx.hir().enclosing_body_owner(call_expr.hir_id)
&& let Some(body_owner) = cx.tcx.hir().maybe_body_owned_by(body_def_id)
&& let body_def_id = cx.tcx.hir_enclosing_body_owner(call_expr.hir_id)
&& let Some(body_owner) = cx.tcx.hir_maybe_body_owned_by(body_def_id)
&& let typeck = cx.tcx.typeck_body(body_owner.id())
&& let Some(call_def_id) = typeck.type_dependent_def_id(call_expr.hir_id)
{

View file

@ -132,8 +132,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
}
fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
let hir = cx.tcx.hir();
if let Some(body) = hir.maybe_body_owned_by(hir_id.expect_owner().def_id) {
if let Some(body) = cx.tcx.hir_maybe_body_owned_by(hir_id.expect_owner().def_id) {
check_node(cx, hir_id, |v| {
v.expr(&v.bind("expr", body.value));
});

View file

@ -156,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
output: &mut self.registered_lints,
cx,
};
let body = cx.tcx.hir().body_owned_by(
let body = cx.tcx.hir_body_owned_by(
impl_item_refs
.iter()
.find(|iiref| iiref.ident.as_str() == "lint_vec")

View file

@ -237,8 +237,7 @@ pub fn is_in_const_context(cx: &LateContext<'_>) -> bool {
debug_assert!(cx.enclosing_body.is_some(), "`LateContext` has no enclosing body");
cx.enclosing_body.is_some_and(|id| {
cx.tcx
.hir()
.body_const_context(cx.tcx.hir().body_owner_def_id(id))
.hir_body_const_context(cx.tcx.hir_body_owner_def_id(id))
.is_some()
})
}
@ -251,8 +250,7 @@ pub fn is_in_const_context(cx: &LateContext<'_>) -> bool {
/// * associated constants
pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
use ConstContext::{Const, ConstFn, Static};
let hir = tcx.hir();
let Some(ctx) = hir.body_const_context(hir.enclosing_body_owner(hir_id)) else {
let Some(ctx) = tcx.hir_body_const_context(tcx.hir_enclosing_body_owner(hir_id)) else {
return false;
};
match ctx {
@ -1648,7 +1646,7 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool
if is_integer_literal(e, value) {
return true;
}
let enclosing_body = cx.tcx.hir().enclosing_body_owner(e.hir_id);
let enclosing_body = cx.tcx.hir_enclosing_body_owner(e.hir_id);
if let Some(Constant::Int(v)) =
ConstEvalCtxt::with_env(cx.tcx, cx.typing_env(), cx.tcx.typeck(enclosing_body)).eval(e)
{
@ -2762,7 +2760,7 @@ impl<'tcx> ExprUseCtxt<'tcx> {
Node::Expr(use_expr) => match use_expr.kind {
ExprKind::Ret(_) => ExprUseNode::Return(OwnerId {
def_id: cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap()),
def_id: cx.tcx.hir_body_owner_def_id(cx.enclosing_body.unwrap()),
}),
ExprKind::Closure(closure) => ExprUseNode::Return(OwnerId { def_id: closure.def_id }),

View file

@ -136,8 +136,8 @@ pub fn used_exactly_once(mir: &Body<'_>, local: Local) -> Option<bool> {
/// Returns the `mir::Body` containing the node associated with `hir_id`.
#[allow(clippy::module_name_repetitions)]
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<&Body<'_>> {
let body_owner_local_def_id = tcx.hir().enclosing_body_owner(hir_id);
if tcx.hir().body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
let body_owner_local_def_id = tcx.hir_enclosing_body_owner(hir_id);
if tcx.hir_body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
Some(tcx.optimized_mir(body_owner_local_def_id.to_def_id()))
} else {
None

View file

@ -267,7 +267,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
// through calling `body_owner_kind`, which would panic if the callee
// does not have a body.
if let Some(callee_id) = callee_id {
let _ = tcx.hir().body_owner_kind(callee_id);
let _ = tcx.hir_body_owner_kind(callee_id);
}
let ty = tcx.erase_regions(ty);
@ -705,7 +705,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'t
ty::Closure(id, subs) => {
let decl = id
.as_local()
.and_then(|id| cx.tcx.hir().fn_decl_by_hir_id(cx.tcx.local_def_id_to_hir_id(id)));
.and_then(|id| cx.tcx.hir_fn_decl_by_hir_id(cx.tcx.local_def_id_to_hir_id(id)));
Some(ExprFnSig::Closure(decl, subs.as_closure().sig()))
},
ty::FnDef(id, subs) => Some(ExprFnSig::Sig(cx.tcx.fn_sig(id).instantiate(cx.tcx, subs), Some(id))),