Immutable and mutable? are covariant on their inner types
Whereas [mutable T] is invariant with respect to T, [T] and [mutable? T] are covariant with respect to T.
This commit is contained in:
parent
71a4a66135
commit
314c011d71
|
@ -1947,9 +1947,16 @@ mod unify {
|
||||||
variance: variance) ->
|
variance: variance) ->
|
||||||
option::t<(ast::mutability, variance)> {
|
option::t<(ast::mutability, variance)> {
|
||||||
|
|
||||||
// If you're unifying mutability then the thing inside
|
// If you're unifying on something mutable then we have to
|
||||||
// will be invariant on anything it contains
|
// be invariant on the inner type
|
||||||
let newvariance = variance_transform(variance, invariant);
|
let newvariance = alt expected {
|
||||||
|
ast::mut. {
|
||||||
|
variance_transform(variance, invariant)
|
||||||
|
}
|
||||||
|
_ {
|
||||||
|
variance_transform(variance, covariant)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if expected == actual { ret some((expected, newvariance)); }
|
if expected == actual { ret some((expected, newvariance)); }
|
||||||
if variance == covariant {
|
if variance == covariant {
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
// error-pattern: mismatched types
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let v = [[0]];
|
||||||
|
|
||||||
|
// This is ok because the outer vec is covariant with respect
|
||||||
|
// to the inner vec. If the outer vec was mutable then we
|
||||||
|
// couldn't do this.
|
||||||
|
fn f(&&v: [[mutable? int]]) {
|
||||||
|
}
|
||||||
|
|
||||||
|
f(v);
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
// error-pattern: mismatched types
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let v = [[0]];
|
||||||
|
|
||||||
|
// This is ok because the outer vec is covariant with respect
|
||||||
|
// to the inner vec. If the outer vec was mutable then we
|
||||||
|
// couldn't do this.
|
||||||
|
fn f(&&v: [mutable? [mutable? int]]) {
|
||||||
|
}
|
||||||
|
|
||||||
|
f(v);
|
||||||
|
}
|
Loading…
Reference in New Issue