Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,10 +1569,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let tcx = self.infcx.tcx;
let generics = tcx.generics_of(self.mir_def_id());

let Some(hir_generics) = tcx
.typeck_root_def_id(self.mir_def_id().to_def_id())
.as_local()
.and_then(|def_id| tcx.hir_get_generics(def_id))
let Some(hir_generics) =
tcx.hir_get_generics(tcx.typeck_root_def_id_local(self.mir_def_id()))
else {
return;
};
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,12 +1274,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
if let ty::Param(param_ty) = *self_ty.kind()
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
&& let param = generics.type_param(param_ty, self.infcx.tcx)
&& let Some(hir_generics) = self
.infcx
.tcx
.typeck_root_def_id(self.mir_def_id().to_def_id())
.as_local()
.and_then(|def_id| self.infcx.tcx.hir_get_generics(def_id))
&& let Some(hir_generics) = self.infcx.tcx.hir_get_generics(
self.infcx.tcx.typeck_root_def_id_local(self.mir_def_id()),
)
&& let spans = hir_generics
.predicates
.iter()
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,10 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
let mut indices = self.compute_indices(fr_static, defining_ty);
debug!("build: indices={:?}", indices);

let typeck_root_def_id = self.infcx.tcx.typeck_root_def_id(self.mir_def.to_def_id());

