Compute ty_param_owner using DefIdTree.

This commit is contained in:
Camille GILLOT 2022-04-07 22:24:07 +02:00
parent dd38eea722
commit b5dfa6a78d
4 changed files with 13 additions and 15 deletions

View File

@ -1,5 +1,5 @@
use crate::hir::{ModuleItems, Owner}; use crate::hir::{ModuleItems, Owner};
use crate::ty::TyCtxt; use crate::ty::{DefIdTree, TyCtxt};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -545,13 +545,12 @@ pub fn par_body_owners<F: Fn(LocalDefId) + Sync + Send>(self, f: F) {
}); });
} }
pub fn ty_param_owner(self, id: HirId) -> LocalDefId { pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
match self.get(id) { let def_kind = self.tcx.def_kind(def_id);
Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => { match def_kind {
id.expect_owner() DefKind::Trait | DefKind::TraitAlias => def_id,
} DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id).unwrap(),
Node::GenericParam(_) => self.get_parent_item(id), _ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id)),
} }
} }

View File

@ -2265,11 +2265,11 @@ pub fn res_to_ty(
assert_eq!(opt_self_ty, None); assert_eq!(opt_self_ty, None);
self.prohibit_generics(path.segments); self.prohibit_generics(path.segments);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let def_id = def_id.expect_local();
let item_id = tcx.hir().get_parent_node(hir_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let item_def_id = tcx.hir().local_def_id(item_id); let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id); let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id]; let index = generics.param_def_id_to_index[&def_id.to_def_id()];
tcx.mk_ty_param(index, tcx.hir().name(hir_id)) tcx.mk_ty_param(index, tcx.hir().name(hir_id))
} }
Res::SelfTy { trait_: Some(_), alias_to: None } => { Res::SelfTy { trait_: Some(_), alias_to: None } => {

View File

@ -184,8 +184,7 @@ fn get_type_parameter_bounds(
_: Ident, _: Ident,
) -> ty::GenericPredicates<'tcx> { ) -> ty::GenericPredicates<'tcx> {
let tcx = self.tcx; let tcx = self.tcx;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local());
let item_def_id = tcx.hir().ty_param_owner(hir_id);
let generics = tcx.generics_of(item_def_id); let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id]; let index = generics.param_def_id_to_index[&def_id];
ty::GenericPredicates { ty::GenericPredicates {

View File

@ -558,7 +558,7 @@ fn type_param_predicates(
// `where T: Foo`. // `where T: Foo`.
let param_id = tcx.hir().local_def_id_to_hir_id(def_id); let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
let param_owner = tcx.hir().ty_param_owner(param_id); let param_owner = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(param_owner); let generics = tcx.generics_of(param_owner);
let index = generics.param_def_id_to_index[&def_id.to_def_id()]; let index = generics.param_def_id_to_index[&def_id.to_def_id()];
let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id)); let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id));