Skip to content

Commit ed711b6

Browse files
committed
rustc_typeck: support functions in variance computation.
1 parent 8e0aa50 commit ed711b6

File tree

9 files changed

+314
-502
lines changed

9 files changed

+314
-502
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
@@ -516,7 +516,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
516516

517517
ty: Some(self.encode_item_type(def_id)),
518518
inherent_impls: LazySeq::empty(),
519-
variances: LazySeq::empty(),
519+
variances: if variant.ctor_kind == CtorKind::Fn {
520+
self.encode_variances_of(def_id)
521+
} else {
522+
LazySeq::empty()
523+
},
520524
generics: Some(self.encode_generics(def_id)),
521525
predicates: Some(self.encode_predicates(def_id)),
522526

@@ -644,7 +648,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
644648

645649
ty: Some(self.encode_item_type(def_id)),
646650
inherent_impls: LazySeq::empty(),
647-
variances: LazySeq::empty(),
651+
variances: if variant.ctor_kind == CtorKind::Fn {
652+
self.encode_variances_of(def_id)
653+
} else {
654+
LazySeq::empty()
655+
},
648656
generics: Some(self.encode_generics(def_id)),
649657
predicates: Some(self.encode_predicates(def_id)),
650658

@@ -736,7 +744,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
736744
}
737745
},
738746
inherent_impls: LazySeq::empty(),
739-
variances: LazySeq::empty(),
747+
variances: if trait_item.kind == ty::AssociatedKind::Method {
748+
self.encode_variances_of(def_id)
749+
} else {
750+
LazySeq::empty()
751+
},
740752
generics: Some(self.encode_generics(def_id)),
741753
predicates: Some(self.encode_predicates(def_id)),
742754

@@ -813,7 +825,11 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
813825

814826
ty: Some(self.encode_item_type(def_id)),
815827
inherent_impls: LazySeq::empty(),
816-
variances: LazySeq::empty(),
828+
variances: if impl_item.kind == ty::AssociatedKind::Method {
829+
self.encode_variances_of(def_id)
830+
} else {
831+
LazySeq::empty()
832+
},
817833
generics: Some(self.encode_generics(def_id)),
818834
predicates: Some(self.encode_predicates(def_id)),
819835

@@ -1047,7 +1063,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
10471063
hir::ItemEnum(..) |
10481064
hir::ItemStruct(..) |
10491065
hir::ItemUnion(..) |
1050-
hir::ItemTrait(..) => self.encode_variances_of(def_id),
1066+
hir::ItemFn(..) => self.encode_variances_of(def_id),
10511067
_ => LazySeq::empty(),
10521068
},
10531069
generics: match item.node {
@@ -1392,7 +1408,10 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13921408

13931409
ty: Some(self.encode_item_type(def_id)),
13941410
inherent_impls: LazySeq::empty(),
1395-
variances: LazySeq::empty(),
1411+
variances: match nitem.node {
1412+
hir::ForeignItemFn(..) => self.encode_variances_of(def_id),
1413+
_ => LazySeq::empty(),
1414+
},
13961415
generics: Some(self.encode_generics(def_id)),
13971416
predicates: Some(self.encode_predicates(def_id)),
13981417

0 commit comments

Comments
 (0)