Skip to content

Commit 0d5122a

Browse files
committed
rustc: support cross-crate DefId's in ty_param_{name,owner}.
1 parent b3e001b commit 0d5122a

File tree

13 files changed

+72
-99
lines changed

13 files changed

+72
-99
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -602,27 +602,6 @@ impl<'hir> Map<'hir> {
602602
}
603603
}
604604

605-
pub fn ty_param_owner(&self, id: NodeId) -> NodeId {
606-
match self.get(id) {
607-
NodeItem(&Item { node: ItemKind::Trait(..), .. }) => id,
608-
NodeGenericParam(_) => self.get_parent_node(id),
609-
_ => {
610-
bug!("ty_param_owner: {} not a type parameter",
611-
self.node_to_string(id))
612-
}
613-
}
614-
}
615-
616-
pub fn ty_param_name(&self, id: NodeId) -> Name {
617-
match self.get(id) {
618-
NodeItem(&Item { node: ItemKind::Trait(..), .. }) => {
619-
keywords::SelfType.name()
620-
}
621-
NodeGenericParam(param) => param.name.ident().name,
622-
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
623-
}
624-
}
625-
626605
pub fn trait_impls(&self, trait_did: DefId) -> &'hir [NodeId] {
627606
self.dep_graph.read(DepNode::new_no_params(DepKind::AllLocalTraitImpls));
628607

src/librustc/infer/region_constraints/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,8 @@ impl<'tcx> fmt::Display for GenericKind<'tcx> {
875875
impl<'a, 'gcx, 'tcx> GenericKind<'tcx> {
876876
pub fn to_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
877877
match *self {
878-
GenericKind::Param(ref p) => p.to_ty(tcx),
879-
GenericKind::Projection(ref p) => tcx.mk_projection(p.item_def_id, p.substs),
878+
GenericKind::Param(p) => tcx.mk_ty(ty::Param(p)),
879+
GenericKind::Projection(p) => tcx.mk_projection(p.item_def_id, p.substs),
880880
}
881881
}
882882
}

src/librustc/traits/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
213213
},
214214

215215
Component::Param(p) => {
216-
let ty = p.to_ty(tcx);
216+
let ty = tcx.mk_ty(ty::Param(p));
217217
Some(ty::Predicate::TypeOutlives(
218218
ty::Binder::dummy(ty::OutlivesPredicate(ty, r_min))))
219219
},

src/librustc/ty/context.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use traits::{Clause, Clauses, Goal, Goals};
4040
use ty::{self, Ty, TypeAndMut};
4141
use ty::{TyS, TyKind, List};
4242
use ty::{AdtKind, AdtDef, ClosureSubsts, GeneratorSubsts, Region, Const};
43-
use ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predicate};
43+
use ty::{PolyFnSig, InferTy, ProjectionTy, ExistentialPredicate, Predicate};
4444
use ty::RegionKind;
4545
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
4646
use ty::TyKind::*;
@@ -76,7 +76,7 @@ use syntax::attr;
7676
use syntax::source_map::MultiSpan;
7777
use syntax::edition::Edition;
7878
use syntax::feature_gate;
79-
use syntax::symbol::Symbol;
79+
use syntax::symbol::{keywords, Symbol};
8080
use syntax_pos::Span;
8181

8282
use hir;
@@ -2557,11 +2557,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25572557
}
25582558

25592559
pub fn mk_ty_param(self, def: &ty::GenericParamDef) -> Ty<'tcx> {
2560-
ParamTy::for_def(def).to_ty(self)
2560+
self.mk_ty(ty::Param(ty::ParamTy {
2561+
idx: def.index,
2562+
def_id: def.def_id,
2563+
name: def.name,
2564+
}))
25612565
}
25622566

25632567
pub fn mk_self_type(self, trait_def_id: DefId) -> Ty<'tcx> {
2564-
ParamTy::for_self(trait_def_id).to_ty(self)
2568+
self.mk_ty(ty::Param(ty::ParamTy {
2569+
idx: 0,
2570+
def_id: trait_def_id,
2571+
name: keywords::SelfType.name().as_interned_str(),
2572+
}))
25652573
}
25662574

25672575
pub fn mk_param(self, param: &ty::GenericParamDef) -> Kind<'tcx> {

src/librustc/ty/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,6 +2637,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26372637
}
26382638
}
26392639

