inferred leading comptime params

This commit is contained in:
2026-07-12 17:02:12 +02:00
parent 0706188b98
commit 2dea7711f2
11 changed files with 769 additions and 84 deletions
+11 -11
View File
@@ -25,13 +25,13 @@ _fail_allocator mem.Allocator :: mem.Allocator {
_noop func() void {}
run func() i32 ! mem.AllocError {
values arraylist.ArrayList(i32) = arraylist.init(i32, mem.c_allocator)
defer arraylist.deinit(i32, &values)
values arraylist.ArrayList(i32) = arraylist.init(mem.c_allocator)
defer arraylist.deinit(&values)
if (values.items.len != 0 or values.capacity != 0) return 1
i usize = 0
while i < 20 : i += 1 {
arraylist.append(i32, &values, i32(i)) catch |_| {
arraylist.append(&values, i32(i)) catch |_| {
return .out_of_memory
}
}
@@ -40,34 +40,34 @@ run func() i32 ! mem.AllocError {
values.items[3] = 33
if (values.items[3] != 33) return 4
arraylist.reserve(i32, &values, 50) catch |_| {
arraylist.reserve(&values, 50) catch |_| {
return .out_of_memory
}
if (values.capacity < 50 or values.items.len != 20 or values.items[19] != 19) return 5
capacity usize :: values.capacity
arraylist.clear(i32, &values)
arraylist.clear(&values)
if (values.items.len != 0 or values.capacity != capacity) return 6
arraylist.append(i32, &values, 7) catch |_| {
arraylist.append(&values, 7) catch |_| {
return .out_of_memory
}
if (values.items.len != 1 or values.items[0] != 7 or values.capacity != capacity) return 7
empty_values arraylist.ArrayList([0]u8) = arraylist.init([0]u8, mem.c_allocator)
defer arraylist.deinit([0]u8, &empty_values)
empty_values arraylist.ArrayList([0]u8) = arraylist.init(mem.c_allocator)
defer arraylist.deinit(&empty_values)
zero [0]u8 :: []
arraylist.append([0]u8, &empty_values, zero) catch |_| {
arraylist.append(&empty_values, zero) catch |_| {
return .out_of_memory
}
if (empty_values.items.len != 1) return 8
failed arraylist.ArrayList(i32) = arraylist.init(i32, _fail_allocator)
failed_as_expected bool = false
arraylist.append(i32, &failed, 1) catch |_| {
arraylist.append(&failed, 1) catch |_| {
failed_as_expected = true
yield _noop()
}
if (failed_as_expected == false or failed.items.len != 0 or failed.capacity != 0) return 9
arraylist.deinit(i32, &failed)
arraylist.deinit(&failed)
return 0
}
@@ -19,10 +19,37 @@ buffer func($T type, $N usize, value T) [N]T {
return data
}
zero func($T type) T {
value T = undefined
return value
}
array_len func($T type, $N usize, values [N]T) usize {
return values.len
}
Fixed func($T type, $N usize) type {
return struct {
values [N]T
}
}
fixed_len func($T type, $N usize, value @Fixed(T, N)) usize {
return value.values.len
}
take_i32 func(value i32) i32 {
return value
}
return_zero func() i32 {
return zero()
}
main func() i32 {
a i32 :: 42
b i32 :: 27
if max(i32, a, b) != 42 {
if max(a, b) != 42 {
return 1
}
@@ -33,14 +60,32 @@ main func() i32 {
}
p Point :: Point { x = 11 }
q Point :: id(Point, p)
q Point :: id(p)
if q.x != 11 {
return 3
}
bytes [_]u8 :: buffer(u8, 4, small_a)
bytes [4]u8 :: buffer(small_a)
if bytes.len != 4 {
return 4
}
if array_len(bytes) != 4 {
return 5
}
zero_value i32 :: zero()
_ = zero_value
literal :: id(7)
if literal != 7 {
return 6
}
fixed Fixed(u8, 3) :: Fixed(u8, 3) { values = [1, 2, 3] }
if fixed_len(&fixed) != 3 {
return 7
}
assigned i32 = 1
assigned = zero()
_ = assigned
_ = take_i32(zero())
_ = return_zero()
return 0
}
@@ -23,7 +23,7 @@ task_list_init func(allocator mem.Allocator) TaskList {
alloc_i32s func(allocator mem.Allocator, count usize) ?[]mut i32 {
fallback [1]mut i32 = undefined
failed bool = false
values []mut i32 = mem.alloc(i32, allocator, count) catch |_| {
values []mut i32 = mem.alloc(allocator, count) catch |_| {
failed = true
yield (&fallback).ptr[..0]
}
@@ -33,7 +33,7 @@ alloc_i32s func(allocator mem.Allocator, count usize) ?[]mut i32 {
free_i32s func(allocator mem.Allocator, values ?[]mut i32) void {
if values |slice| {
mem.free(i32, allocator, slice)
mem.free(allocator, slice)
}
}
@@ -49,12 +49,12 @@ typed_allocator_test func() i32 {
i32_fallback [1]mut i32 = undefined
empty_failed bool = false
empty []mut i32 = mem.alloc(i32, first_allocator, 0) catch |_| {
empty []mut i32 = mem.alloc(first_allocator, 0) catch |_| {
empty_failed = true
yield (&i32_fallback).ptr[..0]
}
if (empty_failed or empty.len != 0 or first_calls[0] != 3) return 34
mem.free(i32, first_allocator, empty)
mem.free(first_allocator, empty)
if (first_calls[0] != 3) return 33
zero_sized_fallback [1]mut [0]u8 = undefined
@@ -70,7 +70,7 @@ typed_allocator_test func() i32 {
u64_fallback [1]mut u64 = undefined
overflow_fallback_failed bool = false
overflow_fallback []mut u64 = mem.alloc(u64, first_allocator, 0) catch |_| {
overflow_fallback []mut u64 = mem.alloc(first_allocator, 0) catch |_| {
overflow_fallback_failed = true
yield (&u64_fallback).ptr[..0]
}
@@ -83,20 +83,20 @@ typed_allocator_test func() i32 {
if (overflow_failed == false or first_calls[0] != 3) return 40
typed_failed bool = false
typed []mut i32 = mem.alloc(i32, mem.c_allocator, 4) catch |_| {
typed []mut i32 = mem.alloc(mem.c_allocator, 4) catch |_| {
typed_failed = true
yield (&i32_fallback).ptr[..0]
}
if (typed_failed) return 38
defer mem.free(i32, mem.c_allocator, typed)
defer mem.free(mem.c_allocator, typed)
typed[0] = 10
typed[3] = 20
if (typed[0] + typed[3] != 30) return 39
typed = mem.realloc(i32, mem.c_allocator, typed, 8) catch |_| {
typed = mem.realloc(mem.c_allocator, typed, 8) catch |_| {
return 41
}
if (typed.len != 8 or typed[0] != 10 or typed[3] != 20) return 42
typed = mem.realloc(i32, mem.c_allocator, typed, 2) catch |_| {
typed = mem.realloc(mem.c_allocator, typed, 2) catch |_| {
return 43
}
if (typed.len != 2 or typed[0] != 10) return 44