Skip to content

Commit a5caa98

Browse files
authored
Rollup merge of #107339 - aliemjay:covariant, r=lcnr
internally change regions to be covariant Surprisingly, we consider the reference type `&'a T` to be contravaraint in its lifetime parameter. This is confusing and conflicts with the documentation we have in the reference, rustnomicon, and rustc-dev-guide. This also arguably not the correct use of terminology since we can use `&'static u8` in a place where `&' a u8` is expected, this implies that `&'static u8 <: &' a u8` and consequently `'static <: ' a`, hence covariance. Because of this, when relating two types, we used to switch the argument positions in a confusing way: `Subtype(&'a u8 <: &'b u8) => Subtype('b <: 'a) => Outlives('a: 'b) => RegionSubRegion('b <= 'a)` The reason for the current behavior is probably that we wanted `Subtype('b <: 'a)` and `RegionSubRegion('b <= 'a)` to be equivalent, but I don' t think this is a good reason since these relations are sufficiently different in that the first is a relation in the subtyping lattice and is intrinsic to the type-systems, while the the second relation is an implementation detail of regionck. This PR changes this behavior to use covariance, so.. `Subtype(&'a u8 <: &'b u8) => Subtype('a <: 'b) => Outlives('a: 'b) => RegionSubRegion('b <= 'a) ` Resolves #103676 r? `@lcnr`
2 parents fa2cd94 + 43cb610 commit a5caa98

18 files changed

+43
-52
lines changed

compiler/rustc_hir_analysis/src/variance/constraints.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
225225
}
226226

227227
ty::Ref(region, ty, mutbl) => {
228-
let contra = self.contravariant(variance);
229-
self.add_constraints_from_region(current, region, contra);
228+
self.add_constraints_from_region(current, region, variance);
230229
self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
231230
}
232231

@@ -258,9 +257,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
258257
}
259258

