Skip to content

Commit b03ed42

Browse files
authored
Rollup merge of rust-lang#42297 - tschottdorf:proj-ty, r=nikomatsakis
Upgrade ProjectionTy's Name to a DefId Part of rust-lang#42171, in preparation for downgrading the contained `TraitRef` to only its `substs`. Some inline questions in the diff. Look for `FIXME(tschottdorf)`. These comments should be addressed before merging.
2 parents ae75dbf + e5e664f commit b03ed42

File tree

15 files changed

+91
-59
lines changed

15 files changed

+91
-59
lines changed

src/librustc/ich/impls_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a, 'tcx, A, B> HashStable<StableHashingContext<'a, 'tcx>> for ty::Outlives
181181
}
182182

183183
impl_stable_hash_for!(struct ty::ProjectionPredicate<'tcx> { projection_ty, ty });
184-
impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { trait_ref, item_name });
184+
impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { trait_ref, item_def_id });
185185

186186

187187
impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Predicate<'tcx> {

src/librustc/infer/region_inference/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,8 @@ impl<'a, 'gcx, 'tcx> GenericKind<'tcx> {
15421542
pub fn to_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
15431543
match *self {
15441544
GenericKind::Param(ref p) => p.to_ty(tcx),
1545-
GenericKind::Projection(ref p) => tcx.mk_projection(p.trait_ref.clone(), p.item_name),
1545+
GenericKind::Projection(ref p) => tcx.mk_projection(
1546+
p.trait_ref.clone(), p.item_name(tcx)),
15461547
}
15471548
}
15481549
}

