Use local key in providers

This commit is contained in:
Michael Goulet
2023-03-13 18:54:05 +00:00
parent a01b4cc9f3
commit 2eb1c08e43
65 changed files with 458 additions and 395 deletions

View File

@@ -5,16 +5,16 @@ use hir::{
};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
use rustc_hir::*;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let node = tcx.hir().get(hir_id);
let parent_def_id = match node {
@@ -121,7 +121,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
Some(parent_def_id.to_def_id())
}
Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => {
Some(tcx.typeck_root_def_id(def_id))
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
}
// Exclude `GlobalAsm` here which cannot have generics.
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
@@ -140,7 +140,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
}
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
Some(tcx.typeck_root_def_id(def_id))
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
}
Node::Item(item) => match item.kind {
ItemKind::OpaqueTy(hir::OpaqueTy {
@@ -189,7 +189,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
let opt_self = Some(ty::GenericParamDef {
index: 0,
name: kw::SelfUpper,
def_id,
def_id: def_id.to_def_id(),
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
has_default: false,
@@ -326,7 +326,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
index: next_index(),
name: Symbol::intern(arg),
def_id,
def_id: def_id.to_def_id(),
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
}));
@@ -339,7 +339,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
params.push(ty::GenericParamDef {
index: next_index(),
name: Symbol::intern("<const_ty>"),
def_id,
def_id: def_id.to_def_id(),
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
});

View File

@@ -4,7 +4,7 @@ use rustc_hir as hir;
use rustc_infer::traits::util;
use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::def_id::DefId;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::Span;
/// For associated types we include both bounds written on the type
@@ -74,9 +74,9 @@ fn opaque_type_bounds<'tcx>(
pub(super) fn explicit_item_bounds(
tcx: TyCtxt<'_>,
def_id: DefId,
def_id: LocalDefId,
) -> &'_ [(ty::Predicate<'_>, Span)] {
match tcx.opt_rpitit_info(def_id) {
match tcx.opt_rpitit_info(def_id.to_def_id()) {
// RPITIT's bounds are the same as opaque type bounds, but with
// a projection self type.
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
@@ -86,7 +86,10 @@ pub(super) fn explicit_item_bounds(
tcx,
opaque_def_id,
opaque_ty.bounds,
tcx.mk_projection(def_id, ty::InternalSubsts::identity_for_item(tcx, def_id)),
tcx.mk_projection(
def_id.to_def_id(),
ty::InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
),
item.span,
);
}
@@ -95,25 +98,25 @@ pub(super) fn explicit_item_bounds(
None => {}
}
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
match tcx.hir().get(hir_id) {
hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Type(bounds, _),
span,
..
}) => associated_type_bounds(tcx, def_id, bounds, *span),
}) => associated_type_bounds(tcx, def_id.to_def_id(), bounds, *span),
hir::Node::Item(hir::Item {
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait, .. }),
span,
..
}) => {
let substs = InternalSubsts::identity_for_item(tcx, def_id);
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
let item_ty = if *in_trait && !tcx.lower_impl_trait_in_trait_to_assoc_ty() {
tcx.mk_projection(def_id, substs)
tcx.mk_projection(def_id.to_def_id(), substs)
} else {
tcx.mk_opaque(def_id, substs)
tcx.mk_opaque(def_id.to_def_id(), substs)
};
opaque_type_bounds(tcx, def_id, bounds, item_ty, *span)
opaque_type_bounds(tcx, def_id.to_def_id(), bounds, item_ty, *span)
}
_ => bug!("item_bounds called on {:?}", def_id),
}

View File