260259
ty::Dynamic(data, r, _) => {
261-
// The type `Foo<T+'a>` is contravariant w/r/t `'a`:
262-
let contra = self.contravariant(variance);
263-
self.add_constraints_from_region(current, r, contra);
260+
// The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
261+
self.add_constraints_from_region(current, r, variance);
264262

265263
if let Some(poly_trait_ref) = data.principal() {
266264
self.add_constraints_from_invariant_substs(

compiler/rustc_infer/src/infer/glb.rs

+2-1
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

+2-1
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

+4-4
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

+2-1
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

+2-12
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
}
@@ -497,12 +492,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
497492
}
498493

499494
(&ty::Ref(a_r, a_ty, a_mutbl), &ty::Ref(b_r, b_ty, b_mutbl)) => {
500-
let r = relation.relate_with_variance(
501-
ty::Contravariant,
502-
ty::VarianceDiagInfo::default(),
503-
a_r,
504-
b_r,
505-
)?;
495+
let r = relation.relate(a_r, b_r)?;
506496
let a_mt = ty::TypeAndMut { ty: a_ty, mutbl: a_mutbl };
507497
let b_mt = ty::TypeAndMut { ty: b_ty, mutbl: b_mutbl };
508498
let mt = relate_type_and_mut(relation, a_mt, b_mt, a)?;

tests/ui/error-codes/E0208.rs

+1-1
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

+1-1
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

+1-1
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

+1-1
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>> {

tests/ui/variance/variance-regions-direct.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Regions that just appear in normal spots are contravariant:
77

88
#[rustc_variance]
9-
struct Test2<'a, 'b, 'c> { //~ ERROR [-, -, -]
9+
struct Test2<'a, 'b, 'c> { //~ ERROR [+, +, +]
1010
x: &'a isize,
1111
y: &'b [isize],
1212
c: &'c str
@@ -15,7 +15,7 @@ struct Test2<'a, 'b, 'c> { //~ ERROR [-, -, -]
1515
// Those same annotations in function arguments become covariant:
1616

1717
#[rustc_variance]
18-
struct Test3<'a, 'b, 'c> { //~ ERROR [+, +, +]
18+
struct Test3<'a, 'b, 'c> { //~ ERROR [-, -, -]
1919
x: extern "Rust" fn(&'a isize),
2020
y: extern "Rust" fn(&'b [isize]),
2121
c: extern "Rust" fn(&'c str),
@@ -24,15 +24,15 @@ struct Test3<'a, 'b, 'c> { //~ ERROR [+, +, +]
2424
// Mutability induces invariance:
2525

2626
#[rustc_variance]
27-
struct Test4<'a, 'b:'a> { //~ ERROR [-, o]
27+
struct Test4<'a, 'b:'a> { //~ ERROR [+, o]
2828
x: &'a mut &'b isize,
2929
}
3030

3131
// Mutability induces invariance, even when in a
3232
// contravariant context:
3333

3434
#[rustc_variance]
35-
struct Test5<'a, 'b:'a> { //~ ERROR [+, o]
35+
struct Test5<'a, 'b:'a> { //~ ERROR [-, o]
3636
x: extern "Rust" fn(&'a mut &'b isize),
3737
}
3838

@@ -42,7 +42,7 @@ struct Test5<'a, 'b:'a> { //~ ERROR [+, o]
4242
// argument list occurs in an invariant context.
4343

4444
#[rustc_variance]
45-
struct Test6<'a, 'b:'a> { //~ ERROR [-, o]
45+
struct Test6<'a, 'b:'a> { //~ ERROR [+, o]
4646
x: &'a mut extern "Rust" fn(&'b isize),
4747
}
4848

@@ -56,7 +56,7 @@ struct Test7<'a> { //~ ERROR [*]
5656
// Try enums too.
5757

5858
#[rustc_variance]
59-
enum Test8<'a, 'b, 'c:'b> { //~ ERROR [+, -, o]
59+
enum Test8<'a, 'b, 'c:'b> { //~ ERROR [-, +, o]
6060
Test8A(extern "Rust" fn(&'a isize)),
6161
Test8B(&'b [isize]),
6262
Test8C(&'b mut &'c str),

tests/ui/variance/variance-regions-direct.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
error: [-, -, -]
1+
error: [+, +, +]
22
--> $DIR/variance-regions-direct.rs:9:1
33
|
44
LL | struct Test2<'a, 'b, 'c> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: [+, +, +]
7+
error: [-, -, -]
88
--> $DIR/variance-regions-direct.rs:18:1
99
|
1010
LL | struct Test3<'a, 'b, 'c> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: [-, o]
13+
error: [+, o]
1414
--> $DIR/variance-regions-direct.rs:27:1
1515
|
1616
LL | struct Test4<'a, 'b:'a> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: [+, o]
19+
error: [-, o]
2020
--> $DIR/variance-regions-direct.rs:35:1
2121
|
2222
LL | struct Test5<'a, 'b:'a> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error: [-, o]
25+
error: [+, o]
2626
--> $DIR/variance-regions-direct.rs:45:1
2727
|
2828
LL | struct Test6<'a, 'b:'a> {
@@ -34,7 +34,7 @@ error: [*]
3434
LL | struct Test7<'a> {
3535
| ^^^^^^^^^^^^^^^^
3636

37-
error: [+, -, o]
37+
error: [-, +, o]
3838
--> $DIR/variance-regions-direct.rs:59:1
3939
|
4040
LL | enum Test8<'a, 'b, 'c:'b> {

tests/ui/variance/variance-regions-indirect.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
#![feature(rustc_attrs)]
66

77
#[rustc_variance]
8-
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [+, -, o, *]
8+
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [-, +, o, *]
99
Test8A(extern "Rust" fn(&'a isize)),
1010
Test8B(&'b [isize]),
1111
Test8C(&'b mut &'c str),
1212
}
1313

1414
#[rustc_variance]
15-
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR [*, o, -, +]
15+
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR [*, o, +, -]
1616
f: Base<'z, 'y, 'x, 'w>
1717
}
1818

@@ -22,12 +22,12 @@ struct Derived2<'a, 'b:'a, 'c> { //~ ERROR [o, o, *]
2222
}
2323

2424
#[rustc_variance] // Combine + and o to yield o (just pay attention to 'a here)
25-
struct Derived3<'a:'b, 'b, 'c> { //~ ERROR [o, -, *]
25+
struct Derived3<'a:'b, 'b, 'c> { //~ ERROR [o, +, *]
2626
f: Base<'a, 'b, 'a, 'c>
2727
}
2828

2929
#[rustc_variance] // Combine + and * to yield + (just pay attention to 'a here)
30-
struct Derived4<'a, 'b, 'c:'b> { //~ ERROR [+, -, o]
30+
struct Derived4<'a, 'b, 'c:'b> { //~ ERROR [-, +, o]
3131
f: Base<'a, 'b, 'c, 'a>
3232
}
3333

tests/ui/variance/variance-regions-indirect.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: [+, -, o, *]
1+
error: [-, +, o, *]
22
--> $DIR/variance-regions-indirect.rs:8:1
33
|
44
LL | enum Base<'a, 'b, 'c:'b, 'd> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: [*, o, -, +]
7+
error: [*, o, +, -]
88
--> $DIR/variance-regions-indirect.rs:15:1
99
|
1010
LL | struct Derived1<'w, 'x:'y, 'y, 'z> {
@@ -16,13 +16,13 @@ error: [o, o, *]
1616
LL | struct Derived2<'a, 'b:'a, 'c> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: [o, -, *]
19+
error: [o, +, *]
2020
--> $DIR/variance-regions-indirect.rs:25:1
2121
|
2222
LL | struct Derived3<'a:'b, 'b, 'c> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error: [+, -, o]
25+
error: [-, +, o]
2626
--> $DIR/variance-regions-indirect.rs:30:1
2727
|
2828
LL | struct Derived4<'a, 'b, 'c:'b> {

tests/ui/variance/variance-trait-object-bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::mem;
1111
trait T { fn foo(&self); }
1212

1313
#[rustc_variance]
14-
struct TOption<'a> { //~ ERROR [-]
14+
struct TOption<'a> { //~ ERROR [+]
1515
v: Option<Box<dyn T + 'a>>,
1616
}
1717

tests/ui/variance/variance-trait-object-bound.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: [-]
1+
error: [+]
22
--> $DIR/variance-trait-object-bound.rs:14:1
33
|
44
LL | struct TOption<'a> {

tests/ui/variance/variance-types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cell::Cell;
77
// not considered bivariant.
88

99
#[rustc_variance]
10-
struct InvariantMut<'a,A:'a,B:'a> { //~ ERROR [-, o, o]
10+
struct InvariantMut<'a,A:'a,B:'a> { //~ ERROR [+, o, o]
1111
t: &'a mut (A,B)
1212
}
1313

tests/ui/variance/variance-types.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: [-, o, o]
1+
error: [+, o, o]
22
--> $DIR/variance-types.rs:10:1
33
|
44
LL | struct InvariantMut<'a,A:'a,B:'a> {

0 commit comments

Comments
 (0)