src/librustc/traits/project.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ pub fn normalize_projection_type<'a, 'b, 'gcx, 'tcx>(
355355

356356
let tcx = selcx.infcx().tcx;
357357
let def_id = tcx.associated_items(projection_ty.trait_ref.def_id).find(|i|
358-
i.name == projection_ty.item_name && i.kind == ty::AssociatedKind::Type
358+
i.name == projection_ty.item_name(tcx) && i.kind == ty::AssociatedKind::Type
359359
).map(|i| i.def_id).unwrap();
360360
let ty_var = selcx.infcx().next_ty_var(
361361
TypeVariableOrigin::NormalizeProjectionType(tcx.def_span(def_id)));
@@ -436,7 +436,7 @@ fn opt_normalize_projection_type<'a, 'b, 'gcx, 'tcx>(
436436
//
437437
// ```
438438
// let ty = selcx.tcx().mk_projection(projection_ty.trait_ref,
439-
// projection_ty.item_name);
439+
// projection_ty.item_name(tcx);
440440
// return Some(NormalizedTy { value: v, obligations: vec![] });
441441
// ```
442442

@@ -574,7 +574,7 @@ fn normalize_to_error<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 'tc
574574
predicate: trait_ref.to_predicate() };
575575
let tcx = selcx.infcx().tcx;
576576
let def_id = tcx.associated_items(projection_ty.trait_ref.def_id).find(|i|
577-
i.name == projection_ty.item_name && i.kind == ty::AssociatedKind::Type
577+
i.name == projection_ty.item_name(tcx) && i.kind == ty::AssociatedKind::Type
578578
).map(|i| i.def_id).unwrap();
579579
let new_value = selcx.infcx().next_ty_var(
580580
TypeVariableOrigin::NormalizeProjectionType(tcx.def_span(def_id)));
@@ -729,7 +729,7 @@ fn project_type<'cx, 'gcx, 'tcx>(
729729
Ok(ProjectedTy::NoProgress(
730730
selcx.tcx().mk_projection(
731731
obligation.predicate.trait_ref.clone(),
732-
obligation.predicate.item_name)))
732+
obligation.predicate.item_name(selcx.tcx()))))
733733
}
734734
}
735735
}
@@ -815,7 +815,8 @@ fn assemble_candidates_from_predicates<'cx, 'gcx, 'tcx, I>(
815815
predicate);
816816
match predicate {
817817
ty::Predicate::Projection(ref data) => {
818-
let same_name = data.item_name() == obligation.predicate.item_name;
818+
let tcx = selcx.tcx();
819+
let same_name = data.item_name(tcx) == obligation.predicate.item_name(tcx);
819820

820821
let is_match = same_name && infcx.probe(|_| {
821822
let data_poly_trait_ref =
@@ -902,7 +903,7 @@ fn assemble_candidates_from_impls<'cx, 'gcx, 'tcx>(
902903
// type.
903904
let node_item = assoc_ty_def(selcx,
904905
impl_data.impl_def_id,
905-
obligation.predicate.item_name);
906+
obligation.predicate.item_name(selcx.tcx()));
906907

907908
let is_default = if node_item.node.is_from_trait() {
908909
// If true, the impl inherited a `type Foo = Bar`
@@ -1075,9 +1076,10 @@ fn confirm_object_candidate<'cx, 'gcx, 'tcx>(
10751076

10761077
// select only those projections that are actually projecting an
10771078
// item with the correct name
1079+
let tcx = selcx.tcx();
10781080
let env_predicates = env_predicates.filter_map(|p| match p {
10791081
ty::Predicate::Projection(data) =>
1080-
if data.item_name() == obligation.predicate.item_name {
1082+
if data.item_name(tcx) == obligation.predicate.item_name(tcx) {
10811083
Some(data)
10821084
} else {
10831085
None
@@ -1180,10 +1182,11 @@ fn confirm_callable_candidate<'cx, 'gcx, 'tcx>(
11801182
flag);
11811183

11821184
let predicate = ty::Binder(ty::ProjectionPredicate { // (1) recreate binder here
1183-
projection_ty: ty::ProjectionTy {
1184-
trait_ref: trait_ref,
1185-
item_name: Symbol::intern(FN_OUTPUT_NAME),
1186-
},
1185+
projection_ty: ty::ProjectionTy::from_ref_and_name(
1186+
tcx,
1187+
trait_ref,
1188+
Symbol::intern(FN_OUTPUT_NAME),
1189+
),
11871190
ty: ret_type
11881191
});
11891192

@@ -1228,7 +1231,7 @@ fn confirm_impl_candidate<'cx, 'gcx, 'tcx>(
12281231
let VtableImplData { substs, nested, impl_def_id } = impl_vtable;
12291232

12301233
let tcx = selcx.tcx();
1231-
let assoc_ty = assoc_ty_def(selcx, impl_def_id, obligation.predicate.item_name);
1234+
let assoc_ty = assoc_ty_def(selcx, impl_def_id, obligation.predicate.item_name(tcx));
12321235

12331236
let ty = if !assoc_ty.item.defaultness.has_value() {
12341237
// This means that the impl is missing a definition for the

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13231323
item_name: Name)
13241324
-> Ty<'tcx> {
13251325
// take a copy of substs so that we own the vectors inside
1326-
let inner = ProjectionTy { trait_ref: trait_ref, item_name: item_name };
1326+
let inner = ProjectionTy::from_ref_and_name(self, trait_ref, item_name);
13271327
self.mk_ty(TyProjection(inner))
13281328
}
13291329

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,8 @@ pub struct ProjectionPredicate<'tcx> {
10511051
pub type PolyProjectionPredicate<'tcx> = Binder<ProjectionPredicate<'tcx>>;
10521052

10531053
impl<'tcx> PolyProjectionPredicate<'tcx> {
1054-
pub fn item_name(&self) -> Name {
1055-
self.0.projection_ty.item_name // safe to skip the binder to access a name
1054+
pub fn item_name(&self, tcx: TyCtxt) -> Name {
1055+
self.0.projection_ty.item_name(tcx) // safe to skip the binder to access a name
10561056
}
10571057
}
10581058

src/librustc/ty/relate.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,13 @@ impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
225225
-> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
226226
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
227227
{
228-
if a.item_name != b.item_name {
228+
let tcx = relation.tcx();
229+
if a.item_name(tcx) != b.item_name(tcx) {
229230
Err(TypeError::ProjectionNameMismatched(
230-
expected_found(relation, &a.item_name, &b.item_name)))
231+
expected_found(relation, &a.item_name(tcx), &b.item_name(tcx))))
231232
} else {
232233
let trait_ref = relation.relate(&a.trait_ref, &b.trait_ref)?;
233-
Ok(ty::ProjectionTy { trait_ref: trait_ref, item_name: a.item_name })
234+
Ok(ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, a.item_name(tcx)))
234235
}
235236
}
236237
}
@@ -457,7 +458,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
457458
(&ty::TyProjection(ref a_data), &ty::TyProjection(ref b_data)) =>
458459
{
459460
let projection_ty = relation.relate(a_data, b_data)?;
460-
Ok(tcx.mk_projection(projection_ty.trait_ref, projection_ty.item_name))
461+
Ok(tcx.mk_projection(projection_ty.trait_ref, projection_ty.item_name(tcx)))
461462
}
462463

463464
(&ty::TyAnon(a_def_id, a_substs), &ty::TyAnon(b_def_id, b_substs))

src/librustc/ty/structural_impls.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
135135
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
136136
-> Option<ty::ProjectionTy<'tcx>> {
137137
tcx.lift(&self.trait_ref).map(|trait_ref| {
138-
ty::ProjectionTy {
139-
trait_ref: trait_ref,
140-
item_name: self.item_name
141-
}
138+
ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, self.item_name(tcx))
142139
})
143140
}
144141
}
@@ -771,7 +768,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
771768
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
772769
ty::ProjectionTy {
773770
trait_ref: self.trait_ref.fold_with(folder),
774-
item_name: self.item_name,
771+
item_def_id: self.item_def_id,
775772
}
776773
}
777774

