feat: infer complete ptrcast target types

This commit is contained in:
2026-09-20 00:01:13 +02:00
parent 5dac5baa05
commit 7f7de7a8d2
11 changed files with 122 additions and 65 deletions
+1 -1
View File
@@ -98,7 +98,7 @@ writer func(file File) Writer {
.read_write: flags = c.O_RDWR
}
while true {
fd c_int :: c.open(ptrcast!(c_char, path.ptr), flags)
fd c_int :: c.open(ptrcast!(path.ptr), flags)
if fd >= 0 {
return Handle {file_desc = fd}
}
+5 -5
View File
@@ -317,7 +317,7 @@ print func(output Writer, $format []u8, $Args type, args Args) void ! WriteError
}
}
@hide distinct_value func($Backing, $Distinct type, value Distinct) Backing {
return ptrcast!(Backing, &value)^
return ptrcast!(@Backing, &value)^
}
@hide scalar_or_distinct_type func($T type) bool {
@@ -357,14 +357,14 @@ print func(output Writer, $format []u8, $Args type, args Args) void ! WriteError
count c_int := 0
if sizeof!(T) == 4 {
if scientific {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.8e", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.8e", value)
} else {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.9g", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.9g", value)
}
} else if scientific {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.16e", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.16e", value)
} else {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.17g", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.17g", value)
}
if count < 0 or usize(count) >= buffer.len {
return .write_failed
+7 -8
View File
@@ -48,7 +48,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
memory ?*mut u8 := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..count]
}
return .out_of_memory
@@ -78,7 +78,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
old_memory ?*mut u8 := null
old_size usize := 0
if memory.len != 0 {
old_memory = ptrcast!(u8, memory.ptr)
old_memory = ptrcast!(memory.ptr)
old_size = memory.len * element_size
}
resized ?*mut u8 := raw_realloc(
@@ -89,7 +89,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
alignof!(T),
)
if resized |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..new_count]
}
@@ -101,7 +101,6 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
free func($T type, allocator Allocator, memory []T) void {
if (memory.len == 0 or sizeof!(T) == 0) return
raw_free(allocator, ptrcast!(
u8,
constcast!(memory).ptr),
memory.len * sizeof!(T),
alignof!(T),
@@ -110,7 +109,7 @@ free func($T type, allocator Allocator, memory []T) void {
#! get an empty slice of type `T` with `count` elements.
empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr)
pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count]
}
@@ -138,14 +137,14 @@ empty func($T type) []mut T {
if (power_of_two(alignment) == false) return null
if alignment <= malloc_alignment {
return ptrcast!(u8, c.malloc(c_ulong(size)))
return ptrcast!(c.malloc(c_ulong(size)))
}
memory [1]mut ?*mut anyopaque := [null]
status c_int := c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size))
if (status != 0) return null
return ptrcast!(u8, memory[0])
return ptrcast!(memory[0])
}
@hide c_realloc func(_ ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
@@ -158,7 +157,7 @@ empty func($T type) []mut T {
if memory |old_memory| {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 := c_alloc(null, new_size, alignment)