diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/hashmap | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/hashmap')
-rw-r--r-- | tests/ui/hashmap/hashmap-capacity-overflow.rs | 12 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-index-mut.rs | 6 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-index-mut.stderr | 19 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-iter-value-lifetime.rs | 10 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-iter-value-lifetime.stderr | 15 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-lifetimes.rs | 8 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-lifetimes.stderr | 13 | ||||
-rw-r--r-- | tests/ui/hashmap/hashmap-memory.rs | 94 |
8 files changed, 177 insertions, 0 deletions
diff --git a/tests/ui/hashmap/hashmap-capacity-overflow.rs b/tests/ui/hashmap/hashmap-capacity-overflow.rs new file mode 100644 index 000000000..2988af065 --- /dev/null +++ b/tests/ui/hashmap/hashmap-capacity-overflow.rs @@ -0,0 +1,12 @@ +// run-fail +// error-pattern:capacity overflow +// ignore-emscripten no processes + +use std::collections::hash_map::HashMap; +use std::mem::size_of; + +fn main() { + let threshold = usize::MAX / size_of::<(u64, u64, u64)>(); + let mut h = HashMap::<u64, u64>::with_capacity(threshold + 100); + h.insert(0, 0); +} diff --git a/tests/ui/hashmap/hashmap-index-mut.rs b/tests/ui/hashmap/hashmap-index-mut.rs new file mode 100644 index 000000000..98448e9d5 --- /dev/null +++ b/tests/ui/hashmap/hashmap-index-mut.rs @@ -0,0 +1,6 @@ +use std::collections::HashMap; + +fn main() { + let mut map = HashMap::<u32, u32>::new(); + map[&0] = 1; //~ ERROR cannot assign +} diff --git a/tests/ui/hashmap/hashmap-index-mut.stderr b/tests/ui/hashmap/hashmap-index-mut.stderr new file mode 100644 index 000000000..c1948ab62 --- /dev/null +++ b/tests/ui/hashmap/hashmap-index-mut.stderr @@ -0,0 +1,19 @@ +error[E0594]: cannot assign to data in an index of `HashMap<u32, u32>` + --> $DIR/hashmap-index-mut.rs:5:5 + | +LL | map[&0] = 1; + | ^^^^^^^^^^^ cannot assign + | + = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<u32, u32>` +help: to modify a `HashMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API + | +LL | map.insert(&0, 1); + | ~~~~~~~~ ~ + +LL | map.get_mut(&0).map(|val| { *val = 1; }); + | ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++ +LL | let val = map.entry(&0).or_insert(1); + | +++++++++ ~~~~~~~ ~~~~~~~~~~~~ + + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. diff --git a/tests/ui/hashmap/hashmap-iter-value-lifetime.rs b/tests/ui/hashmap/hashmap-iter-value-lifetime.rs new file mode 100644 index 000000000..260ea8c7a --- /dev/null +++ b/tests/ui/hashmap/hashmap-iter-value-lifetime.rs @@ -0,0 +1,10 @@ +fn main() { + let mut my_stuff = std::collections::HashMap::new(); + my_stuff.insert(0, 42); + + let (_, thing) = my_stuff.iter().next().unwrap(); + + my_stuff.clear(); //~ ERROR cannot borrow + + println!("{}", *thing); +} diff --git a/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr b/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr new file mode 100644 index 000000000..d6e7a1d45 --- /dev/null +++ b/tests/ui/hashmap/hashmap-iter-value-lifetime.stderr @@ -0,0 +1,15 @@ +error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable + --> $DIR/hashmap-iter-value-lifetime.rs:7:5 + | +LL | let (_, thing) = my_stuff.iter().next().unwrap(); + | --------------- immutable borrow occurs here +LL | +LL | my_stuff.clear(); + | ^^^^^^^^^^^^^^^^ mutable borrow occurs here +LL | +LL | println!("{}", *thing); + | ------ immutable borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0502`. diff --git a/tests/ui/hashmap/hashmap-lifetimes.rs b/tests/ui/hashmap/hashmap-lifetimes.rs new file mode 100644 index 000000000..295bf3b0e --- /dev/null +++ b/tests/ui/hashmap/hashmap-lifetimes.rs @@ -0,0 +1,8 @@ +fn main() { + let mut my_stuff = std::collections::HashMap::new(); + my_stuff.insert(0, 42); + + let mut it = my_stuff.iter(); + my_stuff.insert(1, 43); //~ ERROR cannot borrow + it; +} diff --git a/tests/ui/hashmap/hashmap-lifetimes.stderr b/tests/ui/hashmap/hashmap-lifetimes.stderr new file mode 100644 index 000000000..d1bcd53ae --- /dev/null +++ b/tests/ui/hashmap/hashmap-lifetimes.stderr @@ -0,0 +1,13 @@ +error[E0502]: cannot borrow `my_stuff` as mutable because it is also borrowed as immutable + --> $DIR/hashmap-lifetimes.rs:6:5 + | +LL | let mut it = my_stuff.iter(); + | --------------- immutable borrow occurs here +LL | my_stuff.insert(1, 43); + | ^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here +LL | it; + | -- immutable borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0502`. diff --git a/tests/ui/hashmap/hashmap-memory.rs b/tests/ui/hashmap/hashmap-memory.rs new file mode 100644 index 000000000..87f8b6ad5 --- /dev/null +++ b/tests/ui/hashmap/hashmap-memory.rs @@ -0,0 +1,94 @@ +// run-pass + +#![allow(non_camel_case_types)] +#![allow(dead_code)] +#![allow(unused_mut)] +// ignore-emscripten No support for threads + +/** + A somewhat reduced test case to expose some Valgrind issues. + + This originally came from the word-count benchmark. +*/ + +pub fn map(filename: String, mut emit: map_reduce::putter) { + emit(filename, "1".to_string()); +} + +mod map_reduce { + use std::collections::HashMap; + use std::sync::mpsc::{channel, Sender}; + use std::str; + use std::thread; + + pub type putter<'a> = Box<dyn FnMut(String, String) + 'a>; + + pub type mapper = extern "C" fn(String, putter); + + enum ctrl_proto { find_reducer(Vec<u8>, Sender<isize>), mapper_done, } + + fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) { + for i in &inputs { + let ctrl = ctrl.clone(); + let i = i.clone(); + thread::spawn(move|| map_task(ctrl.clone(), i.clone()) ); + } + } + + fn map_task(ctrl: Sender<ctrl_proto>, input: String) { + let mut intermediates = HashMap::new(); + + fn emit(im: &mut HashMap<String, isize>, + ctrl: Sender<ctrl_proto>, key: String, + _val: String) { + if im.contains_key(&key) { + return; + } + let (tx, rx) = channel(); + println!("sending find_reducer"); + ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap(); + println!("receiving"); + let c = rx.recv().unwrap(); + println!("{}", c); + im.insert(key, c); + } + + let ctrl_clone = ctrl.clone(); + ::map(input, Box::new(|a,b| emit(&mut intermediates, ctrl.clone(), a, b))); + ctrl_clone.send(ctrl_proto::mapper_done).unwrap(); + } + + pub fn map_reduce(inputs: Vec<String>) { + let (tx, rx) = channel(); + + // This thread becomes the master control thread. It spawns others + // to do the rest. + + let mut reducers: HashMap<String, isize>; + + reducers = HashMap::new(); + + start_mappers(tx, inputs.clone()); + + let mut num_mappers = inputs.len() as isize; + + while num_mappers > 0 { + match rx.recv().unwrap() { + ctrl_proto::mapper_done => { num_mappers -= 1; } + ctrl_proto::find_reducer(k, cc) => { + let mut c; + match reducers.get(&str::from_utf8(&k).unwrap().to_string()) { + Some(&_c) => { c = _c; } + None => { c = 0; } + } + cc.send(c).unwrap(); + } + } + } + } +} + +pub fn main() { + map_reduce::map_reduce( + vec!["../tests/run-pass/hashmap-memory.rs".to_string()]); +} |