1
Fork 0

Move some Map methods onto TyCtxt.

The end goal is to eliminate `Map` altogether.

I added a `hir_` prefix to all of them, that seemed simplest. The
exceptions are `module_items` which became `hir_module_free_items` because
there was already a `hir_module_items`, and `items` which became
`hir_free_items` for consistency with `hir_module_free_items`.
This commit is contained in:
Nicholas Nethercote 2025-02-03 10:45:49 +11:00
parent cd1d84cdf7
commit f86f7ad5f2
197 changed files with 465 additions and 476 deletions

View file

@ -1989,7 +1989,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
trace: &TypeTrace<'tcx>,
span: Span,
) -> Option<TypeErrorAdditionalDiags> {
let hir = self.tcx.hir();
let TypeError::ArraySize(sz) = terr else {
return None;
};
@ -1997,7 +1996,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn { body: body_id, .. }, ..
}) => {
let body = hir.body(*body_id);
let body = self.tcx.hir_body(*body_id);
struct LetVisitor {
span: Span,
}

View file

@ -1320,7 +1320,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
{
let output = args.as_closure().sig().output().skip_binder();
if self.generic_arg_contains_target(output.into()) {
let body = self.tecx.tcx.hir().body(body);
let body = self.tecx.tcx.hir_body(body);
let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) {
None
} else {

View file

@ -66,7 +66,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
);
let mut impl_span = None;
let mut implicit_static_lifetimes = Vec::new();
if let Some(impl_node) = self.tcx().hir().get_if_local(*impl_def_id) {
if let Some(impl_node) = self.tcx().hir_get_if_local(*impl_def_id) {
// If an impl is local, then maybe this isn't what they want. Try to
// be as helpful as possible with implicit lifetimes.

View file

@ -318,7 +318,7 @@ pub fn suggest_new_region_bound(
} else {
// get a lifetime name of existing named lifetimes if any
let existing_lt_name = if let Some(id) = scope_def_id
&& let Some(generics) = tcx.hir().get_generics(id)
&& let Some(generics) = tcx.hir_get_generics(id)
&& let named_lifetimes = generics
.params
.iter()
@ -349,7 +349,7 @@ pub fn suggest_new_region_bound(
// if there are more than one elided lifetimes in inputs, the explicit `'_` lifetime cannot be used.
// introducing a new lifetime `'a` or making use of one from existing named lifetimes if any
if let Some(id) = scope_def_id
&& let Some(generics) = tcx.hir().get_generics(id)
&& let Some(generics) = tcx.hir_get_generics(id)
&& let mut spans_suggs =
make_elided_region_spans_suggs(name, generics.params.iter())
&& spans_suggs.len() > 1
@ -470,7 +470,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
def_id: DefId,
trait_objects: &FxIndexSet<DefId>,
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
match tcx.hir().get_if_local(def_id)? {
match tcx.hir_get_if_local(def_id)? {
Node::ImplItem(impl_item) => {
let impl_did = tcx.hir().get_parent_item(impl_item.hir_id());
if let hir::OwnerNode::Item(Item {

View file

@ -543,7 +543,7 @@ impl<T> Trait<T> for X {
let tcx = self.tcx;
let assoc = tcx.associated_item(proj_ty.def_id);
let (trait_ref, assoc_args) = proj_ty.trait_ref_and_own_args(tcx);
let Some(item) = tcx.hir().get_if_local(body_owner_def_id) else {
let Some(item) = tcx.hir_get_if_local(body_owner_def_id) else {
return false;
};
let Some(hir_generics) = item.generics() else {
@ -625,7 +625,7 @@ impl<T> Trait<T> for X {
)
};
let body_owner = tcx.hir().get_if_local(body_owner_def_id);
let body_owner = tcx.hir_get_if_local(body_owner_def_id);
let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
// We don't want to suggest calling an assoc fn in a scope where that isn't feasible.

View file

@ -487,7 +487,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&format!("`{sup}: {sub}`"),
);
// We should only suggest rewriting the `where` clause if the predicate is within that `where` clause
if let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id)
if let Some(generics) = self.tcx.hir_get_generics(impl_item_def_id)
&& generics.where_clause_span.contains(span)
{
self.suggest_copy_trait_method_bounds(
@ -590,7 +590,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
return;
};
let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else {
let Some(generics) = self.tcx.hir_get_generics(impl_item_def_id) else {
return;
};
@ -763,7 +763,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
// instead we suggest `T: 'a + 'b` in that case.
let hir_generics = self.tcx.hir().get_generics(scope).unwrap();
let hir_generics = self.tcx.hir_get_generics(scope).unwrap();
let sugg_span = match hir_generics.bounds_span_for_suggestions(def_id) {
Some((span, open_paren_sp)) => Some((span, true, open_paren_sp)),
// If `param` corresponds to `Self`, no usable suggestion span.
@ -822,7 +822,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
{
// The lifetime found in the `impl` is longer than the one on the RPITIT.
// Do not suggest `<Type as Trait>::{opaque}: 'static`.
} else if let Some(generics) = self.tcx.hir().get_generics(suggestion_scope) {
} else if let Some(generics) = self.tcx.hir_get_generics(suggestion_scope) {
let pred = format!("{bound_kind}: {lt_name}");
let suggestion = format!("{} {}", generics.add_where_or_trailing_comma(), pred);
suggs.push((generics.tail_span_for_predicate_suggestion(), suggestion))
@ -907,7 +907,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
hir::OwnerNode::Synthetic => unreachable!(),
}
let ast_generics = self.tcx.hir().get_generics(lifetime_scope).unwrap();
let ast_generics = self.tcx.hir_get_generics(lifetime_scope).unwrap();
let sugg = ast_generics
.span_for_lifetime_suggestion()
.map(|span| (span, format!("{new_lt}, ")))
@ -1382,7 +1382,7 @@ fn suggest_precise_capturing<'tcx>(
new_params += name_as_bounds;
}
let Some(generics) = tcx.hir().get_generics(fn_def_id) else {
let Some(generics) = tcx.hir_get_generics(fn_def_id) else {
// This shouldn't happen, but don't ICE.
return;
};

View file

@ -649,7 +649,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
else {
return;
};
let hir::Body { params, .. } = self.tcx.hir().body(*body);
let hir::Body { params, .. } = self.tcx.hir_body(*body);
// 1. Get the args of the closure.
// 2. Assume exp_found is FnOnce / FnMut / Fn, we can extract function parameters from [1].
@ -846,7 +846,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
true
};
let hir = self.tcx.hir();
for stmt in blk.stmts.iter().rev() {
let hir::StmtKind::Let(local) = &stmt.kind else {
continue;
@ -871,7 +870,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
..
}) => {
for param in hir.body(*body).params {
for param in self.tcx.hir_body(*body).params {
param.pat.walk(&mut find_compatible_candidates);
}
}

View file

@ -925,7 +925,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let hir_id = self.tcx.local_def_id_to_hir_id(obligation.cause.body_id);
let Some(body_id) = self.tcx.hir_node(hir_id).body_id() else { return false };
let ControlFlow::Break(expr) =
(FindMethodSubexprOfTry { search_span: span }).visit_body(self.tcx.hir().body(body_id))
(FindMethodSubexprOfTry { search_span: span }).visit_body(self.tcx.hir_body(body_id))
else {
return false;
};
@ -1012,7 +1012,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let [arg] = args
&& let hir::ExprKind::Closure(closure) = arg.kind
// The closure has a block for its body with no tail expression
&& let body = self.tcx.hir().body(closure.body)
&& let body = self.tcx.hir_body(closure.body)
&& let hir::ExprKind::Block(block, _) = body.value.kind
&& let None = block.expr
// The last statement is of a type that can be converted to the return error type
@ -1447,7 +1447,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let [associated_item]: &[ty::AssocItem] = &associated_items[..] else {
return None;
};
match self.tcx.hir().get_if_local(associated_item.def_id) {
match self.tcx.hir_get_if_local(associated_item.def_id) {
Some(
hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Type(_, Some(ty)),
@ -1514,7 +1514,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span = match fn_decl.output {
hir::FnRetTy::Return(ty) => ty.span,
hir::FnRetTy::DefaultReturn(_) => {
let body = self.tcx.hir().body(id);
let body = self.tcx.hir_body(id);
match body.value.kind {
hir::ExprKind::Block(
hir::Block { expr: Some(expr), .. },
@ -2850,7 +2850,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
_ => None,
};
let found_node = found_did.and_then(|did| self.tcx.hir().get_if_local(did));
let found_node = found_did.and_then(|did| self.tcx.hir_get_if_local(did));
let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did));
if !self.reported_signature_mismatch.borrow_mut().insert((span, found_span)) {
@ -2896,7 +2896,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if found.len() != expected.len() {
let (closure_span, closure_arg_span, found) = found_did
.and_then(|did| {
let node = self.tcx.hir().get_if_local(did)?;
let node = self.tcx.hir_get_if_local(did)?;
let (found_span, closure_arg_span, found) = self.get_fn_like_arguments(node)?;
Some((Some(found_span), closure_arg_span, found))
})
@ -2946,7 +2946,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}) => (
fn_decl_span,
fn_arg_span,
hir.body(body)
self.tcx
.hir_body(body)
.params
.iter()
.map(|arg| {
@ -3208,7 +3209,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Some(obligation.cause.body_id)
};
if let Some(def_id) = def_id
&& let Some(generics) = self.tcx.hir().get_generics(def_id)
&& let Some(generics) = self.tcx.hir_get_generics(def_id)
{
err.span_suggestion_verbose(
generics.tail_span_for_predicate_suggestion(),

View file

@ -418,7 +418,7 @@ pub fn report_dyn_incompatibility<'tcx>(
violations: &[DynCompatibilityViolation],
) -> Diag<'tcx> {
let trait_str = tcx.def_path_str(trait_def_id);
let trait_span = tcx.hir().get_if_local(trait_def_id).and_then(|node| match node {
let trait_span = tcx.hir_get_if_local(trait_def_id).and_then(|node| match node {
hir::Node::Item(item) => Some(item.ident.span),
_ => None,
});

View file

@ -790,8 +790,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
// Get the name of the callable and the arguments to be used in the suggestion.
let hir = self.tcx.hir();
let msg = match def_id_or_name {
DefIdOrName::DefId(def_id) => match self.tcx.def_kind(def_id) {
DefKind::Ctor(CtorOf::Struct, _) => {
@ -834,7 +832,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
Applicability::HasPlaceholders,
);
} else if let DefIdOrName::DefId(def_id) = def_id_or_name {
let name = match hir.get_if_local(def_id) {
let name = match self.tcx.hir_get_if_local(def_id) {
Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(hir::Closure { fn_decl_span, .. }),
..
@ -950,7 +948,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
) -> bool {
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
self.enter_forall(self_ty, |ty: Ty<'_>| {
let Some(generics) = self.tcx.hir().get_generics(obligation.cause.body_id) else {
let Some(generics) = self.tcx.hir_get_generics(obligation.cause.body_id) else {
return false;
};
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
@ -1595,7 +1593,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let ty = typeck_results.expr_ty_adjusted(base)
&& let ty::FnDef(def_id, _args) = ty.kind()
&& let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
hir.get_if_local(*def_id)
self.tcx.hir_get_if_local(*def_id)
{
let msg = format!("alternatively, consider making `fn {ident}` asynchronous");
if vis_span.is_empty() {
@ -1703,10 +1701,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
span: Span,
trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool {
let hir = self.tcx.hir();
let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn {sig, body: body_id, .. }, .. }) = node
&& let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
&& let hir::ExprKind::Block(blk, _) = &self.tcx.hir_body(*body_id).value.kind
&& sig.decl.output.span().overlaps(span)
&& blk.expr.is_none()
&& trait_pred.self_ty().skip_binder().is_unit()
@ -2790,7 +2787,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
generics,
kind: hir::TraitItemKind::Type(bounds, None),
..
})) = tcx.hir().get_if_local(item_def_id)
})) = tcx.hir_get_if_local(item_def_id)
// Do not suggest relaxing if there is an explicit `Sized` obligation.
&& !bounds.iter()
.filter_map(|bound| bound.trait_ref())
@ -3298,7 +3295,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string();
let msg = format!("required for `{self_ty_str}` to implement `{trait_name}`");
let mut is_auto_trait = false;
match tcx.hir().get_if_local(data.impl_or_alias_def_id) {
match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(is_auto, ..),
ident,
@ -3423,7 +3420,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
.map_bound(|pred| pred.trait_ref)
.print_only_trait_path(),
);
match tcx.hir().get_if_local(data.impl_def_id) {
match tcx.hir_get_if_local(data.impl_def_id) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Impl(hir::Impl { of_trait, self_ty, .. }),
..
@ -3564,7 +3561,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let expr = tcx.hir().expect_expr(hir_id);
(expr_ty, expr)
} else if let Some(body_id) = tcx.hir_node_by_def_id(body_id).body_id()
&& let body = tcx.hir().body(body_id)
&& let body = tcx.hir_body(body_id)
&& let hir::ExprKind::Block(block, _) = body.value.kind
&& let Some(expr) = block.expr
&& let Some(expr_ty) = self
@ -3841,7 +3838,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let hir::ExprKind::Closure(hir::Closure {
body, fn_decl_span, ..
}) = value.kind
&& let body = tcx.hir().body(*body)
&& let body = tcx.hir_body(*body)
&& !matches!(body.value.kind, hir::ExprKind::Block(..))
{
// Check if the failed predicate was an expectation of a closure type
@ -4068,7 +4065,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
&& let [arg] = args
&& let hir::ExprKind::Closure(closure) = arg.kind
{
let body = tcx.hir().body(closure.body);
let body = tcx.hir_body(closure.body);
if let hir::ExprKind::Block(block, None) = body.value.kind
&& let None = block.expr
&& let [.., stmt] = block.stmts
@ -4761,7 +4758,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
{
let mut sugg_spans =
vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
let body = self.tcx.hir().body(body_id);
let body = self.tcx.hir_body(body_id);
if let hir::ExprKind::Block(b, _) = body.value.kind
&& b.expr.is_none()
{
@ -4807,7 +4804,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
debug!(?pred, ?item_def_id, ?span);
let (Some(node), true) = (
self.tcx.hir().get_if_local(item_def_id),
self.tcx.hir_get_if_local(item_def_id),
self.tcx.is_lang_item(pred.def_id(), LangItem::Sized),
) else {
return;
@ -5248,7 +5245,7 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
// If there's a body, we also need to wrap it in `async {}`
if let hir::TraitFn::Provided(body) = body {
let body = tcx.hir().body(body);
let body = tcx.hir_body(body);
let body_span = body.value.span;
let body_span_without_braces =
body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));

View file

@ -1857,7 +1857,7 @@ pub fn impl_trait_overcapture_suggestion<'tcx>(
new_params += name_as_bounds;
}
let Some(generics) = tcx.hir().get_generics(fn_def_id) else {
let Some(generics) = tcx.hir_get_generics(fn_def_id) else {
// This shouldn't happen, but don't ICE.
return None;
};

View file

@ -26,7 +26,7 @@ impl<'a> DescriptionCtx<'a> {
.parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id)
.expect_local();
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
tcx.hir_get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
param.span
} else {
@ -48,8 +48,7 @@ impl<'a> DescriptionCtx<'a> {
match fr.kind {
ty::LateParamRegionKind::Named(_, name) => {
let span = if let Some(param) = tcx
.hir()
.get_generics(scope)
.hir_get_generics(scope)
.and_then(|generics| generics.get_named(name))
{
param.span

View file

@ -128,8 +128,7 @@ fn sized_trait_bound_spans<'tcx>(
}
fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> {
tcx.hir()
.get_if_local(trait_def_id)
tcx.hir_get_if_local(trait_def_id)
.and_then(|node| match node {
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Trait(.., generics, bounds, _),
@ -304,7 +303,7 @@ pub fn dyn_compatibility_violations_for_assoc_item(
ty::AssocKind::Fn => virtual_call_violations_for_method(tcx, trait_def_id, item)
.into_iter()
.map(|v| {
let node = tcx.hir().get_if_local(item.def_id);
let node = tcx.hir_get_if_local(item.def_id);
// Get an accurate span depending on the violation.
let span = match (&v, node) {
(MethodViolationCode::ReferencesSelfInput(Some(span)), _) => *span,
@ -349,7 +348,7 @@ fn virtual_call_violations_for_method<'tcx>(
generics,
kind: hir::TraitItemKind::Fn(sig, _),
..
})) = tcx.hir().get_if_local(method.def_id).as_ref()
})) = tcx.hir_get_if_local(method.def_id).as_ref()
{
let sm = tcx.sess.source_map();
Some((
@ -383,7 +382,7 @@ fn virtual_call_violations_for_method<'tcx>(
let span = if let Some(hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(sig, _),
..
})) = tcx.hir().get_if_local(method.def_id).as_ref()
})) = tcx.hir_get_if_local(method.def_id).as_ref()
{
Some(sig.decl.inputs[i].span)
} else {
@ -421,7 +420,7 @@ fn virtual_call_violations_for_method<'tcx>(
let span = if let Some(hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(sig, _),
..
})) = tcx.hir().get_if_local(method.def_id).as_ref()
})) = tcx.hir_get_if_local(method.def_id).as_ref()
{
Some(sig.decl.inputs[0].span)
} else {

View file

@ -208,7 +208,7 @@ pub fn all_fields_implement_trait<'tcx>(
}
let field_span = tcx.def_span(field.did);
let field_ty_span = match tcx.hir().get_if_local(field.did) {
let field_ty_span = match tcx.hir_get_if_local(field.did) {
Some(hir::Node::Field(field_def)) => field_def.ty.span,
_ => field_span,
};

View file

@ -289,7 +289,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
&& let Some(impl_item) =
items.iter().find(|item| item.id.owner_id.to_def_id() == impl_item_id)
{
Some(tcx.hir().impl_item(impl_item.id).expect_type().span)
Some(tcx.hir_impl_item(impl_item.id).expect_type().span)
} else {
None
}