Skip to content

Commit 79068b7

Browse files
committed
rustc: keep the DefId of type parameters in types (ParamTy).
1 parent 12f8341 commit 79068b7

36 files changed

+169
-154
lines changed

src/librustc/ich/impls_ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ for ty::FloatVid
940940

941941
impl_stable_hash_for!(struct ty::ParamTy {
942942
idx,
943+
def_id,
943944
name
944945
});
945946

src/librustc/infer/error_reporting/mod.rs

+23-29
Original file line numberDiff line numberDiff line change
@@ -1112,36 +1112,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11121112
{
11131113
// Attempt to obtain the span of the parameter so we can
11141114
// suggest adding an explicit lifetime bound to it.
1115-
let type_param_span = match (self.in_progress_tables, bound_kind) {
1116-
(Some(ref table), GenericKind::Param(ref param)) => {
1117-
let table = table.borrow();
1118-
table.local_id_root.and_then(|did| {
1119-
let generics = self.tcx.generics_of(did);
1120-
let type_param = generics.type_param(param, self.tcx);
1121-
let hir = &self.tcx.hir;
1122-
hir.as_local_node_id(type_param.def_id).and_then(|id| {
1123-
// Get the `hir::Param` to verify whether it already has any bounds.
1124-
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
1125-
// instead we suggest `T: 'a + 'b` in that case.
1126-
// Also, `Self` isn't in the HIR so we rule it out here.
1127-
if let hir_map::NodeGenericParam(ref hir_param) = hir.get(id) {
1128-
let has_bounds = !hir_param.bounds.is_empty();
1129-
let sp = hir.span(id);
1130-
// `sp` only covers `T`, change it so that it covers
1131-
// `T:` when appropriate
1132-
let sp = if has_bounds {
1133-
sp.to(self.tcx
1134-
.sess
1135-
.source_map()
1136-
.next_point(self.tcx.sess.source_map().next_point(sp)))
1137-
} else {
1138-
sp
1139-
};
1140-
Some((sp, has_bounds))
1115+
let type_param_span = match bound_kind {
1116+
GenericKind::Param(ref param) => {
1117+
self.tcx.hir.as_local_node_id(param.def_id).and_then(|id| {
1118+
// Get the `hir::Param` to verify whether it already has any bounds.
1119+
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
1120+
// instead we suggest `T: 'a + 'b` in that case.
1121+
// Also, `Self` isn't in the HIR so we rule it out here.
1122+
if let hir_map::NodeGenericParam(ref hir_param) = self.tcx.hir.get(id) {
1123+
let has_bounds = !hir_param.bounds.is_empty();
1124+
let sp = self.tcx.hir.span(id);
1125+
// `sp` only covers `T`, change it so that it covers
1126+
// `T:` when appropriate
1127+
let sp = if has_bounds {
1128+
sp.to(self.tcx
1129+
.sess
1130+
.source_map()
1131+
.next_point(self.tcx.sess.source_map().next_point(sp)))
11411132
} else {
1142-
None
1143-
}
1144-
})
1133+
sp
1134+
};
1135+
Some((sp, has_bounds))
1136+
} else {
1137+
None
1138+
}
11451139
})
11461140
}
11471141
_ => None,

src/librustc/traits/object_safety.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
169169
trait_def_id: DefId,
170170
supertraits_only: bool) -> bool
171171
{
172-
let trait_self_ty = self.mk_self_type();
172+
let trait_self_ty = self.mk_self_type(trait_def_id);
173173
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
174174
let predicates = if supertraits_only {
175175
self.super_predicates_of(trait_def_id)
@@ -203,11 +203,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
203203
}
204204

205205
fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
206-
self.generics_require_sized_self(trait_def_id)
206+
self.generics_require_sized_self(trait_def_id, trait_def_id)
207207
}
208208

209-
fn generics_require_sized_self(self, def_id: DefId) -> bool {
210-
let trait_self_ty = self.mk_self_type();
209+
fn generics_require_sized_self(self, trait_def_id: DefId, def_id: DefId) -> bool {
210+
let trait_self_ty = self.mk_self_type(trait_def_id);
211211
let sized_def_id = match self.lang_items().sized_trait() {
212212
Some(def_id) => def_id,
213213
None => { return false; /* No Sized trait, can't require it! */ }
@@ -245,7 +245,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
245245
{
246246
// Any method that has a `Self : Sized` requisite is otherwise
247247
// exempt from the regulations.
248-
if self.generics_require_sized_self(method.def_id) {
248+
if self.generics_require_sized_self(trait_def_id, method.def_id) {
249249
return None;
250250
}
251251

@@ -262,7 +262,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
262262
-> bool
263263
{
264264
// Any method that has a `Self : Sized` requisite can't be called.
265-
if self.generics_require_sized_self(method.def_id) {
265+
if self.generics_require_sized_self(trait_def_id, method.def_id) {
266266
return false;
267267
}
268268

@@ -290,7 +290,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
290290

291291
let sig = self.fn_sig(method.def_id);
292292

293-
let self_ty = self.mk_self_type();
293+
let self_ty = self.mk_self_type(trait_def_id);
294294
let self_arg_ty = sig.skip_binder().inputs()[0];
295295
if let ExplicitSelf::Other = ExplicitSelf::determine(self_arg_ty, |ty| ty == self_ty) {
296296
return Some(MethodViolationCode::NonStandardSelfType);
@@ -371,7 +371,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
371371
// object type, and we cannot resolve `Self as SomeOtherTrait`
372372
// without knowing what `Self` is.
373373

374-
let trait_self_ty = self.mk_self_type();
374+
let trait_self_ty = self.mk_self_type(trait_def_id);
375375
let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
376376
let mut error = false;
377377
ty.maybe_walk(|ty| {

src/librustc/traits/util.rs

+1-1
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 = tcx.mk_ty_param(p.idx, p.name);
216+
let ty = p.to_ty(tcx);
217217
Some(ty::Predicate::TypeOutlives(
218218
ty::Binder::dummy(ty::OutlivesPredicate(ty, r_min))))
219219
},

src/librustc/ty/context.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -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, keywords, InternedString};
79+
use syntax::symbol::Symbol;
8080
use syntax_pos::Span;
8181

8282
use hir;
@@ -2556,22 +2556,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25562556
self.mk_ty(Infer(it))
25572557
}
25582558

2559-
pub fn mk_ty_param(self,
2560-
index: u32,
2561-
name: InternedString) -> Ty<'tcx> {
2562-
self.mk_ty(Param(ParamTy { idx: index, name: name }))
2559+
pub fn mk_ty_param(self, def: &ty::GenericParamDef) -> Ty<'tcx> {
2560+
ParamTy::for_def(def).to_ty(self)
25632561
}
25642562

2565-
pub fn mk_self_type(self) -> Ty<'tcx> {
2566-
self.mk_ty_param(0, keywords::SelfType.name().as_interned_str())
2563+
pub fn mk_self_type(self, trait_def_id: DefId) -> Ty<'tcx> {
2564+
ParamTy::for_self(trait_def_id).to_ty(self)
25672565
}
25682566

2569-
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
2567+
pub fn mk_param(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
25702568
match param.kind {
25712569
GenericParamDefKind::Lifetime => {
25722570
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
25732571
}
2574-
GenericParamDefKind::Type {..} => self.mk_ty_param(param.index, param.name).into(),
2572+
GenericParamDefKind::Type {..} => self.mk_ty_param(param).into(),
25752573
}
25762574
}
25772575

src/librustc/ty/sty.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -963,24 +963,25 @@ impl<'tcx> PolyFnSig<'tcx> {
963963
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
964964
pub struct ParamTy {
965965
pub idx: u32,
966+
pub def_id: DefId,
966967
pub name: InternedString,
967968
}
968969

969-
impl<'a, 'gcx, 'tcx> ParamTy {
970-
pub fn new(index: u32, name: InternedString) -> ParamTy {
971-
ParamTy { idx: index, name: name }
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() }
972973
}
973974

974-
pub fn for_self() -> ParamTy {
975-
ParamTy::new(0, keywords::SelfType.name().as_interned_str())
975+
pub fn for_self(trait_def_id: DefId) -> ParamTy {
976+
ParamTy::new(0, trait_def_id, keywords::SelfType.name())
976977
}
977978

978979
pub fn for_def(def: &ty::GenericParamDef) -> ParamTy {
979-
ParamTy::new(def.index, def.name)
980+
ParamTy { idx: def.index, def_id: def.def_id, name: def.name }
980981
}
981982

982-
pub fn to_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
983-
tcx.mk_ty_param(self.idx, self.name)
983+
pub fn to_ty(self, tcx: TyCtxt<'_, '_, 'tcx>) -> Ty<'tcx> {
984+
tcx.mk_ty(ty::Param(self))
984985
}
985986
}
986987

src/librustc/ty/subst.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
185185
/// Creates a Substs that maps each generic parameter to itself.
186186
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)
187187
-> &'tcx Substs<'tcx> {
188-
Substs::for_item(tcx, def_id, |param, _| {
189-
tcx.mk_param_from_def(param)
190-
})
188+
Substs::for_item(tcx, def_id, |param, _| tcx.mk_param(param))
191189
}
192190

193191
/// Creates a Substs for generic parameter definitions,

src/librustc_driver/test.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
312312
}
313313

314314
pub fn t_param(&self, index: u32) -> Ty<'tcx> {
315+
let def_id = self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID);
315316
let name = format!("T{}", index);
316-
self.infcx.tcx.mk_ty_param(index, Symbol::intern(&name).as_interned_str())
317+
ty::ParamTy::new(index, def_id, Symbol::intern(&name)).to_ty(self.infcx.tcx)
317318
}
318319

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

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnconditionalRecursion {
10761076
// A trait method, from any number of possible sources.
10771077
// Attempt to select a concrete impl before checking.
10781078
ty::TraitContainer(trait_def_id) => {
1079-
let trait_self_ty = tcx.mk_self_type();
1079+
let trait_self_ty = tcx.mk_self_type(trait_def_id);
10801080
let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, callee_substs);
10811081
let trait_ref = ty::Binder::bind(trait_ref);
10821082
let span = tcx.hir.span(expr_id);

src/librustc_typeck/astconv.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
597597
let default_needs_object_self = |param: &ty::GenericParamDef| {
598598
if let GenericParamDefKind::Type { has_default, .. } = param.kind {
599599
if is_object && has_default {
600-
let trait_self_ty = tcx.mk_self_type();
600+
let trait_self_ty = tcx.mk_self_type(def_id);
601601
let default = tcx.at(span).type_of(param.def_id);
602602
if default.walk().any(|ty| ty == trait_self_ty) {
603603
// There is no suitable inference default for a type parameter
@@ -1412,7 +1412,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
14121412
let item_def_id = tcx.hir.local_def_id(item_id);
14131413
let generics = tcx.generics_of(item_def_id);
14141414
let index = generics.param_def_id_to_index[&tcx.hir.local_def_id(node_id)];
1415-
tcx.mk_ty_param(index, tcx.hir.name(node_id).as_interned_str())
1415+
ty::ParamTy::new(index, did, tcx.hir.name(node_id)).to_ty(tcx)
14161416
}
14171417
Def::SelfTy(_, Some(def_id)) => {
14181418
// Self in impl (we know the concrete type).
@@ -1422,11 +1422,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
14221422

14231423
tcx.at(span).type_of(def_id)
14241424
}
1425-
Def::SelfTy(Some(_), None) => {
1425+
Def::SelfTy(Some(trait_def_id), None) => {
14261426
// Self in trait.
14271427
assert_eq!(opt_self_ty, None);
14281428
self.prohibit_generics(&path.segments);
1429-
tcx.mk_self_type()
1429+
tcx.mk_self_type(trait_def_id)
14301430
}
14311431
Def::AssociatedTy(def_id) => {
14321432
self.prohibit_generics(&path.segments[..path.segments.len()-2]);
@@ -1572,7 +1572,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
15721572
GenericParamDefKind::Lifetime => {
15731573
tcx.types.re_static.into()
15741574
}
1575-
_ => tcx.mk_param_from_def(param)
1575+
_ => tcx.mk_param(param)
15761576
}
15771577
}
15781578
});

src/librustc_typeck/check/compare_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ fn compare_self_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
512512
let self_string = |method: &ty::AssociatedItem| {
513513
let untransformed_self_ty = match method.container {
514514
ty::ImplContainer(_) => impl_trait_ref.self_ty(),
515-
ty::TraitContainer(_) => tcx.mk_self_type()
515+
ty::TraitContainer(def_id) => tcx.mk_self_type(def_id),
516516
};
517517
let self_arg_ty = *tcx.fn_sig(method.def_id).input(0).skip_binder();
518518
let param_env = ty::ParamEnv::reveal_all();

src/librustc_typeck/check/intrinsic.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use require_same_types;
1919

2020
use rustc_target::spec::abi::Abi;
2121
use syntax::ast;
22-
use syntax::symbol::Symbol;
2322
use syntax_pos::Span;
2423

2524
use rustc::hir;
@@ -76,7 +75,9 @@ fn equate_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7675
/// and in libcore/intrinsics.rs
7776
pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7877
it: &hir::ForeignItem) {
79-
let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)).as_interned_str());
78+
let def_id = tcx.hir.local_def_id(it.id);
79+
let generics = tcx.generics_of(def_id);
80+
let param = |n| tcx.mk_ty_param(&generics.params[n]);
8081
let name = it.name.as_str();
8182
let (n_tps, inputs, output) = if name.starts_with("atomic_") {
8283
let split : Vec<&str> = name.split('_').collect();
@@ -335,13 +336,11 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
335336
/// Type-check `extern "platform-intrinsic" { ... }` functions.
336337
pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
337338
it: &hir::ForeignItem) {
338-
let param = |n| {
339-
let name = Symbol::intern(&format!("P{}", n)).as_interned_str();
340-
tcx.mk_ty_param(n, name)
341-
};
342-
343339
let def_id = tcx.hir.local_def_id(it.id);
344-
let i_n_tps = tcx.generics_of(def_id).own_counts().types;
340+
let generics = tcx.generics_of(def_id);
341+
let param = |n| tcx.mk_ty_param(&generics.params[n]);
342+
343+
let i_n_tps = generics.own_counts().types;
345344
let name = it.name.as_str();
346345

347346
let (n_tps, inputs, output) = match &*name {

src/librustc_typeck/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn check_associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
183183
let item = fcx.tcx.associated_item(fcx.tcx.hir.local_def_id(item_id));
184184

185185
let (mut implied_bounds, self_ty) = match item.container {
186-
ty::TraitContainer(_) => (vec![], fcx.tcx.mk_self_type()),
186+
ty::TraitContainer(def_id) => (vec![], fcx.tcx.mk_self_type(def_id)),
187187
ty::ImplContainer(def_id) => (fcx.impl_implied_bounds(def_id, span),
188188
fcx.tcx.type_of(def_id))
189189
};
@@ -431,7 +431,7 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(
431431
match param.kind {
432432
GenericParamDefKind::Lifetime => {
433433
// All regions are identity.
434-
fcx.tcx.mk_param_from_def(param)
434+
fcx.tcx.mk_param(param)
435435
}
436436
GenericParamDefKind::Type {..} => {
437437
// If the param has a default,

src/librustc_typeck/check/writeback.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
433433
if subst == ty {
434434
// found it in the substitution list, replace with the
435435
// parameter from the existential type
436-
return self.tcx()
437-
.global_tcx()
438-
.mk_ty_param(param.index, param.name);
436+
return self.tcx().global_tcx().mk_ty_param(param);
439437
}
440438
}
441439
}

src/librustc_typeck/collect.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn type_param_predicates<'a, 'tcx>(
249249
let param_owner_def_id = tcx.hir.local_def_id(param_owner);
250250
let generics = tcx.generics_of(param_owner_def_id);
251251
let index = generics.param_def_id_to_index[&def_id];
252-
let ty = tcx.mk_ty_param(index, tcx.hir.ty_param_name(param_id).as_interned_str());
252+
let ty = ty::ParamTy::new(index, def_id, tcx.hir.ty_param_name(param_id)).to_ty(tcx);
253253

254254
// Don't look for bounds where the type parameter isn't in scope.
255255
let parent = if item_def_id == param_owner_def_id {
@@ -683,7 +683,7 @@ fn super_predicates_of<'a, 'tcx>(
683683
let icx = ItemCtxt::new(tcx, trait_def_id);
684684

685685
// Convert the bounds that follow the colon, e.g. `Bar+Zed` in `trait Foo : Bar+Zed`.
686-
let self_param_ty = tcx.mk_self_type();
686+
let self_param_ty = tcx.mk_self_type(trait_def_id);
687687
let superbounds1 = compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item.span);
688688

689689
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
@@ -1773,8 +1773,9 @@ fn explicit_predicates_of<'a, 'tcx>(
17731773
for param in &ast_generics.params {
17741774
match param.kind {
17751775
GenericParamKind::Type { .. } => {
1776-
let name = param.name.ident().as_interned_str();
1777-
let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
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);
17781779
index += 1;
17791780

17801781
let sized = SizedByDefault::Yes;

0 commit comments

Comments
 (0)