Skip to content
Open
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_incremental/src/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct IfThisChanged<'tcx> {

impl<'tcx> IfThisChanged<'tcx> {
fn process_attrs(&mut self, def_id: LocalDefId) {
let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
let def_path_hash = self.tcx.local_def_path_hash(def_id);
let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
let attrs = self.tcx.hir_attrs(hir_id);
for attr in attrs {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<'tcx> CleanVisitor<'tcx> {

fn check_item(&mut self, item_id: LocalDefId) {
let item_span = self.tcx.def_span(item_id.to_def_id());
let def_path_hash = self.tcx.def_path_hash(item_id.to_def_id());
let def_path_hash = self.tcx.local_def_path_hash(item_id);

let Some(clean_attrs) = find_attr!(self.tcx, item_id, RustcClean(attr) => attr) else {
return;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/default_could_be_derived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
}
let hir::ImplItemKind::Fn(_sig, body_id) = impl_item.kind else { return };
let impl_id = cx.tcx.local_parent(impl_item.owner_id.def_id);
if cx.tcx.is_automatically_derived(impl_id.to_def_id()) {
if cx.tcx.is_automatically_derived_local(impl_id) {
// We don't care about what `#[derive(Default)]` produces in this lint.
return;
}
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
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/dep_graph/dep_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'tcx> DepNodeKey<'tcx> for LocalDefId {

#[inline(always)]
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
self.to_def_id().to_fingerprint(tcx)
tcx.local_def_path_hash(*self).0
}

#[inline(always)]
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<'tcx> DepNodeKey<'tcx> for HirId {
#[inline(always)]
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
let HirId { owner, local_id } = *self;
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
let def_path_hash = tcx.local_def_path_hash(owner.def_id);
Fingerprint::new(
// `owner` is local, so is completely defined by the local hash
def_path_hash.local_hash(),
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
23 changes: 15 additions & 8 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 Expand Up @@ -1172,16 +1168,21 @@ impl<'tcx> TyCtxt<'tcx> {
self.features_query(())
}

pub fn def_key(self, id: impl IntoQueryKey<DefId>) -> rustc_hir::definitions::DefKey {
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
let id = id.into_query_key();
// Accessing the DefKey is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.definitions_untracked().def_key(id)
self.local_def_key(id)
} else {
self.cstore_untracked().def_key(id)
}
}

#[inline]
pub fn local_def_key(self, id: LocalDefId) -> rustc_hir::definitions::DefKey {
self.definitions_untracked().def_key(id)
}

/// Converts a `DefId` into its fully expanded `DefPath` (every
/// `DefId` is really just an interned `DefPath`).
///
Expand All @@ -1206,6 +1207,12 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

#[inline]
pub fn local_def_path_hash(self, def_id: LocalDefId) -> rustc_hir::definitions::DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
self.definitions_untracked().def_path_hash(def_id)
}

#[inline]
pub fn crate_types(self) -> &'tcx [CrateType] {
&self.crate_types
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,18 @@ impl TyCtxt<'_> {
}

#[inline]
#[track_caller]
pub fn opt_local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
self.opt_parent(id.to_def_id()).map(DefId::expect_local)
self.local_def_key(id).parent.map(|i| LocalDefId { local_def_index: i })
}

#[inline]
#[track_caller]
pub fn local_parent(self, id: impl Into<LocalDefId>) -> LocalDefId {
self.parent(id.into().to_def_id()).expect_local()
pub fn local_parent(self, id: LocalDefId) -> LocalDefId {
match self.opt_local_parent(id) {
Some(id) => id,
// not `unwrap_or_else` to avoid breaking caller tracking
None => bug!("{id:?} doesn't have a parent"),
}
}

pub fn is_descendant_of(self, mut descendant: DefId, ancestor: DefId) -> bool {
Expand Down Expand Up @@ -1408,7 +1411,7 @@ impl<'tcx> TyCtxt<'tcx> {

// Generate a deterministically-derived seed from the item's path hash
// to allow for cross-crate compilation to actually work
let mut field_shuffle_seed = self.def_path_hash(did.to_def_id()).0.to_smaller_hash();
let mut field_shuffle_seed = self.local_def_path_hash(did).0.to_smaller_hash();

// If the user defined a custom seed for layout randomization, xor the item's
// path hash with the user defined seed, this will allowing determinism while
Expand Down Expand Up @@ -1985,6 +1988,11 @@ impl<'tcx> TyCtxt<'tcx> {
find_attr!(self, def_id, AutomaticallyDerived(..))
}

/// Check if the given `DefId` is `#\[automatically_derived\]`.
pub fn is_automatically_derived_local(self, def_id: LocalDefId) -> bool {
find_attr!(self, def_id, AutomaticallyDerived(..))
}

/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`
/// with the name of the crate containing the impl.
pub fn span_of_impl(self, impl_def_id: DefId) -> Result<Span, Symbol> {
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 {
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
Loading
Loading