Skip to content

Commit 18ea55f

Browse files
committed
remove bivariance
There is one fishy part of these changes: when computing the LUB/GLB of a "bivariant" type parameter, I currently return the `a` value. Bivariant type parameters are only allowed in a very particular situation, where the type parameter is only used as an associated type output, like this: ```rust pub struct Foo<A, B> where A: Fn() -> B { data: A } ``` In principle, if one had `T=Foo<A, &'a u32>` and `U=Foo<A, &'b u32>` and (e.g.) `A: for<'a> Fn() -> &'a u32`, then I think that computing the LUB of `T` and `U` might do the wrong thing. Probably the right behavior is just to create a fresh type variable. However, that particular example would not compile (because the where-clause is illegal; `'a` does not appear in any input type). I was not able to make an example that *would* compile and demonstrate this shortcoming, and handling the LUB/GLB was mildly inconvenient, so I left it as is. I am considering whether to revisit this.
1 parent 4a0a0e9 commit 18ea55f

File tree

6 files changed

+4
-133
lines changed

6 files changed

+4
-133
lines changed

src/librustc/infer/bivariate.rs

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/librustc/infer/combine.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
// is also useful to track which value is the "expected" value in
3333
// terms of error reporting.
3434

35-
use super::bivariate::Bivariate;
3635
use super::equate::Equate;
3736
use super::glb::Glb;
3837
use super::lub::Lub;
@@ -159,10 +158,6 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
159158
Equate::new(self, a_is_expected)
160159
}
161160

162-
pub fn bivariate<'a>(&'a mut self, a_is_expected: bool) -> Bivariate<'a, 'infcx, 'gcx, 'tcx> {
163-
Bivariate::new(self, a_is_expected)
164-
}
165-
166161
pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> Sub<'a, 'infcx, 'gcx, 'tcx> {
167162
Sub::new(self, a_is_expected)
168163
}
@@ -251,7 +246,7 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
251246
// to associate causes/spans with each of the relations in
252247
// the stack to get this right.
253248
match dir {
254-
BiTo => self.bivariate(a_is_expected).relate(&a_ty, &b_ty),
249+
BiTo => Ok(a_ty),
255250
EqTo => self.equate(a_is_expected).relate(&a_ty, &b_ty),
256251
SubtypeOf => self.sub(a_is_expected).relate(&a_ty, &b_ty),
257252
SupertypeOf => self.sub(a_is_expected).relate_with_variance(

src/librustc/infer/glb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
4949
match variance {
5050
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
5151
ty::Covariant => self.relate(a, b),
52-
ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b),
52+
ty::Bivariant => Ok(a.clone()),
5353
ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b),
5454
}
5555
}

src/librustc/infer/lub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
4949
match variance {
5050
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
5151
ty::Covariant => self.relate(a, b),
52-
ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b),
52+
ty::Bivariant => Ok(a.clone()),
5353
ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
5454
}
5555
}

src/librustc/infer/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use self::region_inference::{RegionVarBindings, RegionSnapshot};
4848
use self::type_variable::TypeVariableOrigin;
4949
use self::unify_key::ToType;
5050

51-
mod bivariate;
5251
mod combine;
5352
mod equate;
5453
pub mod error_reporting;

src/librustc/infer/sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
6565
match variance {
6666
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
6767
ty::Covariant => self.relate(a, b),
68-
ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b),
68+
ty::Bivariant => Ok(a.clone()),
6969
ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
7070
}
7171
}

0 commit comments

Comments
 (0)