Skip to content

Commit 3039efc

Browse files
authored
fix spec for HashMap::clone (#2513)
1 parent ee05df3 commit 3039efc

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

source/rust_verify_test/tests/std.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,3 +926,39 @@ test_verify_one_file! {
926926
}
927927
} => Err(err) => assert_fails(err, 7)
928928
}
929+
930+
test_verify_one_file! {
931+
#[test] hash_map_clone_issue1835 verus_code! {
932+
use vstd::prelude::*;
933+
use std::collections::HashMap;
934+
935+
pub struct WeirdPair {
936+
pub x: u64,
937+
pub y: u64,
938+
}
939+
940+
impl Clone for WeirdPair {
941+
fn clone(&self) -> (ret: Self)
942+
ensures ret == (Self { x: self.x, y: 0 })
943+
{
944+
Self { x: self.x, y: 0 }
945+
}
946+
}
947+
948+
fn test() {
949+
let mut h = HashMap::<u64, WeirdPair>::new();
950+
h.insert(0, WeirdPair { x: 1, y: 2 });
951+
let h2 = h.clone();
952+
assert(h2@.dom() == h@.dom());
953+
assert(h2@[0] == WeirdPair { x: 1, y: 0 } || h@[0] == h2@[0]);
954+
}
955+
956+
fn test_fails() {
957+
let mut h = HashMap::<u64, WeirdPair>::new();
958+
h.insert(0, WeirdPair { x: 1, y: 2 });
959+
let h2 = h.clone();
960+
assert(h2@.dom() == h@.dom());
961+
assert(h2@[0] == WeirdPair { x: 1, y: 2 }); // FAILS
962+
}
963+
} => Err(err) => assert_fails(err, 1)
964+
}

source/vstd/std_specs/hash.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,10 @@ pub assume_specification<K: Clone, V: Clone, S: Clone, A: Allocator + Clone>[ <H
612612
A,
613613
> as Clone>::clone ](this: &HashMap<K, V, S, A>) -> (other: HashMap<K, V, S, A>)
614614
ensures
615-
other@ == this@,
615+
other@.dom() == this@.dom(),
616+
forall|key|
617+
#![trigger other@.dom().contains(key)]
618+
other@.dom().contains(key) ==> cloned(this@[key], #[trigger] other@[key]),
616619
;
617620

618621
pub assume_specification<Key, Value>[ HashMap::<Key, Value>::new ]() -> (m: HashMap<

0 commit comments

Comments
 (0)