src/librustc/ty/sty.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,34 @@ pub struct ProjectionTy<'tcx> {
556556
/// The trait reference `T as Trait<..>`.
557557
pub trait_ref: ty::TraitRef<'tcx>,
558558

559-
/// The name `N` of the associated type.
560-
pub item_name: Name,
559+
/// The DefId of the TraitItem for the associated type N.
560+
///
561+
/// Note that this is not the DefId of the TraitRef containing this
562+
/// associated type, which is in tcx.associated_item(item_def_id).container.
563+
pub item_def_id: DefId,
561564
}
565+
566+
impl<'a, 'tcx> ProjectionTy<'tcx> {
567+
/// Construct a ProjectionTy by searching the trait from trait_ref for the
568+
/// associated item named item_name.
569+
pub fn from_ref_and_name(
570+
tcx: TyCtxt, trait_ref: ty::TraitRef<'tcx>, item_name: Name
571+
) -> ProjectionTy<'tcx> {
572+
let item_def_id = tcx.associated_items(trait_ref.def_id).find(
573+
|item| item.name == item_name).unwrap().def_id;
574+
575+
ProjectionTy {
576+
trait_ref: trait_ref,
577+
item_def_id: item_def_id,
578+
}
579+
}
580+
581+
pub fn item_name(self, tcx: TyCtxt) -> Name {
582+
tcx.associated_item(self.item_def_id).name
583+
}
584+
}
585+
586+
562587
/// Signature of a function type, which I have arbitrarily
563588
/// decided to use to refer to the input/output types.
564589
///
@@ -871,10 +896,10 @@ impl<'a, 'tcx, 'gcx> ExistentialProjection<'tcx> {
871896
assert!(!self_ty.has_escaping_regions());
872897

873898
ty::ProjectionPredicate {
874-
projection_ty: ty::ProjectionTy {
875-
trait_ref: self.trait_ref.with_self_ty(tcx, self_ty),
876-
item_name: self.item_name,
877-
},
899+
projection_ty: ty::ProjectionTy::from_ref_and_name(
900+
tcx,
901+
self.trait_ref.with_self_ty(tcx, self_ty),
902+
self.item_name),
878903
ty: self.ty,
879904
}
880905
}

src/librustc/ty/util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,7 @@ impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
691691
self.hash(p.name.as_str());
692692
}
693693
TyProjection(ref data) => {
694-
self.def_id(data.trait_ref.def_id);
695-
self.hash(data.item_name.as_str());
694+
self.def_id(data.item_def_id);
696695
}
697696
TyNever |
698697
TyBool |

src/librustc/util/ppaux.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,11 @@ pub fn parameterized(f: &mut fmt::Formatter,
216216

217217
for projection in projections {
218218
start_or_continue(f, "<", ", ")?;
219-
write!(f, "{}={}",
220-
projection.projection_ty.item_name,
221-
projection.ty)?;
219+
ty::tls::with(|tcx|
220+
write!(f, "{}={}",
221+
projection.projection_ty.item_name(tcx),
222+
projection.ty)
223+
)?;
222224
}
223225

224226
start_or_continue(f, "", ">")?;
@@ -929,9 +931,10 @@ impl<'tcx> fmt::Display for ty::ProjectionPredicate<'tcx> {
929931

930932
impl<'tcx> fmt::Display for ty::ProjectionTy<'tcx> {
931933
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
934+
let item_name = ty::tls::with(|tcx| self.item_name(tcx));
932935
write!(f, "{:?}::{}",
933936
self.trait_ref,
934-
self.item_name)
937+
item_name)
935938
}
936939
}
937940

