add constcast and immutable free

This commit is contained in:
2026-07-22 00:44:35 +02:00
parent 5f343ad2d3
commit 9c6215776e
12 changed files with 256 additions and 11 deletions
+11 -7
View File
@@ -960,6 +960,11 @@
value-producing control-flow forms with or without an error capture
- void fallthrough and diverging `noreturn` fallbacks remain valid
50. add `constcast!` and immutable deallocation (implemented)
- `constcast!` restores mutability only for pointers, optional pointers, and slices while
preserving child type, pointer kind, optionality, length, and sentinel shape
- `std/mem.free` accepts immutable slices and restores mutability only at the allocator boundary
## A word on unchecked casts
For casts that bypass safety checks, Honey provides builtin functions:
@@ -968,7 +973,8 @@ For casts that bypass safety checks, Honey provides builtin functions:
| -- | -- | -- |
| `truncate(x, T)` | Keep low bits, discard rest | Never |
| `bitcast(x, T)` | Reinterpret bits, no cast | Sizes don't match (compile error) |
| `ptrcast!(p, T)` | Change pointer type | Gaining mutability (compile error) |
| `ptrcast!(T, p)` | Change a pointer's child type while preserving its shape | Invalid child or non-pointer operand (compile error) |
| `constcast!(p)` | Restore pointer or slice mutability | Non-pointer/slice operand (compile error) |
```honey
# truncation
@@ -981,12 +987,10 @@ m := bitcast(n, u32) # m == 0xFFFFFFFF (same bits)
f: f32 = 3.14
bits := bitcast(f, u32) # IEEE 754 representation
# pointer casts (element type, many ↔ single, pointer ↔ usize)
buf: *u8 = get_buffer()
ints := ptrcast!(buf, *u32) # element type change
single := ptrcast!(buf, @u8) # many → single (restricting)
addr := ptrcast!(buf, usize) # pointer to integer
ptr := ptrcast!(addr, @u8) # integer to pointer
# pointer casts
buf *u8 = get_buffer()
ints *u32 = ptrcast!(u32, buf) # element type change, same pointer shape
writable *mut u8 = constcast!(buf) # explicit unsafe mutability restoration
```
## A word on multi-unwrap