cli: add safe owning general-key hash maps

Use explicit static hash/equality functions, dense complete key/value entries
and Copy probe indices. Preserve returned ownership and allocation failure
state without unsafe code or new runtime instructions.

Add normative contracts, API/reference budgets, 33 conformance cases, independent
Copy/owner dict traces and 55 CLI/package/profile checks. Full corpus: 947 passing.
Collection-every-step scheduler stress remains in progress, recorded in RFC/plan.
This commit is contained in:
dev committed 2026-10-07 10:55:09 +03:00
1 parent f02f4bd55b
commit 7bb5bd7931
52 files changed
+7736 -11

No files matched your search

@@ -0,0 +1,30 @@
// expect: reject E2101
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(map: mut<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> void effects [] {
ref entries = map.entries; ref view = buffer.view(entries);
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(map, Key { id: 1 }, 42));
assert(view.len == 0usize);
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,50 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:256
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
fn hash(key: ref<buffer<u8>>) -> u64 effects [] { return 3; }
fn equal(a: ref<buffer<u8>>, b: ref<buffer<u8>>) -> bool effects [] {
ref left = buffer.view(a); ref right = buffer.view(b);
if left.len != right.len { return false; }
for i in 0usize..left.len { if left[i] != right[i] { return false; } }
return true;
}
fn bytes(value: u8, heap: cap<mem.alloc>) -> buffer<u8> effects [mem.alloc] {
let result = buffer.new<u8>(2usize, heap) or trap;
{ mut q = result; buffer.push(q, value) or trap; buffer.push(q, 0u8) or trap; }
return move result;
}
fn key_freed(key: buffer<u8>) -> void effects [] {
{ mut q = key; while buffer.len(q) != 0usize { let unused = buffer.pop(q) or trap; } } buffer.free(move key) or trap;
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<buffer<u8>, u64, fn hash, fn equal>(2, heap);
{ mut q = map;
let first = bytes(7u8, heap); inserted::<buffer<u8>,u64>(std.hashmap.insert::<buffer<u8>, u64, fn hash, fn equal>(q, move first, 42));
let second = bytes(8u8, heap); inserted::<buffer<u8>,u64>(std.hashmap.insert::<buffer<u8>, u64, fn hash, fn equal>(q, move second, 8));
let query = bytes(7u8, heap);
assert(extracted::<u64>(std.hashmap.get::<buffer<u8>, u64, fn hash, fn equal>(ref q, ref query)) == 42u64);
let entry = extracted::<std.hashmap.Entry<buffer<u8>,u64>>(std.hashmap.remove::<buffer<u8>, u64, fn hash, fn equal>(q, ref query));
let (key,value) = struct.unpack(move entry); assert(value == 42u64); key_freed(move key); key_freed(move query);
let last = extracted::<std.hashmap.Entry<buffer<u8>,u64>>(std.hashmap.pop::<buffer<u8>, u64, fn hash, fn equal>(q)); let (last_key,last_value) = struct.unpack(move last); assert(last_value == 8u64); key_freed(move last_key);
} released::<buffer<u8>, u64, fn hash, fn equal>(move map); return 42;
}
+53
View File
@@ -0,0 +1,53 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:1024
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(2, heap);
{ mut q = map;
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 11));
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 22));
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 3 }, 33) {
std.hashmap.InsertResult::<Key,u64>::full(entry) => { assert(entry.key.id == 3u64); assert(entry.value == 33u64); },
std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::replaced(old) => { trap; }
}
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 42) {
std.hashmap.InsertResult::<Key,u64>::replaced(old) => { assert(old.key.id == 2u64); assert(old.value == 22u64); },
std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::full(entry) => { trap; }
}
let key = Key { id: 1 };
let removed = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.remove::<Key, u64, fn hash, fn equal>(q, ref key));
assert(removed.key.id == 1u64); assert(removed.value == 11u64);
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 3 }, 33));
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 4, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
}
{ ref q = map; let key = Key { id: 2 }; assert(std.hashmap.contains::<Key, u64, fn hash, fn equal>(q, ref key));
assert(extracted::<u64>(std.hashmap.get::<Key, u64, fn hash, fn equal>(q, ref key)) == 42u64);
assert(std.hashmap.len::<Key, u64, fn hash, fn equal>(q) == 2usize); assert(std.hashmap.capacity::<Key, u64, fn hash, fn equal>(q) == 4usize);
}
{ mut q = map; let last = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(last.key.id == 3u64);
let first = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(first.value == 42u64); }
released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
+35
View File
@@ -0,0 +1,35 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:58
// grant: dst=fs.write:/out
// file: /out/hashmap.txt=42
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>, dst: cap<fs.write>) -> u64 effects [mem.alloc, fs.write] {
let map = opened::<Key, file<write>, fn hash, fn equal>(1, heap); let file = fs.create(dst, b"/out/hashmap.txt") or trap;
{ mut q = map; inserted::<Key,file<write>>(std.hashmap.insert::<Key, file<write>, fn hash, fn equal>(q, Key { id: 7 }, move file));
let key = Key { id: 7 }; let entry = extracted::<std.hashmap.Entry<Key,file<write>>>(std.hashmap.remove::<Key, file<write>, fn hash, fn equal>(q, ref key));
let (stored_key,item) = struct.unpack(move entry); fs.write_all(item, b"42") or trap; fs.close(move item) or trap; }
released::<Key, file<write>, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,28 @@
// expect: reject E3002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(map: std.hashmap.HashMap<Key, own<u64>, fn hash, fn equal>) -> void effects [] {
match std.hashmap.free::<Key, own<u64>, fn hash, fn equal>(move map) { std.hashmap.FreeResult::<Key, own<u64>, fn hash, fn equal>::not_empty(returned) => { return; }, std.hashmap.FreeResult::<Key, own<u64>, fn hash, fn equal>::ok => {} }
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,32 @@
// expect: reject E3002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(map: mut<std.hashmap.HashMap<Key,own<u64>,fn hash,fn equal>>, value: own<u64>) -> void effects [] {
match std.hashmap.insert::<Key,own<u64>,fn hash,fn equal>(map, Key { id: 1 }, move value) {
std.hashmap.InsertResult::<Key,own<u64>>::inserted => {},
std.hashmap.InsertResult::<Key,own<u64>>::replaced(entry) => { trap; },
std.hashmap.InsertResult::<Key,own<u64>>::full(entry) => { return; }
}
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,28 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(map: ref<std.hashmap.HashMap<Key,own<u64>,fn hash,fn equal>>, key: ref<Key>) -> void effects [] {
std.hashmap.get::<Key,own<u64>,fn hash,fn equal>(map, key);
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,35 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:58
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(1, heap);
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42));
mut index = q.buckets; mut buckets = buffer.view(index); buckets[1].state = std.hashmap.BucketState::empty; }
match std.hashmap.free::<Key, u64, fn hash, fn equal>(move map) { std.hashmap.FreeResult::<Key, u64, fn hash, fn equal>::not_empty(returned) => {
let (entries,buckets) = struct.unpack(move returned); { mut q = entries; let entry = buffer.pop(q) or trap; assert(entry.value == 42u64); } buffer.free(move entries) or trap;
{ mut q = buckets; while buffer.len(q) != 0usize { let unused = buffer.pop(q) or trap; } } buffer.free(move buckets) or trap;
}, std.hashmap.FreeResult::<Key, u64, fn hash, fn equal>::ok => { trap; } } return 42;
}
@@ -0,0 +1,40 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:82
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64, owner: own<u64> }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn consumed(entry: std.hashmap.Entry<Key,own<u64>>, expected_key: u64, expected_value: u64) -> void effects [] {
let (key,value) = struct.unpack(move entry); let (id,owner) = struct.unpack(move key);
assert(own.take(move owner) == expected_key); assert(own.take(move value) == expected_value);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key,u64,fn hash,fn equal>(1, heap);
let key = Key { id: 7, owner: own.new(7u64,heap) or trap };
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key,u64,fn hash,fn equal>(q, move key, 42)); }
let query = Key { id: 7, owner: own.new(7u64,heap) or trap };
{ ref q = map; assert(extracted::<u64>(std.hashmap.get::<Key,u64,fn hash,fn equal>(q,ref query)) == 42u64); }
let (query_id,query_owner) = struct.unpack(move query); own.take(move query_owner);
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key,u64,fn hash,fn equal>(q)); let (stored_key,value) = struct.unpack(move entry); let (id,owner) = struct.unpack(move stored_key); assert(own.take(move owner) == 7u64); assert(value == 42u64); }
released::<Key,u64,fn hash,fn equal>(move map); return 42;
}
@@ -0,0 +1,30 @@
// expect: reject E7003
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64, owner: own<u64> }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn consumed(entry: std.hashmap.Entry<Key,own<u64>>, expected_key: u64, expected_value: u64) -> void effects [] {
let (key,value) = struct.unpack(move entry); let (id,owner) = struct.unpack(move key);
assert(own.take(move owner) == expected_key); assert(own.take(move value) == expected_value);
}
fn bad(q: mut<std.hashmap.HashMap<Key, own<u64>, fn hash, fn equal>>, key: Key, value: own<u64>) -> void effects [] { std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, key, move value); }
fn main() -> u64 effects [] { return 42; }
+27
View File
@@ -0,0 +1,27 @@
// expect: reject E4101
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn worker(q: mut<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> void effects [] {}
fn bad(q: mut<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> void effects [task.spawn, task.wait] { scope s { let t = task.spawn(s, worker, q); inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42)); task.join(move t); } }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,34 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:58
// schedules: 12
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn worker(q: mut<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> void effects [] { inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42)); }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc, task.spawn, task.wait] {
let map = opened::<Key, u64, fn hash, fn equal>(1, heap);
{ mut q = map; scope s { let t = task.spawn(s, worker, q); task.join(move t);
let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64); } }
released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,28 @@
// expect: reject E3101
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(map: mut<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>, heap: cap<mem.alloc>) -> void effects [] {
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(map, 0, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
}
fn main() -> u64 effects [] { return 42; }
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,38 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:116
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return key.id; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(2, heap);
{ mut q = map;
for number in 0usize..4usize {
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: number as! u64 }, 42));
let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64);
}
let absent = Key { id: 100 }; assert(not std.hashmap.contains::<Key, u64, fn hash, fn equal>(ref q, ref absent));
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, absent, 42));
let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64);
} released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,27 @@
// expect: reject E4002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn worker(q: ref<std.hashmap.HashMap<Key, file<write>, fn hash, fn equal>>) -> void effects [] {}
fn bad(q: ref<std.hashmap.HashMap<Key, file<write>, fn hash, fn equal>>) -> void effects [task.spawn, task.wait] { scope s { let t = task.spawn(s, worker, q); task.join(move t); } }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,33 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:8
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
match std.hashmap.new::<Key, u64, fn hash, fn equal>((0usize -% 1usize) / 2usize + 1usize, heap) { std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::err(error) => {}, std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::ok(map) => { trap; } }
match std.hashmap.new::<Key, u64, fn hash, fn equal>((0usize -% 1usize) / 24usize + 1usize, heap) { std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::err(error) => {}, std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::ok(map) => { trap; } }
let map = opened::<Key, u64, fn hash, fn equal>(0, heap);
{ mut q = map; match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, (0usize -% 1usize) / 2usize + 1usize, heap) { std.hashmap.ReserveResult::err(error) => {}, std.hashmap.ReserveResult::ok => { trap; } } }
released::<Key, u64, fn hash, fn equal>(move map); let owner = own.new(42u64, heap) or trap; return own.take(move owner);
}
@@ -0,0 +1,45 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:1024
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64, owner: own<u64> }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn consumed(entry: std.hashmap.Entry<Key,own<u64>>) -> u64 effects [] {
let (key, value) = struct.unpack(move entry); let (id, owner) = struct.unpack(move key);
assert(own.take(move owner) == id); return own.take(move value);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, own<u64>, fn hash, fn equal>(2, heap);
{ mut q = map;
let k1 = Key { id: 1, owner: own.new(1u64, heap) or trap }; let v1 = own.new(11u64, heap) or trap;
inserted::<Key,own<u64>>(std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move k1, move v1));
let k2 = Key { id: 2, owner: own.new(2u64, heap) or trap }; let v2 = own.new(22u64, heap) or trap;
inserted::<Key,own<u64>>(std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move k2, move v2));
let query = Key { id: 1, owner: own.new(1u64, heap) or trap };
assert(consumed(extracted::<std.hashmap.Entry<Key,own<u64>>>(std.hashmap.remove::<Key, own<u64>, fn hash, fn equal>(q, ref query))) == 11u64);
let (id, owner) = struct.unpack(move query); own.take(move owner);
match std.hashmap.reserve::<Key, own<u64>, fn hash, fn equal>(q, 3, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
assert(consumed(extracted::<std.hashmap.Entry<Key,own<u64>>>(std.hashmap.pop::<Key, own<u64>, fn hash, fn equal>(q))) == 22u64);
}
released::<Key, own<u64>, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,47 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:98
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64, owner: own<u64> }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn consumed(entry: std.hashmap.Entry<Key,own<u64>>, expected_key: u64, expected_value: u64) -> void effects [] {
let (key,value) = struct.unpack(move entry); let (id,owner) = struct.unpack(move key);
assert(own.take(move owner) == expected_key); assert(own.take(move value) == expected_value);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, own<u64>, fn hash, fn equal>(1, heap);
{ mut q = map;
let key = Key { id: 7, owner: own.new(101u64, heap) or trap }; let value = own.new(11u64, heap) or trap;
inserted::<Key,own<u64>>(std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move key, move value));
let replacement = Key { id: 7, owner: own.new(202u64, heap) or trap }; let replacement_value = own.new(42u64, heap) or trap;
match std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move replacement, move replacement_value) {
std.hashmap.InsertResult::<Key,own<u64>>::replaced(entry) => { consumed(move entry, 101, 11); }, std.hashmap.InsertResult::<Key,own<u64>>::inserted => { trap; }, std.hashmap.InsertResult::<Key,own<u64>>::full(entry) => { trap; } }
let full_key = Key { id: 8, owner: own.new(303u64, heap) or trap }; let full_value = own.new(99u64, heap) or trap;
match std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move full_key, move full_value) {
std.hashmap.InsertResult::<Key,own<u64>>::full(entry) => { consumed(move entry, 303, 99); }, std.hashmap.InsertResult::<Key,own<u64>>::inserted => { trap; }, std.hashmap.InsertResult::<Key,own<u64>>::replaced(entry) => { trap; } }
}
match std.hashmap.free::<Key, own<u64>, fn hash, fn equal>(move map) {
std.hashmap.FreeResult::<Key, own<u64>, fn hash, fn equal>::not_empty(returned) => { { mut q = returned; consumed(extracted::<std.hashmap.Entry<Key,own<u64>>>(std.hashmap.pop::<Key, own<u64>, fn hash, fn equal>(q)), 202, 42); } released::<Key, own<u64>, fn hash, fn equal>(move returned); },
std.hashmap.FreeResult::<Key, own<u64>, fn hash, fn equal>::ok => { trap; } }
return 42;
}
@@ -0,0 +1,27 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn effectful(key: ref<Key>) -> u64 effects [mem.alloc] { return 3; }
fn bad(q: ref<std.hashmap.HashMap<Key,own<u64>,fn effectful,fn equal>>) -> void effects [] {}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,27 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn other(key: ref<Key>) -> u64 effects [] { return 3; }
fn bad(map: ref<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> void effects [] { std.hashmap.len::<Key,u64,fn other,fn equal>(map); }
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,28 @@
// expect: reject E3002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(q: mut<std.hashmap.HashMap<Key, own<u64>, fn hash, fn equal>>) -> void effects [] {
match std.hashmap.pop::<Key, own<u64>, fn hash, fn equal>(q) { std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::some(item) => { return; }, std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::none => {} }
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,28 @@
// expect: reject E3002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(q: mut<std.hashmap.HashMap<Key, own<u64>, fn hash, fn equal>>, key: ref<Key>) -> void effects [] {
match std.hashmap.remove::<Key, own<u64>, fn hash, fn equal>(q,key) { std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::some(item) => { return; }, std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::none => {} }
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,28 @@
// expect: reject E3002
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bad(q: mut<std.hashmap.HashMap<Key, own<u64>, fn hash, fn equal>>, value: own<u64>) -> void effects [] {
match std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q,Key { id: 1 },move value) { std.hashmap.InsertResult::<Key,own<u64>>::replaced(item) => { return; }, std.hashmap.InsertResult::<Key,own<u64>>::inserted => {}, std.hashmap.InsertResult::<Key,own<u64>>::full(item) => { trap; } }
}
fn main() -> u64 effects [] { return 42; }
@@ -0,0 +1,35 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: first=mem.alloc:58
// grant: second=mem.alloc:116
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(first: cap<mem.alloc>, second: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(1, first);
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42));
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 2, second) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } } }
let old = opened::<Key, u64, fn hash, fn equal>(1, first); released::<Key, u64, fn hash, fn equal>(move old);
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64); }
released::<Key, u64, fn hash, fn equal>(move map); let next = opened::<Key, u64, fn hash, fn equal>(2, second); released::<Key, u64, fn hash, fn equal>(move next); return 42;
}
@@ -0,0 +1,48 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:251
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn check(heap: cap<mem.alloc>) -> void effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(1, heap);
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42)); }
let empty = std.hashmap.Bucket { hash: 0, index: 0, state: std.hashmap.BucketState::empty };
var before: [std.hashmap.Bucket; 2] = [empty; 2];
{ ref q = map; ref index = q.buckets; ref buckets = buffer.view(index); before[0] = buckets[0]; before[1] = buckets[1]; }
{ mut q = map;
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 2, heap) { std.hashmap.ReserveResult::err(error) => { match error { alloc_error::budget_exceeded => {}, alloc_error::out_of_memory => { trap; } } }, std.hashmap.ReserveResult::ok => { trap; } }
assert(std.hashmap.capacity::<Key, u64, fn hash, fn equal>(ref q) == 1usize);
}
{ ref q = map; ref index = q.buckets; ref buckets = buffer.view(index);
for i in 0usize..2usize { assert(buckets[i].hash == before[i].hash); assert(buckets[i].index == before[i].index);
match before[i].state { std.hashmap.BucketState::empty => { match buckets[i].state { std.hashmap.BucketState::empty => {}, std.hashmap.BucketState::deleted => { trap; }, std.hashmap.BucketState::occupied => { trap; } } }, std.hashmap.BucketState::deleted => { trap; }, std.hashmap.BucketState::occupied => { match buckets[i].state { std.hashmap.BucketState::occupied => {}, std.hashmap.BucketState::empty => { trap; }, std.hashmap.BucketState::deleted => { trap; } } } }
} }
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64); }
released::<Key, u64, fn hash, fn equal>(move map);
let reused = own.new([0u8; 125], heap) or trap; own.free(move reused);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let early = cap.narrow.budget(heap, 125usize) or trap; check(early);
let late = cap.narrow.budget(heap, 126usize) or trap; check(late); return 42;
}
@@ -0,0 +1,34 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:174
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(1, heap);
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 42));
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 2, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
let owner = own.new([0u8; 58], heap) or trap; own.free(move owner);
let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(entry.value == 42u64);
} released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,33 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:40
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
match std.hashmap.new::<Key, u64, fn hash, fn equal>(1, heap) {
std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::err(error) => { match error { alloc_error::budget_exceeded => {}, alloc_error::out_of_memory => { trap; } } },
std.hashmap.NewResult::<Key, u64, fn hash, fn equal>::ok(map) => { trap; }
}
let owner = own.new([0u8; 40], heap) or trap; own.free(move owner); return 42;
}
@@ -0,0 +1,39 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:256
// schedules: 12
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
struct Job { number: u64 }
fn worker(map: std.hashmap.HashMap<Key, sender<Job>, fn hash, fn equal>) -> void effects [task.wait] {
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,sender<Job>>>(std.hashmap.pop::<Key, sender<Job>, fn hash, fn equal>(q));
let (key,tx) = struct.unpack(move entry); channel.send(tx, Job { number: 42 }) or trap; channel.close_sender(move tx); }
released::<Key, sender<Job>, fn hash, fn equal>(move map);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc, task.spawn, task.wait] {
match channel.make<Job>(1, heap) { ok(tx,rx) => {
let map = opened::<Key, sender<Job>, fn hash, fn equal>(1, heap); { mut q = map; inserted::<Key,sender<Job>>(std.hashmap.insert::<Key, sender<Job>, fn hash, fn equal>(q, Key { id: 7 }, move tx)); }
scope s { let t = task.spawn(s, worker, move map); let job = channel.recv(rx) or trap; task.join(move t); channel.close_receiver(move rx) or trap; assert(job.number == 42u64); }
}, err(error) => { trap; } } return 42;
}
@@ -0,0 +1,33 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:58
// schedules: 12
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn worker(q: ref<std.hashmap.HashMap<Key, u64, fn hash, fn equal>>) -> u64 effects [] { let key = Key { id: 7 }; return extracted::<u64>(std.hashmap.get::<Key, u64, fn hash, fn equal>(q, ref key)); }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc, task.spawn, task.wait] {
let map = opened::<Key, u64, fn hash, fn equal>(1, heap); { mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 7 }, 42)); }
{ ref q = map; scope s { let a = task.spawn(s, worker, q); let b = task.spawn(s, worker, q); assert(task.join(move a) == 42u64); assert(task.join(move b) == 42u64); } }
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); } released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
@@ -0,0 +1,38 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:116
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(2, heap);
{ mut q = map;
for cycle in 0usize..12usize {
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 1));
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 2));
let key = Key { id: 1 }; let removed = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.remove::<Key, u64, fn hash, fn equal>(q, ref key));
assert(removed.value == 1u64);
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 42) { std.hashmap.InsertResult::<Key,u64>::replaced(old) => { assert(old.value == 2u64); }, std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::full(old) => { trap; } }
let item = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(item.key.id == 2u64); assert(item.value == 42u64);
} } released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
+36
View File
@@ -0,0 +1,36 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:0
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, u64, fn hash, fn equal>(0, heap);
{ mut q = map; let key = Key { id: 1 };
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, key, 42) { std.hashmap.InsertResult::<Key,u64>::full(entry) => { assert(entry.value == 42u64); }, std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::replaced(old) => { trap; } }
assert(not std.hashmap.contains::<Key, u64, fn hash, fn equal>(ref q, ref key));
match std.hashmap.remove::<Key, u64, fn hash, fn equal>(q, ref key) { std.option.Option::<std.hashmap.Entry<Key,u64>>::none => {}, std.option.Option::<std.hashmap.Entry<Key,u64>>::some(entry) => { trap; } }
match std.hashmap.pop::<Key, u64, fn hash, fn equal>(q) { std.option.Option::<std.hashmap.Entry<Key,u64>>::none => {}, std.option.Option::<std.hashmap.Entry<Key,u64>>::some(entry) => { trap; } }
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 0, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
} released::<Key, u64, fn hash, fn equal>(move map); return 42;
}
+101
View File
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
"""General owning maps through explicit policies, source/AIR, packages and profiles."""
import json
from pathlib import Path
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / "zig-out/bin/aji"
CASES = REPO / "tests/conformance/source/hashmap"
checks = 0
def cli(*args, code=0):
global checks
run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr)
checks += 1
return run.stdout
def returned(path, *grants):
args = [item for grant in grants for item in ("--grant", grant)]
record = json.loads(cli("run", "--fuel", "4000000", *args, path))
assert record["status"] == "returned" and record["value"] == 42, record
return record
example = REPO / "examples/std/hashmap.aji"
returned(example, "heap=mem.alloc:66")
cli("check", example)
report = json.loads(cli("report", example))
assert report["unsafe"]["operations"] == 0 and report["unsafe"]["trusted_functions"] == [], report
canonical = cli("lower", example)
assert canonical == cli("lower", example)
assert "trusted" not in canonical and "unsafe" not in canonical
for name, budget in [("copy", 1024), ("owner", 1024), ("owner_returned", 98), ("byte_keys", 256),
("key_copy_lookup", 82), ("reserve_failure", 251), ("reserve_peak", 174),
("no_empty", 116), ("zero", 0), ("rollback", 40), ("model", 1120), ("model_owner", 1120)]:
returned(CASES / (name + ".aji"), f"heap=mem.alloc:{budget}")
for name, expected in [("full_leak", "E3002"), ("key_missing_move", "E7003"),
("get_owner", "E7005"), ("policy_mismatch", "E7005"),
("policy_effect", "E7005"), ("free_leak", "E3002")]:
commands = ("check", "report", "run", "explore") if expected == "E3002" else ("check", "lower", "report", "run", "explore")
for command in commands:
records = [json.loads(line) for line in cli(command, CASES / (name + ".aji"), code=1).splitlines()]
assert records and {error["code"] for error in records} == {expected}, records
with tempfile.TemporaryDirectory(prefix="aji-hashmap-") as directory:
root = Path(directory)
air = root / "lowered.air"
air.write_text(canonical)
cli("check", air)
assert returned(air, "heap=mem.alloc:66") == returned(example, "heap=mem.alloc:66")
# Hosted results use the same pinned library and explicit heap accounting.
result = root / "hosted.json"
stdout = cli("run", "--profile", "hosted", "--fuel", "4000000", "--grant", "heap=mem.alloc:66", "--result-file", result, example)
assert stdout == "" and json.loads(result.read_text())["value"] == 42
# Package policy selection is explicit and preserves nominal identities.
policy = root / "policy.aji"
policy.write_text('''module app.policy
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
''')
entry, manifest = root / "main.aji", root / "aji.toml"
manifest.write_text('[package]\nname = "hashmap"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.policy = "policy.aji"\n[capabilities]\nheap = "mem.alloc:66"\n')
program = example.read_text().replace("module demo.hashmap", "module app.entry")
start, end = program.index("struct Key"), program.index("fn main(")
program = program[:start].replace("import std.option;", "import std.option;\nimport app.policy;") + program[end:]
program = program.replace("<Key,", "<app.policy.Key,").replace("fn hash", "fn app.policy.hash").replace("fn equal", "fn app.policy.equal").replace("Key {", "app.policy.Key {")
entry.write_text(program)
record = json.loads(cli("run", "--fuel", "4000000", "--manifest", manifest))
assert record["value"] == 42, record
package_air = cli("lower", "--manifest", manifest)
cli("report", "--manifest", manifest)
imports = "import std.hashmap;\nimport std.option;\nimport app.policy;"
entry.write_text(program.replace(imports, "import app.policy;\nimport std.option;\nimport std.hashmap;"))
assert cli("lower", "--manifest", manifest) == package_air
entry.write_text(program)
policy.write_text(policy.read_text().replace("return 3;", "assert(false); return 3;"))
record = json.loads(cli("run", "--fuel", "4000000", "--manifest", manifest, code=4))
assert Path(record["source_location"]["file"]).name == "policy.aji", record
# Imported standard code diagnostics/traps retain repository paths.
invalid = (CASES / "copy.aji").read_text()
invalid = invalid.replace("fn main(", "fn entry_original(")
invalid += '''
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key,u64,fn hash,fn equal>(1,heap);
{ mut q = map; inserted::<Key,u64>(std.hashmap.insert::<Key,u64,fn hash,fn equal>(q, Key { id: 1 },42));
mut storage = q.buckets; mut buckets = buffer.view(storage); buckets[1].index = 7usize; }
{ ref q = map; let key = Key { id: 1 }; std.hashmap.contains::<Key,u64,fn hash,fn equal>(q,ref key); }
{ mut q = map; let entry = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key,u64,fn hash,fn equal>(q)); }
released::<Key,u64,fn hash,fn equal>(move map); return 42;
}
'''
entry.write_text(invalid)
record = json.loads(cli("run", "--fuel", "4000000", "--grant", "heap=mem.alloc:1024", entry, code=4))
assert record["code"] == "T0008" and record["source_location"]["file"] == "std/hashmap.aji", record
print(f"general hash-map CLI integration passed ({checks} checks)")