2640+
pub fn ty_param_name(&self, id: DefId) -> InternedString {
2641+
let def_key = self.def_key(id);
2642+
match def_key.disambiguated_data.data {
2643+
DefPathData::Trait(_) => {
2644+
keywords::SelfType.name().as_interned_str()
2645+
}
2646+
DefPathData::TypeParam(name) => name,
2647+
_ => bug!("ty_param_name: {:?} not a type parameter", id),
2648+
}
2649+
}
2650+
2651+
pub fn ty_param_owner(&self, id: DefId) -> DefId {
2652+
let def_key = self.def_key(id);
2653+
match def_key.disambiguated_data.data {
2654+
DefPathData::Trait(_) => id,
2655+
DefPathData::TypeParam(_) => DefId {
2656+
krate: id.krate,
2657+
index: def_key.parent.unwrap()
2658+
},
2659+
_ => bug!("ty_param_owner: {:?} not a type parameter", id),
2660+
}
2661+
}
2662+
26402663
/// Return the possibly-auto-generated MIR of a (DefId, Subst) pair.
26412664
pub fn instance_mir(self, instance: ty::InstanceDef<'gcx>)
26422665
-> &'gcx Mir<'gcx>

src/librustc/ty/query/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ impl<'tcx> QueryDescription<'tcx> for queries::erase_regions_ty<'tcx> {
212212

213213
impl<'tcx> QueryDescription<'tcx> for queries::type_param_predicates<'tcx> {
214214
fn describe(tcx: TyCtxt, (_, def_id): (DefId, DefId)) -> String {
215-
let id = tcx.hir.as_local_node_id(def_id).unwrap();
216215
format!("computing the bounds for type parameter `{}`",
217-
tcx.hir.ty_param_name(id))
216+
tcx.ty_param_name(def_id))
218217
}
219218
}
220219

src/librustc/ty/sty.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::iter;
2626
use std::cmp::Ordering;
2727
use rustc_target::spec::abi;
2828
use syntax::ast::{self, Ident};
29-
use syntax::symbol::{keywords, InternedString};
29+
use syntax::symbol::InternedString;
3030

3131
use serialize;
3232

@@ -967,24 +967,6 @@ pub struct ParamTy {
967967
pub name: InternedString,
968968
}
969969

970-
impl ParamTy {
971-
pub fn new(idx: u32, def_id: DefId, name: ast::Name) -> ParamTy {
972-
ParamTy { idx, def_id, name: name.as_interned_str() }
973-
}
974-
975-
pub fn for_self(trait_def_id: DefId) -> ParamTy {
976-
ParamTy::new(0, trait_def_id, keywords::SelfType.name())
977-
}
978-
979-
pub fn for_def(def: &ty::GenericParamDef) -> ParamTy {
980-
ParamTy { idx: def.index, def_id: def.def_id, name: def.name }
981-
}
982-
983-
pub fn to_ty(self, tcx: TyCtxt<'_, '_, 'tcx>) -> Ty<'tcx> {
984-
tcx.mk_ty(ty::Param(self))
985-
}
986-
}
987-
988970
/// A [De Bruijn index][dbi] is a standard means of representing
989971
/// regions (and perhaps later types) in a higher-ranked setting. In
990972
/// particular, imagine a type like this:

src/librustc_driver/test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
314314
pub fn t_param(&self, index: u32) -> Ty<'tcx> {
315315
let def_id = self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID);
316316
let name = format!("T{}", index);
317-
ty::ParamTy::new(index, def_id, Symbol::intern(&name)).to_ty(self.infcx.tcx)
317+
self.infcx.tcx.mk_ty(ty::TyParam(ty::ParamTy {
318+
idx: index,
319+
def_id,
320+
name: Symbol::intern(&name).as_interned_str()
321+
}))
318322
}
319323

320324
pub fn re_early_bound(&self, index: u32, name: &'static str) -> ty::Region<'tcx> {

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
11321132
traits::transitive_bounds(tcx, &bounds)
11331133
.filter(|b| self.trait_defines_associated_type_named(b.def_id(), assoc_name));
11341134

1135-
let param_node_id = tcx.hir.as_local_node_id(ty_param_def_id).unwrap();
1136-
let param_name = tcx.hir.ty_param_name(param_node_id);
1135+
let param_name = tcx.ty_param_name(ty_param_def_id);
11371136
self.one_bound_for_assoc_type(suitable_bounds,
11381137
&param_name.as_str(),
11391138
assoc_name,
@@ -1412,7 +1411,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
14121411
let item_def_id = tcx.hir.local_def_id(item_id);
14131412
let generics = tcx.generics_of(item_def_id);
14141413
let index = generics.param_def_id_to_index[&tcx.hir.local_def_id(node_id)];
1415-
ty::ParamTy::new(index, did, tcx.hir.name(node_id)).to_ty(tcx)
1414+
tcx.mk_ty_param(generics.param_at(index, tcx))
14161415
}
14171416
Def::SelfTy(_, Some(def_id)) => {
14181417
// Self in impl (we know the concrete type).

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,9 +1825,7 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
18251825
-> ty::GenericPredicates<'tcx>
18261826
{
18271827
let tcx = self.tcx;
1828-
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
1829-
let item_id = tcx.hir.ty_param_owner(node_id);
1830-
let item_def_id = tcx.hir.local_def_id(item_id);
1828+
let item_def_id = tcx.ty_param_owner(def_id);
18311829
let generics = tcx.generics_of(item_def_id);
18321830
let index = generics.param_def_id_to_index[&def_id];
18331831
ty::GenericPredicates {

0 commit comments

Comments
 (0)