1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
fn require_sync<T: Sync>(_: T) {}
fn require_send_sync<T: Send + Sync>(_: T) {}
struct NotSend(#[allow(dead_code)] *const ());
unsafe impl Sync for NotSend {}
#[test]
fn test_btree_map() {
// Tests of this form are prone to https://github.com/rust-lang/rust/issues/64552.
//
// In theory the async block's future would be Send if the value we hold
// across the await point is Send, and Sync if the value we hold across the
// await point is Sync.
//
// We test autotraits in this convoluted way, instead of a straightforward
// `require_send_sync::<TypeIWantToTest>()`, because the interaction with
// coroutines exposes some current limitations in rustc's ability to prove a
// lifetime bound on the erased coroutine witness types. See the above link.
//
// A typical way this would surface in real code is:
//
// fn spawn<T: Future + Send>(_: T) {}
//
// async fn f() {
// let map = BTreeMap::<u32, Box<dyn Send + Sync>>::new();
// for _ in &map {
// async {}.await;
// }
// }
//
// fn main() {
// spawn(f());
// }
//
// where with some unintentionally overconstrained Send impls in alloc's
// internals, the future might incorrectly not be Send even though every
// single type involved in the program is Send and Sync.
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Iter<'_, &u32, &u32>>;
async {}.await;
});
// Testing like this would not catch all issues that the above form catches.
require_send_sync(None::<alloc::collections::btree_map::Iter<'_, &u32, &u32>>);
require_sync(async {
let _v = None::<alloc::collections::btree_map::Iter<'_, u32, NotSend>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::BTreeMap<&u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<
alloc::collections::btree_map::ExtractIf<'_, &u32, &u32, fn(&&u32, &mut &u32) -> bool>,
>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Entry<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::IntoIter<&u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::IntoKeys<&u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::IntoValues<&u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Iter<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::IterMut<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Keys<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::OccupiedEntry<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::OccupiedError<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Range<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::RangeMut<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::VacantEntry<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::Values<'_, &u32, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_map::ValuesMut<'_, &u32, &u32>>;
async {}.await;
});
}
#[test]
fn test_btree_set() {
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::BTreeSet<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::Difference<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::ExtractIf<'_, &u32, fn(&&u32) -> bool>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::Intersection<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::IntoIter<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::Iter<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::Range<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::SymmetricDifference<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::btree_set::Union<'_, &u32>>;
async {}.await;
});
}
#[test]
fn test_binary_heap() {
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::BinaryHeap<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::Drain<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::DrainSorted<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::IntoIter<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::IntoIterSorted<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::Iter<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::binary_heap::PeekMut<'_, &u32>>;
async {}.await;
});
}
#[test]
fn test_linked_list() {
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::Cursor<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::CursorMut<'_, &u32>>;
async {}.await;
});
// FIXME
/*
require_send_sync(async {
let _v =
None::<alloc::collections::linked_list::ExtractIf<'_, &u32, fn(&mut &u32) -> bool>>;
async {}.await;
});
*/
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::IntoIter<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::Iter<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::IterMut<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::linked_list::LinkedList<&u32>>;
async {}.await;
});
}
#[test]
fn test_vec_deque() {
require_send_sync(async {
let _v = None::<alloc::collections::vec_deque::Drain<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::vec_deque::IntoIter<&u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::vec_deque::Iter<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::vec_deque::IterMut<'_, &u32>>;
async {}.await;
});
require_send_sync(async {
let _v = None::<alloc::collections::vec_deque::VecDeque<&u32>>;
async {}.await;
});
}
|