Skip to content

Commit 589108b

Browse files
committed
Test that HashMap, HashSet, and their iterators are properly covariant
1 parent 1639f2d commit 589108b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/libstd/collections/hash/map.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,20 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
16701670
}
16711671
}
16721672

1673+
#[allow(dead_code)]
1674+
fn assert_covariance() {
1675+
fn map_key<'new>(v: HashMap<&'static str, u8>) -> HashMap<&'new str, u8> { v }
1676+
fn map_val<'new>(v: HashMap<u8, &'static str>) -> HashMap<u8, &'new str> { v }
1677+
fn iter_key<'a, 'new>(v: Iter<'a, &'static str, u8>) -> Iter<'a, &'new str, u8> { v }
1678+
fn iter_val<'a, 'new>(v: Iter<'a, u8, &'static str>) -> Iter<'a, u8, &'new str> { v }
1679+
fn into_iter_key<'new>(v: IntoIter<&'static str, u8>) -> IntoIter<&'new str, u8> { v }
1680+
fn into_iter_val<'new>(v: IntoIter<u8, &'static str>) -> IntoIter<u8, &'new str> { v }
1681+
fn keys_key<'a, 'new>(v: Keys<'a, &'static str, u8>) -> Keys<'a, &'new str, u8> { v }
1682+
fn keys_val<'a, 'new>(v: Keys<'a, u8, &'static str>) -> Keys<'a, u8, &'new str> { v }
1683+
fn values_key<'a, 'new>(v: Values<'a, &'static str, u8>) -> Values<'a, &'new str, u8> { v }
1684+
fn values_val<'a, 'new>(v: Values<'a, u8, &'static str>) -> Values<'a, u8, &'new str> { v }
1685+
}
1686+
16731687
#[cfg(test)]
16741688
mod test_map {
16751689
use prelude::v1::*;

src/libstd/collections/hash/set.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,21 @@ impl<'a, T, S> Iterator for Union<'a, T, S>
10241024
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
10251025
}
10261026

1027+
#[allow(dead_code)]
1028+
fn assert_covariance() {
1029+
fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> { v }
1030+
fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> { v }
1031+
fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> { v }
1032+
fn difference<'a, 'new>(v: Difference<'a, &'static str, RandomState>)
1033+
-> Difference<'a, &'new str, RandomState> { v }
1034+
fn symmetric_difference<'a, 'new>(v: SymmetricDifference<'a, &'static str, RandomState>)
1035+
-> SymmetricDifference<'a, &'new str, RandomState> { v }
1036+
fn intersection<'a, 'new>(v: Intersection<'a, &'static str, RandomState>)
1037+
-> Intersection<'a, &'new str, RandomState> { v }
1038+
fn union<'a, 'new>(v: Union<'a, &'static str, RandomState>)
1039+
-> Union<'a, &'new str, RandomState> { v }
1040+
}
1041+
10271042
#[cfg(test)]
10281043
mod test_set {
10291044
use prelude::v1::*;

0 commit comments

Comments
 (0)