feat: run existing ArrayList with imported C allocator

This commit is contained in:
2026-10-29 19:31:54 +01:00
parent 026fe36325
commit 73684f856a
20 changed files with 875 additions and 54 deletions
+45 -7
View File
@@ -4349,6 +4349,21 @@ expression_type proc(state @mut State, id ast.NodeId, depth usize) ast.NodeId !
.literal_bool: return try builtin(state, id, .bool)
.literal_string: return try builtin(state, id, .string)
.literal_void: return try builtin(state, id, .void)
.builtin_call: {
name :: ast.literal_text(&state.tree, ast.builtin_call(&state.tree, id).name)
count :: ast.call_arg_count(&state.tree, id)
if ((mem.eql(name, "sizeof") or mem.eql(name, "alignof")) and count == 1)
return try builtin(state, id, .usize)
if ((mem.eql(name, "maxval") or mem.eql(name, "minval")) and count == 1)
return try canonical_type(state, ast.call_arg(&state.tree, id, 0))
if (mem.eql(name, "divtrunc") and count == 2) {
left :: ast.call_arg(&state.tree, id, 0)
right :: ast.call_arg(&state.tree, id, 1)
operand :: if (flexible(state, left)) right else left
return try expression_type(state, operand, depth + 1)
}
return no_node
}
.catch_expr: {
type :: try expression_type(state, ast.catch_expr(&state.tree, id).operand, depth + 1)
if (type == no_node or ast.expr_kind(&state.tree, type) != .fallible_type) return no_node
@@ -4447,6 +4462,25 @@ expression_type proc(state @mut State, id ast.NodeId, depth usize) ast.NodeId !
)
)
return try builtin(state, id, .usize)
if (mem.eql(member, "ptr")) {
element := no_node
mutable := false
if (base_kind == .array_type) {
element = ast.array_type(&state.tree, base).element
mutable = ast.array_type(&state.tree, base).mutable
}
if (base_kind == .slice_type) {
element = ast.slice_type(&state.tree, base).element
mutable = ast.slice_type(&state.tree, base).mutable
}
if (binding(state, base).kind == .builtin and binding(state, base).data.builtin == .string)
element = try builtin(state, id, .u8)
if (element != no_node) {
result :: try ast.add_pointer_type(state.builder, view.span, element, mutable, true)
try refresh(state)
return result
}
}
if (base_kind != .record_type and base_kind != .union_type) return no_node
slot :: type_record_slot(state, base, view.member)
if (slot == no_index) return no_node
@@ -4524,32 +4558,36 @@ infer proc(state @mut State, function, pattern, actual ast.NodeId, arguments []m
resolved :: binding(state, pattern)
if (resolved.kind == .declaration and parameter_index(state, function, resolved.data.declaration) != no_index)
return try infer_value(state, function, pattern, Value{ kind = .type, node = actual }, arguments, contextual)
# only normalize the actual type: the pattern may still contain unbound
# parameters or a type-generator call whose arguments must be inferred.
concrete :: try canonical_type(state, actual)
if (concrete == no_node) return false
kind :: ast.expr_kind(&state.tree, pattern)
actual_kind :: ast.expr_kind(&state.tree, actual)
actual_kind :: ast.expr_kind(&state.tree, concrete)
match kind {
.pointer_type: {
if (actual_kind != .pointer_type) return false
return try infer(state, function, ast.pointer_type(&state.tree, pattern).element,
ast.pointer_type(&state.tree, actual).element, arguments, contextual)
ast.pointer_type(&state.tree, concrete).element, arguments, contextual)
}
.slice_type: {
element := no_node
if (actual_kind == .slice_type) element = ast.slice_type(&state.tree, actual).element
if (actual_kind == .array_type) element = ast.array_type(&state.tree, actual).element
if (actual_kind == .slice_type) element = ast.slice_type(&state.tree, concrete).element
if (actual_kind == .array_type) element = ast.array_type(&state.tree, concrete).element
if (element == no_node) return false
return try infer(state, function, ast.slice_type(&state.tree, pattern).element, element, arguments, contextual)
}
.array_type: {
if (actual_kind != .array_type) return false
p :: ast.array_type(&state.tree, pattern)
a :: ast.array_type(&state.tree, actual)
a :: ast.array_type(&state.tree, concrete)
if (p.count) |count| {
if (a.count == null or !try infer_value(state, function, count, try evaluate(state, a.count?), arguments, false)) return false
}
return try infer(state, function, p.element, a.element, arguments, contextual)
}
.optional_type: {
element :: if (actual_kind == .optional_type) ast.type_operand(&state.tree, actual) else actual
element :: if (actual_kind == .optional_type) ast.type_operand(&state.tree, concrete) else concrete
return try infer(state, function, ast.type_operand(&state.tree, pattern), element, arguments, contextual)
}
.call: {
@@ -4558,7 +4596,7 @@ infer proc(state @mut State, function, pattern, actual ast.NodeId, arguments []m
for (0..(state.instances.items.len)) |i| {
instance :: state.instances.items[i]
if (instance.type == no_node or instance.function != callee.data.declaration) continue
if (try canonical_type(state, instance.type) != actual) continue
if (try canonical_type(state, instance.type) != concrete) continue
count :: ast.call_arg_count(&state.tree, pattern)
if (count != instance.arguments.len) return false
for (0..count) |j| {
+129
View File
@@ -0,0 +1,129 @@
import "@std/mem"
import "@source/ast"
import "@source/diagnostics"
import "@source/strpool"
sema :: import "@source/sema"
typecheck :: import "@source/sema/check"
import "@std/testing"
import "@source/lexer"
import "@source/parser"
@hide:file
expect_specialization proc(
source []u8, expected diagnostics.Code, expected_count usize,
) void ! (mem.AllocError | diagnostics.CapacityError | strpool.InternError | testing.Error) {
strings := strpool.init(mem.c_allocator)
defer strpool.deinit(&strings)
diagnostic_store := diagnostics.init(mem.c_allocator)
defer diagnostics.deinit(&diagnostic_store)
scan_state := lexer.init(mem.c_allocator)
defer lexer.deinit(&scan_state)
try lexer.scan(&scan_state, source, &strings, &diagnostic_store)
builder := ast.init_builder(mem.c_allocator, source)
defer ast.deinit_builder(&builder)
root :: try parser.parse_source(&builder, scan_state.tokens.items, &diagnostic_store)
tree := ast.view(&builder, root)
resolution := try sema.resolve(mem.c_allocator, &tree, &strings, &diagnostic_store)
defer sema.deinit(&resolution)
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
elaborated := try elaborate(mem.c_allocator, &builder, root, &resolution, &strings, &diagnostic_store)
defer deinit(&elaborated)
tree = ast.view(&builder, root)
checked := try typecheck.check_elaborated(mem.c_allocator, &tree, &resolution, elaborated.constraints.items, &diagnostic_store)
defer typecheck.deinit(&checked)
if (expected_count == 0) {
try testing.expect_equal(0, diagnostics.count(&diagnostic_store))
for (0..ast.package_decl_count(&tree)) |i| {
declaration :: ast.package_decl(&tree, i)
if (ast.node_class(&tree, declaration) != .function) continue
try testing.expect(!ast.function_generic(&tree, declaration))
for (0..ast.function_param_count(&tree, declaration)) |j|
try testing.expect(!ast.param(&tree, ast.function_param(&tree, declaration, j)).comptime)
}
} else {
count := usize(0)
for (diagnostic_store.list.items) |diagnostic| {
if (diagnostic.code == expected) count += 1
}
try testing.expect_equal(expected_count, count)
}
}
infers_nested_nominal_pointer_parameters_and_readonly_slice_views test {
try expect_specialization(
`Storage proc($T type) type { return struct { items []mut T, capacity usize } }
`make proc($T type, values []mut T) Storage(T) {
` return Storage(T) { items = values, capacity = values.len }
`}
`observe proc($T type, values []T) usize { return values.len }
`reserve proc($T type, storage @mut Storage(T), count usize) void {
` _ = observe(storage.items.ptr[..count])
`}
`append proc($T type, storage @mut Storage(T), value T) void {
` reserve(storage, storage.items.len)
` storage.items[0] = value
`}
`release proc($T type, storage @mut Storage(T)) void {
` allocation :: storage.items.ptr[..storage.capacity]
` _ = observe(allocation)
`}
`forward proc(storage @mut Storage(i16)) void {
` append(storage, 7)
` release(storage)
`}
`main proc() i16 {
` values [2]mut i16 := [1, 2]
` storage := make(i16, values[..])
` forward(&storage)
` append(&storage, 9)
` release(&storage)
` return storage.items[0]
`}
, .cannot_infer_generic, 0,
)
}
rejects_conflicting_rigid_types_and_unrelated_nominal_generators test {
try expect_specialization(
`same proc($T type, left, right T) void {}
`main proc() void { same(i16(1), u8(2)) }
, .cannot_infer_generic, 1,
)
try expect_specialization(
`Box proc($T type) type { return struct { value T } }
`Other proc($T type) type { return struct { value T } }
`read proc($T type, value @Box(T)) void {}
`main proc() void {
` value Other(i16) :: { value = 1 }
` read(&value)
`}
, .cannot_infer_generic, 1,
)
try expect_specialization(
`unknown proc($T type, value usize) void {}
`main proc() void { unknown(1) }
, .cannot_infer_generic, 1,
)
}
preserves_rigid_inference_before_contextual_literals test {
try expect_specialization(
`same proc($T type, left, right T) T { return right }
`main proc() i16 { return same(i16(1), 2) }
, .cannot_infer_generic, 0,
)
}
infers_integer_builtin_results_from_their_type_or_rigid_operand test {
try expect_specialization(
`same proc($T type, left, right T) T { return right }
`main proc() i16 {
` upper :: same(maxval!(i16), i16(1))
` lower :: same(minval!(i16), i16(2))
` left :: same(divtrunc!(i16(8), 2), i16(3))
` right :: same(divtrunc!(8, i16(2)), i16(4))
` return upper + lower + left + right
`}
, .cannot_infer_generic, 0,
)
}