// If this is a 'root' body (not a closure/coroutine/inline const), then
// there are no extern regions, so the local regions start at the same
// position as the (empty) sub-list of extern regions
let first_local_index = if self.mir_def.to_def_id() == typeck_root_def_id {
let first_local_index = if !self.infcx.tcx.is_typeck_child(self.mir_def.to_def_id()) {
first_extern_index
} else {
// If this is a closure, coroutine, or inline-const, then the late-bound regions from the enclosing
Expand Down Expand Up @@ -583,7 +581,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
/// see `DefiningTy` for details.
fn defining_ty(&self) -> DefiningTy<'tcx> {
let tcx = self.infcx.tcx;
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
let typeck_root_def_id = tcx.typeck_root_def_id_local(self.mir_def);

match tcx.hir_body_owner_kind(self.mir_def) {
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
Expand Down Expand Up @@ -614,7 +612,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {

BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => {
let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
if self.mir_def.to_def_id() == typeck_root_def_id {
if self.mir_def == typeck_root_def_id {
let args = self.infcx.replace_free_regions_with_nll_infer_vars(
NllRegionVariableOrigin::FreeRegion,
identity_args,
Expand Down Expand Up @@ -660,7 +658,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
defining_ty: DefiningTy<'tcx>,
) -> UniversalRegionIndices<'tcx> {
let tcx = self.infcx.tcx;
let typeck_root_def_id = tcx.typeck_root_def_id(self.mir_def.to_def_id());
let typeck_root_def_id = tcx.typeck_root_def_id_local(self.mir_def);
let identity_args = GenericArgs::identity_for_item(tcx, typeck_root_def_id);
let renumbered_args = defining_ty.args();

Expand Down Expand Up @@ -948,16 +946,14 @@ fn for_each_late_bound_region_in_recursive_scope<'tcx>(
mut mir_def_id: LocalDefId,
mut f: impl FnMut(ty::Region<'tcx>),
) {
let typeck_root_def_id = tcx.typeck_root_def_id(mir_def_id.to_def_id());

// Walk up the tree, collecting late-bound regions until we hit the typeck root
loop {
for_each_late_bound_region_in_item(tcx, mir_def_id, &mut f);

if mir_def_id.to_def_id() == typeck_root_def_id {
break;
} else {
if tcx.is_typeck_child(mir_def_id.to_def_id()) {
mir_def_id = tcx.local_parent(mir_def_id);
} else {
break;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::mem;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Arm, Block, Expr, LetStmt, Pat, PatKind, Stmt};
use rustc_index::Idx;
Expand Down Expand Up @@ -849,13 +849,13 @@ impl<'tcx> Visitor<'tcx> for ScopeResolutionVisitor<'tcx> {
/// re-use in incremental scenarios. We may sometimes need to rerun the
/// type checker even when the HIR hasn't changed, and in those cases
/// we can avoid reconstructing the region scope tree.
pub(crate) fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
pub(crate) fn region_scope_tree(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &ScopeTree {
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
if typeck_root_def_id != def_id {
return tcx.region_scope_tree(typeck_root_def_id);
}

let scope_tree = if let Some(body) = tcx.hir_maybe_body_owned_by(def_id.expect_local()) {
let scope_tree = if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
let mut visitor = ScopeResolutionVisitor {
tcx,
scope_tree: ScopeTree::default(),
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
| Node::Ctor(..)
| Node::Field(_) => {
let parent_id = tcx.hir_get_parent_item(hir_id);
Some(parent_id.to_def_id())
Some(parent_id.def_id)
}
// FIXME(#43408) always enable this once `lazy_normalization` is
// stable enough and does not need a feature gate anymore.
Node::AnonConst(_) => {
let parent_did = tcx.parent(def_id.to_def_id());
let parent_did = tcx.local_parent(def_id);
debug!(?parent_did);

let mut in_param_ty = false;
Expand Down Expand Up @@ -175,7 +175,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
}
Node::ConstBlock(_)
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
Some(tcx.typeck_root_def_id_local(def_id))
}
Node::OpaqueTy(&hir::OpaqueTy {
origin:
Expand All @@ -188,7 +188,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
} else {
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);
}
Some(fn_def_id.to_def_id())
Some(fn_def_id)
}
Node::OpaqueTy(&hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty },
Expand All @@ -202,7 +202,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent);
// Opaque types are always nested within another item, and
// inherit the generics of the item.
Some(parent.to_def_id())
Some(parent)
}

// All of these nodes have no parent from which to inherit generics.
Expand Down Expand Up @@ -380,7 +380,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
own_params.iter().map(|param| (param.def_id, param.index)).collect();

ty::Generics {
parent: parent_def_id,
parent: parent_def_id.map(LocalDefId::to_def_id),
parent_count,
own_params,
param_def_id_to_index,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
);

return ty::GenericPredicates {
parent: Some(tcx.parent(def_id.to_def_id())),
parent: Some(tcx.local_parent(def_id).to_def_id()),
predicates: tcx.arena.alloc_from_iter(predicates),
};
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!(?bound_sig, ?liberated_sig);

let parent_args =
GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id(expr_def_id.to_def_id()));
GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id_local(expr_def_id));

let tupled_upvars_ty = self.next_ty_var(expr_span);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn typeck_with_inspect<'tcx>(
) -> &'tcx ty::TypeckResults<'tcx> {
// Closures' typeck results come from their outermost function,
// as they are part of the same "inference environment".
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local();
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
if typeck_root_def_id != def_id {
return tcx.typeck(typeck_root_def_id);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
is_stub: false,
},
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(),
stable_crate_id: tcx.stable_crate_id(LOCAL_CRATE),
required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE),
panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop,
edition: tcx.sess.edition(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ rustc_queries! {

/// Per-body `region::ScopeTree`. The `DefId` should be the owner `DefId` for the body;
/// in the case of closures, this will be redirected to the enclosing function.
query region_scope_tree(def_id: DefId) -> &'tcx crate::middle::region::ScopeTree {
query region_scope_tree(def_id: LocalDefId) -> &'tcx crate::middle::region::ScopeTree {
desc { "computing drop scopes for `{}`", tcx.def_path_str(def_id) }
}

Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,12 +899,8 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn has_typeck_results(self, def_id: LocalDefId) -> bool {
// Closures' typeck results come from their outermost function,
// as they are part of the same "inference environment".
let typeck_root_def_id = self.typeck_root_def_id(def_id.to_def_id());
if typeck_root_def_id != def_id.to_def_id() {
return self.has_typeck_results(typeck_root_def_id.expect_local());
}

self.hir_node_by_def_id(def_id).body_id().is_some()
let root = self.typeck_root_def_id_local(def_id);
self.hir_node_by_def_id(root).body_id().is_some()
}

/// Expects a body and returns its codegen attributes.
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,20 @@ impl<'tcx> TyCtxt<'tcx> {
def_id
}

/// Given the `LocalDefId`, returns the `LocalDefId` of the innermost item that
/// has its own type-checking context or "inference environment".
///
/// For example, a closure has its own `LocalDefId`, but it is type-checked
/// with the containing item. Therefore, when we fetch the `typeck` of the closure,
/// for example, we really wind up fetching the `typeck` of the enclosing fn item.
pub fn typeck_root_def_id_local(self, def_id: LocalDefId) -> LocalDefId {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body can be changed to typeck_root_def_id(def_id.to_def_id()).expect_local().
(Because the copy-pasted version doesn't show perf benefits while being more complex.)

let mut def_id = def_id;
while self.is_typeck_child(def_id.to_def_id()) {
def_id = self.local_parent(def_id);
}
def_id
}

/// Given the `DefId` and args a closure, creates the type of
/// `self` argument that the closure expects. For example, for a
/// `Fn` closure, this would return a reference type `&T` where
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,16 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
hir::InlineAsmOperand::Const { ref anon_const } => {
let ty = self.typeck_results.node_type(anon_const.hir_id);
let did = anon_const.def_id.to_def_id();
let typeck_root_def_id = tcx.typeck_root_def_id(did);
let did = anon_const.def_id;
let typeck_root_def_id = tcx.typeck_root_def_id_local(did);
let parent_args = tcx.erase_and_anonymize_regions(
GenericArgs::identity_for_item(tcx, typeck_root_def_id),
);
let args =
InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty })
.args;

let uneval = mir::UnevaluatedConst::new(did, args);
let uneval = mir::UnevaluatedConst::new(did.to_def_id(), args);
let value = mir::Const::Unevaluated(uneval, ty);
InlineAsmOperand::Const { value, span: tcx.def_span(did) }
}
Expand Down Expand Up @@ -895,15 +895,15 @@ impl<'tcx> ThirBuildCx<'tcx> {

hir::ExprKind::ConstBlock(ref anon_const) => {
let ty = self.typeck_results.node_type(anon_const.hir_id);
let did = anon_const.def_id.to_def_id();
let typeck_root_def_id = tcx.typeck_root_def_id(did);
let did = anon_const.def_id;
let typeck_root_def_id = tcx.typeck_root_def_id_local(did);
let parent_args = tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(
tcx,
typeck_root_def_id,
));
let args = InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty }).args;

ExprKind::ConstBlock { did, args }
ExprKind::ConstBlock { did: did.to_def_id(), args }
}
// Now comes the rote stuff:
hir::ExprKind::Repeat(v, _) => {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &

// We only need to borrowck non-synthetic MIR.
let tainted_by_errors = if !tcx.is_synthetic_mir(def) {
tcx.mir_borrowck(tcx.typeck_root_def_id(def.to_def_id()).expect_local()).err()
tcx.mir_borrowck(tcx.typeck_root_def_id_local(def)).err()
} else {
None
};
Expand Down Expand Up @@ -554,14 +554,14 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
//
// We do this check here and not during `mir_promoted` because that may result
// in borrowck cycles if WF requires looking into an opaque hidden type.
let root = tcx.typeck_root_def_id(def.to_def_id());
let root = tcx.typeck_root_def_id_local(def);
match tcx.def_kind(root) {
DefKind::Fn
| DefKind::AssocFn
| DefKind::Static { .. }
| DefKind::Const { .. }
| DefKind::AssocConst { .. } => {
if let Err(guar) = tcx.ensure_result().check_well_formed(root.expect_local()) {
if let Err(guar) = tcx.ensure_result().check_well_formed(root) {
body.tainted_by_errors = Some(guar);
}
}
Expand Down Expand Up @@ -840,7 +840,7 @@ fn promoted_mir(tcx: TyCtxt<'_>, def: LocalDefId) -> &IndexVec<Promoted, Body<'_
}

if !tcx.is_synthetic_mir(def) {
tcx.ensure_done().mir_borrowck(tcx.typeck_root_def_id(def.to_def_id()).expect_local());
tcx.ensure_done().mir_borrowck(tcx.typeck_root_def_id_local(def));
}
let mut promoted = tcx.mir_promoted(def).1.steal();

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) fn check_liveness<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Den
}

// Don't run unused pass for #[derive]
let parent = tcx.parent(tcx.typeck_root_def_id(def_id.to_def_id()));
let parent = tcx.local_parent(tcx.typeck_root_def_id_local(def_id));
if let DefKind::Impl { of_trait: true } = tcx.def_kind(parent)
&& find_attr!(tcx, parent, AutomaticallyDerived(..))
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ impl<'v> RootCollector<'_, 'v> {
if (self.strategy == MonoItemCollectionStrategy::Eager || is_pub_fn_coroutine)
&& !self
.tcx
.generics_of(self.tcx.typeck_root_def_id(def_id.to_def_id()))
.generics_of(self.tcx.typeck_root_def_id_local(def_id))
.requires_monomorphization(self.tcx)
{
let instance = match *self.tcx.type_of(def_id).instantiate_identity().kind() {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
/// for discussion).
fn should_ignore_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) -> bool {
if let hir::ImplItemImplKind::Trait { .. } = impl_item.impl_kind
&& let impl_of = self.tcx.parent(impl_item.owner_id.to_def_id())
&& self.tcx.is_automatically_derived(impl_of)
&& let impl_of = self.tcx.local_parent(impl_item.owner_id.def_id)
&& self.tcx.is_automatically_derived(impl_of.to_def_id())
&& let trait_ref = self.tcx.impl_trait_ref(impl_of).instantiate_identity()
&& find_attr!(self.tcx, trait_ref.def_id, RustcTrivialFieldReads)
{
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_symbol_mangling/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ fn get_symbol_hash<'tcx>(
args.hash_stable(hcx, &mut hasher);

if let Some(instantiating_crate) = instantiating_crate {
tcx.def_path_hash(instantiating_crate.as_def_id())
.stable_crate_id()
.hash_stable(hcx, &mut hasher);
tcx.stable_crate_id(instantiating_crate).hash_stable(hcx, &mut hasher);
}

// We want to avoid accidental collision between different types of instances.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.push("C");
if !self.is_exportable {
let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
let stable_crate_id = self.tcx.stable_crate_id(cnum);
self.push_disambiguator(stable_crate_id.as_u64());
}
let name = self.tcx.crate_name(cnum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};

let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, term, ty);
if let Some(body) = self.tcx.hir_maybe_body_owned_by(
self.tcx.typeck_root_def_id(body_def_id.to_def_id()).expect_local(),
) {
if let Some(body) =
self.tcx.hir_maybe_body_owned_by(self.tcx.typeck_root_def_id_local(body_def_id))
{
let expr = body.value;
local_visitor.visit_expr(expr);
}

let Some(InferSource { span, kind }) = local_visitor.infer_source else {
let silence = if let DefKind::AssocFn = self.tcx.def_kind(body_def_id)
&& let parent = self.tcx.parent(body_def_id.into())
&& self.tcx.is_automatically_derived(parent)
&& let Some(parent) = parent.as_local()
&& let parent = self.tcx.local_parent(body_def_id)
&& self.tcx.is_automatically_derived(parent.to_def_id())
&& let hir::Node::Item(item) = self.tcx.hir_node_by_def_id(parent)
&& let hir::ItemKind::Impl(imp) = item.kind
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = imp.self_ty.kind
Expand Down
Loading
Loading