Skip to content

Commit a9d4069

Browse files
committed
rustc_typeck: support functions in variance computation.
1 parent 33ecf72 commit a9d4069

File tree

9 files changed

+317
-505
lines changed

9 files changed

+317
-505
lines changed

src/librustc/ty/relate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
291291
if a.def_id != b.def_id {
292292
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
293293
} else {
294-
let substs = relation.relate_item_substs(a.def_id, a.substs, b.substs)?;
294+
let substs = relate_substs(relation, None, a.substs, b.substs)?;
295295
Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
296296
}
297297
}
@@ -308,7 +308,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
308308
if a.def_id != b.def_id {
309309
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
310310
} else {
311-
let substs = relation.relate_item_substs(a.def_id, a.substs, b.substs)?;
311+
let substs = relate_substs(relation, None, a.substs, b.substs)?;
312312
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
313313
}
314314
}
@@ -443,7 +443,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
443443
(&ty::TyFnDef(a_def_id, a_substs), &ty::TyFnDef(b_def_id, b_substs))
444444
if a_def_id == b_def_id =>
445445
{
446-
let substs = relate_substs(relation, None, a_substs, b_substs)?;
446+
let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
447447
Ok(tcx.mk_fn_def(a_def_id, substs))
448448
}
449449

src/librustc_metadata/encoder.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
524524

525525
ty: Some(self.encode_item_type(def_id)),
526526
inherent_impls: LazySeq::empty(),
527-
variances: LazySeq::empty(),
527+
variances: if variant.ctor_kind == CtorKind::Fn {
528+
self.encode_variances_of(def_id)
529+
} else {
530+
LazySeq::empty()
531+
},
528532
generics: Some(self.encode_generics(def_id)),
529533
predicates: Some(self.encode_predicates(def_id)),
530534

@@ -652,7 +656,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
652656

653657
ty: Some(self.encode_item_type(def_id)),
654658
inherent_impls: LazySeq::empty(),
655-
variances: LazySeq::empty(),
659+
variances: if variant.ctor_kind == CtorKind::Fn {
660+
self.encode_variances_of(def_id)
661+
} else {
662+
LazySeq::empty()
663+
},
656664
generics: Some(self.encode_generics(def_id)),
657665
predicates: Some(self.encode_predicates(def_id)),
658666

@@ -744,7 +752,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
744752
}
745753
},
746754
inherent_impls: LazySeq::empty(),
747-
variances: LazySeq::empty(),
755+
variances: if trait_item.kind == ty::AssociatedKind::Method {
756+
self.encode_variances_of(def_id)
757+
} else {
758+
LazySeq::empty()
759+
},
748760
generics: Some(self.encode_generics(def_id)),
749761
predicates: Some(self.encode_predicates(def_id)),
750762

@@ -821,7 +833,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
821833

822834
ty: Some(self.encode_item_type(def_id)),
823835
inherent_impls: LazySeq::empty(),
824-
variances: LazySeq::empty(),
836+
variances: if impl_item.kind == ty::AssociatedKind::Method {
837+
self.encode_variances_of(def_id)
838+
} else {
839+
LazySeq::empty()
840+
},
825841
generics: Some(self.encode_generics(def_id)),
826842
predicates: Some(self.encode_predicates(def_id)),
827843

@@ -1055,7 +1071,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
10551071
hir::ItemEnum(..) |
10561072
hir::ItemStruct(..) |
10571073
hir::ItemUnion(..) |
1058-
hir::ItemTrait(..) => self.encode_variances_of(def_id),
1074+
hir::ItemFn(..) => self.encode_variances_of(def_id),
10591075
_ => LazySeq::empty(),
10601076
},
10611077
generics: match item.node {
@@ -1400,7 +1416,10 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
14001416

14011417
ty: Some(self.encode_item_type(def_id)),
14021418
inherent_impls: LazySeq::empty(),
1403-
variances: LazySeq::empty(),
1419+
variances: match nitem.node {
1420+
hir::ForeignItemFn(..) => self.encode_variances_of(def_id),
1421+
_ => LazySeq::empty(),
1422+
},
14041423
generics: Some(self.encode_generics(def_id)),
14051424
predicates: Some(self.encode_predicates(def_id)),
14061425

0 commit comments

Comments
 (0)