From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- tests/ui/hashmap/hashmap-capacity-overflow.rs | 12 +++ tests/ui/hashmap/hashmap-index-mut.rs | 6 ++ tests/ui/hashmap/hashmap-index-mut.stderr | 19 +++++ tests/ui/hashmap/hashmap-iter-value-lifetime.rs | 10 +++ .../ui/hashmap/hashmap-iter-value-lifetime.stderr | 15 ++++ tests/ui/hashmap/hashmap-lifetimes.rs | 8 ++ tests/ui/hashmap/hashmap-lifetimes.stderr | 13 +++ tests/ui/hashmap/hashmap-memory.rs | 94 ++++++++++++++++++++++ 8 files changed, 177 insertions(+) create mode 100644 tests/ui/hashmap/hashmap-capacity-overflow.rs create mode 100644 tests/ui/hashmap/hashmap-index-mut.rs create mode 100644 tests/ui/hashmap/hashmap-index-mut.stderr create mode 100644 tests/ui/hashmap/hashmap-iter-value-lifetime.rs create mode 100644 tests/ui/hashmap/hashmap-iter-value-lifetime.stderr create mode 100644 tests/ui/hashmap/hashmap-lifetimes.rs create mode 100644 tests/ui/hashmap/hashmap-lifetimes.stderr create mode 100644 tests/ui/hashmap/hashmap-memory.rs (limited to 'tests/ui/hashmap') 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::::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::::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` + --> $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` +help: to modify a `HashMap`, 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; + + pub type mapper = extern "C" fn(String, putter); + + enum ctrl_proto { find_reducer(Vec, Sender), mapper_done, } + + fn start_mappers(ctrl: Sender, inputs: Vec) { + 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, input: String) { + let mut intermediates = HashMap::new(); + + fn emit(im: &mut HashMap, + ctrl: Sender, 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) { + let (tx, rx) = channel(); + + // This thread becomes the master control thread. It spawns others + // to do the rest. + + let mut reducers: HashMap; + + 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()]); +} -- cgit v1.2.3