src/librustc_save_analysis/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
618618
if let ty::TyProjection(proj) = ty.sty {
619619
for item in self.tcx.associated_items(proj.trait_ref.def_id) {
620620
if item.kind == ty::AssociatedKind::Type {
621-
if item.name == proj.item_name {
621+
if item.name == proj.item_name(self.tcx) {
622622
return Def::AssociatedTy(item.def_id);
623623
}
624624
}

src/librustc_typeck/astconv.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
553553
if self.trait_defines_associated_type_named(trait_ref.def_id(), binding.item_name) {
554554
return Ok(trait_ref.map_bound(|trait_ref| {
555555
ty::ProjectionPredicate {
556-
projection_ty: ty::ProjectionTy {
557-
trait_ref: trait_ref,
558-
item_name: binding.item_name,
559-
},
556+
projection_ty: ty::ProjectionTy::from_ref_and_name(
557+
tcx,
558+
trait_ref,
559+
binding.item_name,
560+
),
560561
ty: binding.ty,
561562
}
562563
}));
@@ -575,10 +576,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
575576

576577
Ok(candidate.map_bound(|trait_ref| {
577578
ty::ProjectionPredicate {
578-
projection_ty: ty::ProjectionTy {
579-
trait_ref: trait_ref,
580-
item_name: binding.item_name,
581-
},
579+
projection_ty: ty::ProjectionTy::from_ref_and_name(
580+
tcx,
581+
trait_ref,
582+
binding.item_name,
583+
),
582584
ty: binding.ty,
583585
}
584586
}))
@@ -652,7 +654,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
652654
let p = b.projection_ty;
653655
ty::ExistentialProjection {
654656
trait_ref: self.trait_ref_to_existential(p.trait_ref),
655-
item_name: p.item_name,
657+
item_name: p.item_name(tcx),
656658
ty: b.ty
657659
}
658660
})
@@ -679,7 +681,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
679681

680682
for projection_bound in &projection_bounds {
681683
let pair = (projection_bound.0.projection_ty.trait_ref.def_id,
682-
projection_bound.0.projection_ty.item_name);
684+
projection_bound.0.projection_ty.item_name(tcx));
683685
associated_types.remove(&pair);
684686
}
685687

src/librustc_typeck/check/autoderef.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
124124
}
125125

126126
let normalized = traits::normalize_projection_type(&mut selcx,
127-
ty::ProjectionTy {
128-
trait_ref: trait_ref,
129-
item_name: Symbol::intern("Target"),
130-
},
127+
ty::ProjectionTy::from_ref_and_name(
128+
tcx,
129+
trait_ref,
130+
Symbol::intern("Target"),
131+
),
131132
cause,
132133
0);
133134

src/librustc_typeck/check/regionck.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,8 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
16511651
declared_bounds, projection_ty);
16521652

16531653
// see the extensive comment in projection_must_outlive
1654-
1655-
let ty = self.tcx.mk_projection(projection_ty.trait_ref, projection_ty.item_name);
1654+
let item_name = projection_ty.item_name(self.tcx);
1655+
let ty = self.tcx.mk_projection(projection_ty.trait_ref, item_name);
16561656
let recursive_bound = self.recursive_type_bound(span, ty);
16571657

16581658
VerifyBound::AnyRegion(declared_bounds).or(recursive_bound)
@@ -1718,9 +1718,9 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
17181718
{
17191719
debug!("projection_bounds(projection_ty={:?})",
17201720
projection_ty);
1721-
1721+
let item_name = projection_ty.item_name(self.tcx);
17221722
let ty = self.tcx.mk_projection(projection_ty.trait_ref.clone(),
1723-
projection_ty.item_name);
1723+
item_name);
17241724

17251725
// Say we have a projection `<T as SomeTrait<'a>>::SomeType`. We are interested
17261726
// in looking for a trait definition like:
@@ -1758,7 +1758,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
17581758
let (outlives, _) =
17591759
self.replace_late_bound_regions_with_fresh_var(
17601760
span,
1761-
infer::AssocTypeProjection(projection_ty.item_name),
1761+
infer::AssocTypeProjection(projection_ty.item_name(self.tcx)),
17621762
&outlives);
17631763

17641764
debug!("projection_bounds: outlives={:?} (3)",

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
955955
}
956956
};
957957
Type::QPath {
958-
name: self.item_name.clean(cx),
958+
name: self.item_name(cx.tcx).clean(cx),
959959
self_type: box self.trait_ref.self_ty().clean(cx),
960960
trait_: box trait_
961961
}

0 commit comments

Comments
 (0)