@@ -62,16 +62,17 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic
/// Returns a list of user-specified type predicates for the definition with ID `def_id`.
/// N.B., this does not include any implied/inferred constraints.
#[instrument(level = "trace", skip(tcx), ret)]
fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
use rustc_hir::*;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let node = tcx.hir().get(hir_id);
let mut is_trait = None;
let mut is_default_impl_trait = None;
let icx = ItemCtxt::new(tcx, def_id);
// FIXME: Should ItemCtxt take a LocalDefId?
let icx = ItemCtxt::new(tcx, def_id.to_def_id());
const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
@@ -99,7 +100,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
| ItemKind::Union(_, generics) => generics,
ItemKind::Trait(_, _, generics, ..) | ItemKind::TraitAlias(generics, _) => {
is_trait = Some(ty::TraitRef::identity(tcx, def_id));
is_trait = Some(ty::TraitRef::identity(tcx, def_id.to_def_id()));
generics
}
ItemKind::OpaqueTy(OpaqueTy { generics, .. }) => generics,
@@ -253,7 +254,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
}
if tcx.features().generic_const_exprs {
predicates.extend(const_evaluatable_predicates_of(tcx, def_id.expect_local()));
predicates.extend(const_evaluatable_predicates_of(tcx, def_id));
}
let mut predicates: Vec<_> = predicates.into_iter().collect();
@@ -392,19 +393,19 @@ pub(super) fn trait_explicit_predicates_and_bounds(
def_id: LocalDefId,
) -> ty::GenericPredicates<'_> {
assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
gather_explicit_predicates_of(tcx, def_id.to_def_id())
gather_explicit_predicates_of(tcx, def_id)
}
pub(super) fn explicit_predicates_of<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
def_id: LocalDefId,
) -> ty::GenericPredicates<'tcx> {
let def_kind = tcx.def_kind(def_id);
if let DefKind::Trait = def_kind {
// Remove bounds on associated types from the predicates, they will be
// returned by `explicit_item_bounds`.
let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id.expect_local());
let trait_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
let trait_identity_substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
let is_assoc_item_ty = |ty: Ty<'tcx>| {
// For a predicate from a where clause to become a bound on an
@@ -418,7 +419,8 @@ pub(super) fn explicit_predicates_of<'tcx>(
// supertrait).
if let ty::Alias(ty::Projection, projection) = ty.kind() {
projection.substs == trait_identity_substs
&& tcx.associated_item(projection.def_id).container_id(tcx) == def_id
&& tcx.associated_item(projection.def_id).container_id(tcx)
== def_id.to_def_id()
} else {
false
}
@@ -449,7 +451,7 @@ pub(super) fn explicit_predicates_of<'tcx>(
}
} else {
if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let parent_def_id = tcx.hir().get_parent_item(hir_id);
if let Some(defaulted_param_def_id) =
@@ -537,9 +539,9 @@ pub(super) fn explicit_predicates_of<'tcx>(
/// the transitive super-predicates are converted.
pub(super) fn super_predicates_of(
tcx: TyCtxt<'_>,
trait_def_id: DefId,
trait_def_id: LocalDefId,
) -> ty::GenericPredicates<'_> {
tcx.super_predicates_that_define_assoc_type((trait_def_id, None))
tcx.super_predicates_that_define_assoc_type((trait_def_id.to_def_id(), None))
}
/// Ensures that the super-predicates of the trait with a `DefId`

View File

@@ -1051,9 +1051,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
}
}
fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifetimeDefault {
fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: LocalDefId) -> ObjectLifetimeDefault {
debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam);
let param_def_id = param_def_id.expect_local();
let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(param_def_id) else {
bug!("expected GenericParam for object_lifetime_default");
};

View File

@@ -244,11 +244,13 @@ fn get_path_containing_arg_in_pat<'hir>(
arg_path
}
pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>> {
pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty<'_>> {
// If we are computing `type_of` the synthesized associated type for an RPITIT in the impl
// side, use `collect_return_position_impl_trait_in_trait_tys` to infer the value of the
// associated type in the impl.
if let Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) {
if let Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) =
tcx.opt_rpitit_info(def_id.to_def_id())
{
match tcx.collect_return_position_impl_trait_in_trait_tys(fn_def_id) {
Ok(map) => {
let assoc_item = tcx.associated_item(def_id);
@@ -263,7 +265,6 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
}
}
let def_id = def_id.expect_local();
use rustc_hir::*;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);