Skip to content

Commit 381187d

Browse files
committed
internally change regions to be covariant
1 parent c62665e commit 381187d

18 files changed

+43
-52
lines changed

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
221221
}
222222

223223
ty::Ref(region, ty, mutbl) => {
224-
let contra = self.contravariant(variance);
225-
self.add_constraints_from_region(current, region, contra);
224+
self.add_constraints_from_region(current, region, variance);
226225
self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
227226
}
228227

@@ -254,9 +253,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
254253
}
255254

256255
ty::Dynamic(data, r, _) => {
257-
// The type `Foo<T+'a>` is contravariant w/r/t `'a`:
258-
let contra = self.contravariant(variance);
259-
self.add_constraints_from_region(current, r, contra);
256+
// The type `Foo<T + 'a>` is covariant w/r/t `'a`:
257+
self.add_constraints_from_region(current, r, variance);
260258

261259
if let Some(poly_trait_ref) = data.principal() {
262260
self.add_constraints_from_invariant_substs(

compiler/rustc_infer/src/infer/glb.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
7979
debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
8080

8181
let origin = Subtype(Box::new(self.fields.trace.clone()));
82-
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().glb_regions(
82+
// GLB(&'static u8, &'a u8) == &RegionLUB('static, 'a) u8 == &'static u8
83+
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions(
8384
self.tcx(),
8485
origin,
8586
a,

compiler/rustc_infer/src/infer/lub.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
7979
debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
8080

8181
let origin = Subtype(Box::new(self.fields.trace.clone()));
82-
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().lub_regions(
82+
// LUB(&'static u8, &'a u8) == &RegionGLB('static, 'a) u8 == &'a u8
83+
Ok(self.fields.infcx.inner.borrow_mut().unwrap_region_constraints().glb_regions(
8384
self.tcx(),
8485
origin,
8586
a,

compiler/rustc_infer/src/infer/nll_relate/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,13 @@ where
663663
debug!(?v_b);
664664

665665
if self.ambient_covariance() {
666-
// Covariance: a <= b. Hence, `b: a`.
667-
self.push_outlives(v_b, v_a, self.ambient_variance_info);
666+
// Covariant: &'a u8 <: &'b u8. Hence, `'a: 'b`.
667+
self.push_outlives(v_a, v_b, self.ambient_variance_info);
668668
}
669669

670670
if self.ambient_contravariance() {
671-
// Contravariant: b <= a. Hence, `a: b`.
672-
self.push_outlives(v_a, v_b, self.ambient_variance_info);
671+
// Contravariant: &'b u8 <: &'a u8. Hence, `'b: 'a`.
672+
self.push_outlives(v_b, v_a, self.ambient_variance_info);
673673
}
674674

675675
Ok(a)

compiler/rustc_infer/src/infer/sub.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
191191
// from the "cause" field, we could perhaps give more tailored
192192
// error messages.
193193
let origin = SubregionOrigin::Subtype(Box::new(self.fields.trace.clone()));
194+
// Subtype(&'a u8, &'b u8) => Outlives('a: 'b) => SubRegion('b, 'a)
194195
self.fields
195196
.infcx
196197
.inner
197198
.borrow_mut()
198199
.unwrap_region_constraints()
199-
.make_subregion(origin, a, b);
200+
.make_subregion(origin, b, a);
200201

201202
Ok(a)
202203
}

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
443443
if a_repr == b_repr =>
444444
{
445445
let region_bound = relation.with_cause(Cause::ExistentialRegionBound, |relation| {
446-
relation.relate_with_variance(
447-
ty::Contravariant,
448-
ty::VarianceDiagInfo::default(),
449-
a_region,
450-
b_region,
451-
)
446+
relation.relate(a_region, b_region)
452447
})?;
453448
Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound, a_repr))
454449
}
@@ -487,12 +482,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
487482
}
488483

489484
(&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) => {
490-
let r = relation.relate_with_variance(
491-
ty::Contravariant,
492-
ty::VarianceDiagInfo::default(),
493-
a_r,
494-
b_r,
495-
)?;
485+
let r = relation.relate(a_r, b_r)?;
496486
let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
497487
let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
498488
let mt = relate_type_and_mut(relation, a_mt, b_mt, a)?;

tests/ui/error-codes/E0208.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(rustc_attrs)]
22

33
#[rustc_variance]
4-
struct Foo<'a, T> { //~ ERROR [-, o]
4+
struct Foo<'a, T> { //~ ERROR [+, o]
55
t: &'a mut T,
66
}
77

tests/ui/error-codes/E0208.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: [-, o]
1+
error: [+, o]
22
--> $DIR/E0208.rs:4:1
33
|
44
LL | struct Foo<'a, T> {

tests/ui/variance/variance-associated-types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait Trait<'a> {
1010
}
1111

1212
#[rustc_variance]
13-
struct Foo<'a, T : Trait<'a>> { //~ ERROR [-, +]
13+
struct Foo<'a, T : Trait<'a>> { //~ ERROR [+, +]
1414
field: (T, &'a ())
1515
}
1616

tests/ui/variance/variance-associated-types.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: [-, +]
1+
error: [+, +]
22
--> $DIR/variance-associated-types.rs:13:1
33
|
44
LL | struct Foo<'a, T : Trait<'a>> {

0 commit comments

Comments
 (0)