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 {

src/librustc_typeck/collect.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,10 @@ fn type_param_predicates<'a, 'tcx>(
244244
// written inline like `<T:Foo>` or in a where clause like
245245
// `where T:Foo`.
246246

247-
let param_id = tcx.hir.as_local_node_id(def_id).unwrap();
248-
let param_owner = tcx.hir.ty_param_owner(param_id);
249-
let param_owner_def_id = tcx.hir.local_def_id(param_owner);
247+
let param_owner_def_id = tcx.ty_param_owner(def_id);
250248
let generics = tcx.generics_of(param_owner_def_id);
251249
let index = generics.param_def_id_to_index[&def_id];
252-
let ty = ty::ParamTy::new(index, def_id, tcx.hir.ty_param_name(param_id)).to_ty(tcx);
250+
let ty = tcx.mk_ty_param(generics.param_at(index, tcx));
253251

254252
// Don't look for bounds where the type parameter isn't in scope.
255253
let parent = if item_def_id == param_owner_def_id {
@@ -290,7 +288,7 @@ fn type_param_predicates<'a, 'tcx>(
290288
| ItemKind::Union(_, ref generics) => generics,
291289
ItemKind::Trait(_, _, ref generics, ..) => {
292290
// Implied `Self: Trait` and supertrait bounds.
293-
if param_id == item_node_id {
291+
if def_id == item_def_id {
294292
result
295293
.predicates
296294
.push(ty::TraitRef::identity(tcx, item_def_id).to_predicate());
@@ -310,6 +308,7 @@ fn type_param_predicates<'a, 'tcx>(
310308
};
311309

312310
let icx = ItemCtxt::new(tcx, item_def_id);
311+
let param_id = tcx.hir.as_local_node_id(def_id).unwrap();
313312
result
314313
.predicates
315314
.extend(icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty));
@@ -1773,9 +1772,7 @@ fn explicit_predicates_of<'a, 'tcx>(
17731772
for param in &ast_generics.params {
17741773
match param.kind {
17751774
GenericParamKind::Type { .. } => {
1776-
let def_id = tcx.hir.local_def_id(param.id);
1777-
let name = param.name.ident().name;
1778-
let param_ty = ty::ParamTy::new(index, def_id, name).to_ty(tcx);
1775+
let param_ty = tcx.mk_ty_param(generics.param_at(index, tcx));
17791776
index += 1;
17801777

17811778
let sized = SizedByDefault::Yes;

src/librustc_typeck/impl_wf_check.rs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ use rustc::ty::{self, TyCtxt};
2626
use rustc::util::nodemap::{FxHashMap, FxHashSet};
2727
use std::collections::hash_map::Entry::{Occupied, Vacant};
2828

29-
use syntax_pos::Span;
30-
3129
/// Checks that all the type/lifetime parameters on an impl also
3230
/// appear in the trait ref or self-type (or are constrained by a
3331
/// where-clause). These rules are needed to ensure that, given a
@@ -114,28 +112,28 @@ fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
114112
}).collect();
115113

116114
for param in &impl_generics.params {
117-
match param.kind {
115+
let ctp_param = ctp::Parameter(param.index);
116+
if input_parameters.contains(&ctp_param) {
117+
continue;
118+
}
119+
let kind = match param.kind {
118120
// Disallow ANY unconstrained type parameters.
119-
ty::GenericParamDefKind::Type {..} => {
120-
let param_ty = ty::ParamTy::for_def(param);
121-
if !input_parameters.contains(&ctp::Parameter::from(param_ty)) {
122-
report_unused_parameter(tcx,
123-
tcx.def_span(param.def_id),
124-
"type",
125-
&param_ty.to_string());
126-
}
127-
}
121+
ty::GenericParamDefKind::Type {..} => "type",
128122
ty::GenericParamDefKind::Lifetime => {
129-
let param_lt = ctp::Parameter::from(param.to_early_bound_region_data());
130-
if lifetimes_in_associated_types.contains(&param_lt) && // (*)
131-
!input_parameters.contains(&param_lt) {
132-
report_unused_parameter(tcx,
133-
tcx.def_span(param.def_id),
134-
"lifetime",
135-
&param.name.to_string());
123+
if !lifetimes_in_associated_types.contains(&ctp_param) { // (*)
124+
continue;
136125
}
126+
"lifetime"
137127
}
138-
}
128+
};
129+
let span = tcx.def_span(param.def_id);
130+
struct_span_err!(
131+
tcx.sess, span, E0207,
132+
"the {} parameter `{}` is not constrained by the \
133+
impl trait, self type, or predicates",
134+
kind, param.name)
135+
.span_label(span, format!("unconstrained {} parameter", kind))
136+
.emit();
139137
}
140138

141139
// (*) This is a horrible concession to reality. I think it'd be
@@ -158,20 +156,6 @@ fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
158156
// used elsewhere are not projected back out.
159157
}
160158

161-
fn report_unused_parameter(tcx: TyCtxt,
162-
span: Span,
163-
kind: &str,
164-
name: &str)
165-
{
166-
struct_span_err!(
167-
tcx.sess, span, E0207,
168-
"the {} parameter `{}` is not constrained by the \
169-
impl trait, self type, or predicates",
170-
kind, name)
171-
.span_label(span, format!("unconstrained {} parameter", kind))
172-
.emit();
173-
}
174-
175159
/// Enforce that we do not have two items in an impl with the same name.
176160
fn enforce_impl_items_are_distinct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
177161
impl_item_refs: &[hir::ImplItemRef])

src/librustc_typeck/outlives/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn insert_outlives_predicate<'tcx>(
7777
// Vec<U>`. Decomposing `Vec<U>` into
7878
// components would yield `U`, and we add the
7979
// where clause that `U: 'a`.
80-
let ty: Ty<'tcx> = param_ty.to_ty(tcx);
80+
let ty = tcx.mk_ty(ty::Param(param_ty));
8181
required_predicates
8282
.insert(ty::OutlivesPredicate(ty.into(), outlived_region));
8383
}

0 commit comments

Comments
 (0)