Skip to content

Commit 1b0f0ad

Browse files
committed
Extract out mts into combine using tys_with_variance
1 parent bfac337 commit 1b0f0ad

File tree

6 files changed

+18
-100
lines changed

6 files changed

+18
-100
lines changed

src/librustc/middle/infer/bivariate.rs

-10
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ impl<'f, 'tcx> Combine<'tcx> for Bivariate<'f, 'tcx> {
7474
Ok(a)
7575
}
7676

77-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
78-
debug!("mts({} <: {})",
79-
a.repr(self.fields.infcx.tcx),
80-
b.repr(self.fields.infcx.tcx));
81-
82-
if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
83-
let t = try!(self.tys(a.ty, b.ty));
84-
Ok(ty::mt { mutbl: a.mutbl, ty: t })
85-
}
86-
8777
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
8878
if a != b {
8979
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))

src/librustc/middle/infer/combine.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,24 @@ pub trait Combine<'tcx> : Sized {
7474
fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields().clone()) }
7575
fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields().clone()) }
7676

77-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>>;
77+
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
78+
debug!("{}.mts({}, {})",
79+
self.tag(),
80+
a.repr(self.tcx()),
81+
b.repr(self.tcx()));
82+
83+
if a.mutbl != b.mutbl {
84+
Err(ty::terr_mutability)
85+
} else {
86+
let mutbl = a.mutbl;
87+
let variance = match mutbl {
88+
ast::MutImmutable => ty::Covariant,
89+
ast::MutMutable => ty::Invariant,
90+
};
91+
let ty = try!(self.tys_with_variance(variance, a.ty, b.ty));
92+
Ok(ty::mt {ty: ty, mutbl: mutbl})
93+
}
94+
}
7895

7996
fn tys_with_variance(&self, variance: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
8097
-> cres<'tcx, Ty<'tcx>>;

src/librustc/middle/infer/equate.rs

-9
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ impl<'f, 'tcx> Combine<'tcx> for Equate<'f, 'tcx> {
5454
Ok(a)
5555
}
5656

57-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
58-
debug!("mts({} <: {})",
59-
a.repr(self.fields.infcx.tcx),
60-
b.repr(self.fields.infcx.tcx));
61-
62-
if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
63-
let t = try!(self.tys(a.ty, b.ty));
64-
Ok(ty::mt { mutbl: a.mutbl, ty: t })
65-
}
6657

6758
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
6859
if a != b {

src/librustc/middle/infer/glb.rs

-30
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,6 @@ impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
5555
}
5656
}
5757

58-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
59-
let tcx = self.fields.infcx.tcx;
60-
61-
debug!("{}.mts({}, {})",
62-
self.tag(),
63-
mt_to_string(tcx, a),
64-
mt_to_string(tcx, b));
65-
66-
match (a.mutbl, b.mutbl) {
67-
// If one side or both is mut, then the GLB must use
68-
// the precise type from the mut side.
69-
(MutMutable, MutMutable) => {
70-
let t = try!(self.equate().tys(a.ty, b.ty));
71-
Ok(ty::mt {ty: t, mutbl: MutMutable})
72-
}
73-
74-
// If one side or both is immutable, we can use the GLB of
75-
// both sides but mutbl must be `MutImmutable`.
76-
(MutImmutable, MutImmutable) => {
77-
let t = try!(self.tys(a.ty, b.ty));
78-
Ok(ty::mt {ty: t, mutbl: MutImmutable})
79-
}
80-
81-
// There is no mutual subtype of these combinations.
82-
(MutMutable, MutImmutable) |
83-
(MutImmutable, MutMutable) => {
84-
Err(ty::terr_mutability)
85-
}
86-
}
87-
}
8858

8959
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
9060
match (a, b) {

src/librustc/middle/infer/lub.rs

-26
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,6 @@ impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
5555
}
5656
}
5757

58-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
59-
let tcx = self.tcx();
60-
61-
debug!("{}.mts({}, {})",
62-
self.tag(),
63-
mt_to_string(tcx, a),
64-
mt_to_string(tcx, b));
65-
66-
if a.mutbl != b.mutbl {
67-
return Err(ty::terr_mutability)
68-
}
69-
70-
let m = a.mutbl;
71-
match m {
72-
MutImmutable => {
73-
let t = try!(self.tys(a.ty, b.ty));
74-
Ok(ty::mt {ty: t, mutbl: m})
75-
}
76-
77-
MutMutable => {
78-
let t = try!(self.equate().tys(a.ty, b.ty));
79-
Ok(ty::mt {ty: t, mutbl: m})
80-
}
81-
}
82-
}
83-
8458
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
8559
match (a, b) {
8660
(Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),

src/librustc/middle/infer/sub.rs

-24
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,6 @@ impl<'f, 'tcx> Combine<'tcx> for Sub<'f, 'tcx> {
6666
Ok(a)
6767
}
6868

69-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
70-
debug!("mts({} <: {})",
71-
a.repr(self.tcx()),
72-
b.repr(self.tcx()));
73-
74-
if a.mutbl != b.mutbl {
75-
return Err(ty::terr_mutability);
76-
}
77-
78-
match b.mutbl {
79-
MutMutable => {
80-
// If supertype is mut, subtype must match exactly
81-
// (i.e., invariant if mut):
82-
try!(self.equate().tys(a.ty, b.ty));
83-
}
84-
MutImmutable => {
85-
// Otherwise we can be covariant:
86-
try!(self.tys(a.ty, b.ty));
87-
}
88-
}
89-
90-
Ok(*a) // return is meaningless in sub, just return *a
91-
}
92-
9369
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
9470
self.lub().unsafeties(a, b).compare(b, || {
9571
ty::terr_unsafety_mismatch(expected_found(self, a, b))

0 commit comments

Comments
 (0)