refactor: clarify diagnostics and adopt ptrcast inference

This commit is contained in:
2026-09-19 23:16:18 +02:00
parent 8bb539359b
commit 438f660e76
7 changed files with 27 additions and 28 deletions
+10 -10
View File
@@ -21,30 +21,30 @@ Diagnostic :: struct {
span Span span Span
} }
Store :: struct { items arraylist.ArrayList(Diagnostic) } Store :: struct { list arraylist.ArrayList(Diagnostic) }
CapacityError :: enum { diagnostic_limit_reached } CapacityError :: enum { diagnostic_limit_reached }
init proc(allocator mem.Allocator) Store { init proc(allocator mem.Allocator) Store {
return Store{ items = arraylist.init(allocator) } return Store{ list = arraylist.init(allocator) }
} }
deinit proc(store @mut Store) void { deinit proc(store @mut Store) void {
arraylist.deinit(&store.items) arraylist.deinit(&store.list)
} }
add proc(store @mut Store, code Code, span Span) DiagnosticId ! (mem.AllocError | CapacityError) { add proc(store @mut Store, code Code, span Span) DiagnosticId ! (mem.AllocError | CapacityError) {
if (store.items.items.len >= usize(NO_DIAGNOSTIC)) return .diagnostic_limit_reached if (store.list.items.len >= usize(NO_DIAGNOSTIC)) return .diagnostic_limit_reached
id :: DiagnosticId(store.items.items.len) id :: DiagnosticId(store.list.items.len)
try arraylist.append(&store.items, Diagnostic{ code = code, span = span }) try arraylist.append(&store.list, Diagnostic{ code = code, span = span })
return id return id
} }
get proc(store @Store, id DiagnosticId) ?Diagnostic { get proc(store @Store, id DiagnosticId) ?Diagnostic {
if (usize(id) >= store.items.items.len) return null if (usize(id) >= store.list.items.len) return null
return store.items.items[usize(id)] return store.list.items[usize(id)]
} }
len proc(store @Store) usize { count proc(store @Store) usize {
return store.items.items.len return store.list.items.len
} }
+1 -1
View File
@@ -22,7 +22,7 @@ reports_invalid_input_and_keeps_scanning test {
try testing.expect_equal(TokenKind.float, state.tokens.items[5].kind) try testing.expect_equal(TokenKind.float, state.tokens.items[5].kind)
try testing.expect_equal(TokenKind.string, state.tokens.items[6].kind) try testing.expect_equal(TokenKind.string, state.tokens.items[6].kind)
try testing.expect_equal(TokenKind.eof, state.tokens.items[7].kind) try testing.expect_equal(TokenKind.eof, state.tokens.items[7].kind)
try testing.expect_equal(2, diagnostics.len(&diagnostic_store)) try testing.expect_equal(2, diagnostics.count(&diagnostic_store))
try testing.expect_equal("name", strpool.get_str(&strpool.STRINGS, state.tokens.items[1].data.str_id)?) try testing.expect_equal("name", strpool.get_str(&strpool.STRINGS, state.tokens.items[1].data.str_id)?)
try testing.expect_equal(u32(3), state.tokens.items[1].span.start) try testing.expect_equal(u32(3), state.tokens.items[1].span.start)
try testing.expect_equal(u32(7), state.tokens.items[1].span.end) try testing.expect_equal(u32(7), state.tokens.items[1].span.end)
+1 -1
View File
@@ -59,5 +59,5 @@ main proc() void! {
try ast_renderer.render(&renderer, &tree) try ast_renderer.render(&renderer, &tree)
debug.print("{s}", {renderer.bytes.items}) debug.print("{s}", {renderer.bytes.items})
debug.print("diagnostics: {}\n", {diagnostics.len(&diagnostic_store)}) debug.print("diagnostics: {}\n", {diagnostics.count(&diagnostic_store)})
} }
+2 -2
View File
@@ -30,7 +30,7 @@ parses_expression_precedence_through_tree_views test {
try testing.expect_equal(ast.ExprKind.binary, ast.expr_kind(&tree, add.right)) try testing.expect_equal(ast.ExprKind.binary, ast.expr_kind(&tree, add.right))
multiply :: ast.binary_expr(&tree, add.right) multiply :: ast.binary_expr(&tree, add.right)
try testing.expect_equal(ast.BinaryOperator.multiply, multiply.operator) try testing.expect_equal(ast.BinaryOperator.multiply, multiply.operator)
try testing.expect_equal(0, diagnostics.len(&diagnostic_store)) try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
} }
parses_function_structure_through_tree_views test { parses_function_structure_through_tree_views test {
@@ -63,7 +63,7 @@ parses_function_structure_through_tree_views test {
try testing.expect_equal(first.type, second.type) try testing.expect_equal(first.type, second.type)
try testing.expect(first.type != third.type) try testing.expect(first.type != third.type)
try testing.expect_equal(1, ast.block_statement_count(&tree, function.body)) try testing.expect_equal(1, ast.block_statement_count(&tree, function.body))
try testing.expect_equal(0, diagnostics.len(&diagnostic_store)) try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
} }
recovers_invalid_declaration_and_preserves_sibling test { recovers_invalid_declaration_and_preserves_sibling test {
+1 -1
View File
@@ -84,7 +84,7 @@ writer proc(file File) Writer {
.read_write: flags = c.O_RDWR .read_write: flags = c.O_RDWR
} }
while true { 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 } if (fd >= 0) return Handle{ file_desc = fd }
if (c.__error()?^ != c.EINTR) return .open_failed if (c.__error()?^ != c.EINTR) return .open_failed
} }
+5 -5
View File
@@ -336,7 +336,7 @@ format_field_name proc($T type, index usize) []u8 {
@hide:file @hide:file
distinct_value proc($Backing, $Distinct type, value Distinct) Backing { distinct_value proc($Backing, $Distinct type, value Distinct) Backing {
return ptrcast!(Backing, &value)^ return ptrcast!(@Backing, &value)^
} }
@hide:file @hide:file
@@ -375,12 +375,12 @@ write_float proc(output Writer, $T type, value T, scientific bool) void ! WriteE
buffer [64]mut u8 := undefined buffer [64]mut u8 := undefined
count := 0 count := 0
if sizeof!(T) == 4 { if sizeof!(T) == 4 {
if (scientific) count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.8e", value) if (scientific) 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) else count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.9g", value)
} else if scientific { } 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 { } 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 if (count < 0 or usize(count) >= buffer.len) return .write_failed
try write_all(output, buffer[0..usize(count)]) try write_all(output, buffer[0..usize(count)])
+7 -8
View File
@@ -48,7 +48,7 @@ alloc proc($T type, allocator Allocator, count usize) []mut T ! AllocError {
memory := raw_alloc(allocator, count * element_size, alignof!(T)) memory := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| { if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes) pointer *mut T :: ptrcast!(bytes)
return pointer[..count] return pointer[..count]
} }
@@ -75,7 +75,7 @@ realloc proc($T type, allocator Allocator, memory []mut T, new_count usize) []mu
old_memory ?*mut u8 := null old_memory ?*mut u8 := null
old_size := 0 old_size := 0
if memory.len != 0 { if memory.len != 0 {
old_memory = ptrcast!(u8, memory.ptr) old_memory = ptrcast!(memory.ptr)
old_size = memory.len * element_size old_size = memory.len * element_size
} }
@@ -88,7 +88,7 @@ realloc proc($T type, allocator Allocator, memory []mut T, new_count usize) []mu
) )
if resized |bytes| { if resized |bytes| {
pointer *mut T :: ptrcast!(T, bytes) pointer *mut T :: ptrcast!(bytes)
return pointer[..new_count] return pointer[..new_count]
} }
@@ -100,7 +100,6 @@ realloc proc($T type, allocator Allocator, memory []mut T, new_count usize) []mu
free proc($T type, allocator Allocator, memory []T) void { free proc($T type, allocator Allocator, memory []T) void {
if (memory.len == 0 or sizeof!(T) == 0) return if (memory.len == 0 or sizeof!(T) == 0) return
raw_free(allocator, ptrcast!( raw_free(allocator, ptrcast!(
u8,
constcast!(memory).ptr), constcast!(memory).ptr),
memory.len * sizeof!(T), memory.len * sizeof!(T),
alignof!(T), alignof!(T),
@@ -112,7 +111,7 @@ free proc($T type, allocator Allocator, memory []T) void {
#! exceeds its backing storage. #! exceeds its backing storage.
@hide:file @hide:file
empty_slice proc($T type, count usize) []mut T { empty_slice proc($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr) pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count] return pointer[..count]
} }
@@ -142,13 +141,13 @@ power_of_two proc(value usize) bool {
@hide:file @hide:file
c_alloc proc(_ ?@mut anyopaque, size usize, alignment usize) ?*mut u8 { c_alloc proc(_ ?@mut anyopaque, size usize, alignment usize) ?*mut u8 {
if (power_of_two(alignment) == false) return null if (power_of_two(alignment) == false) return null
if (alignment <= malloc_alignment) return ptrcast!(u8, c.malloc(c_ulong(size))) if (alignment <= malloc_alignment) return ptrcast!(c.malloc(c_ulong(size)))
memory [1]mut ?*mut anyopaque := [null] memory [1]mut ?*mut anyopaque := [null]
status := c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size)) status := c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size))
if (status != 0) return null if (status != 0) return null
return ptrcast!(u8, memory[0]) return ptrcast!(memory[0])
} }
@hide:file @hide:file
@@ -168,7 +167,7 @@ c_realloc proc(
if memory |old_memory| { if memory |old_memory| {
if alignment <= malloc_alignment { 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 := c_alloc(null, new_size, alignment) new_memory := c_alloc(null, new_size, alignment)