summaryrefslogtreecommitdiffstats
path: root/src/test/ui/hashmap
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ui/hashmap')
-rw-r--r--src/test/ui/hashmap/hashmap-capacity-overflow.rs12
-rw-r--r--src/test/ui/hashmap/hashmap-iter-value-lifetime.rs10
-rw-r--r--src/test/ui/hashmap/hashmap-iter-value-lifetime.stderr15
-rw-r--r--src/test/ui/hashmap/hashmap-lifetimes.rs8
-rw-r--r--src/test/ui/hashmap/hashmap-lifetimes.stderr13
-rw-r--r--src/test/ui/hashmap/hashmap-memory.rs94
6 files changed, 152 insertions, 0 deletions
diff --git a/src/test/ui/hashmap/hashmap-capacity-overflow.rs b/src/test/ui/hashmap/hashmap-capacity-overflow.rs
new file mode 100644
index 000000000..2988af065
--- /dev/null
+++ b/src/test/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/src/test/ui/hashmap/hashmap-iter-value-lifetime.rs b/src/test/ui/hashmap/hashmap-iter-value-lifetime.rs
new file mode 100644
index 000000000..260ea8c7a
--- /dev/null
+++ b/src/test/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/src/test/ui/hashmap/hashmap-iter-value-lifetime.stderr b/src/test/ui/hashmap/hashmap-iter-value-lifetime.stderr
new file mode 100644
index 000000000..d6e7a1d45
--- /dev/null
+++ b/src/test/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/src/test/ui/hashmap/hashmap-lifetimes.rs b/src/test/ui/hashmap/hashmap-lifetimes.rs
new file mode 100644
index 000000000..295bf3b0e
--- /dev/null
+++ b/src/test/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/src/test/ui/hashmap/hashmap-lifetimes.stderr b/src/test/ui/hashmap/hashmap-lifetimes.stderr
new file mode 100644
index 000000000..d1bcd53ae
--- /dev/null
+++ b/src/test/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/src/test/ui/hashmap/hashmap-memory.rs b/src/test/ui/hashmap/hashmap-memory.rs
new file mode 100644
index 000000000..2031196ab
--- /dev/null
+++ b/src/test/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!["../src/test/run-pass/hashmap-memory.rs".to_string()]);
+}