conditionally unwrapping while loops

This commit is contained in:
2026-08-11 18:33:54 +02:00
parent ba6052eef3
commit fa53ca2219
8 changed files with 486 additions and 307 deletions
+33
View File
@@ -11514,6 +11514,39 @@ braceless_while_compiles_and_runs :: proc(t: ^testing.T) {
testing.expect_value(t, state.exit_code, 42)
}
@(test)
while_unwrap_compiles_and_runs_at_runtime_and_comptime :: proc(t: ^testing.T) {
directory := "/tmp/brolang-test-while-unwrap"
main_path := "/tmp/brolang-test-while-unwrap/main.bro"
output := "/tmp/brolang-test-while-unwrap-output"
text := `next func(index @mut usize, limit usize) ?i32 {
if (index^ == limit) return null
index^ += 1
return i32(index^)
}
sum func(limit, cap usize) i32 {
index usize := 0
total i32 := 0
while next(&index, limit) |value : usize(value) <= cap| : total += value {}
return total
}
COMPTIME_SUM :: $sum(4, 4)
COMPTIME_GUARDED :: $sum(5, 3)
main func() i32 {
if COMPTIME_SUM != 10 or COMPTIME_GUARDED != 6 { return 1 }
return sum(4, 4) + sum(5, 3) - 16
}
`
_ = os2.remove_all(directory)
defer _ = os2.remove_all(directory)
defer _ = os.remove(output)
testing.expect(t, os.make_directory(directory) == nil)
testing.expect(t, os.write_entire_file(main_path, transmute([]byte)text))
testing.expect_value(t, compiler_core.compile_package(directory, output), 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
parser_diagnoses_braceless_for_and_unwrap_without_parens_or_call :: proc(t: ^testing.T) {
text := `main func() void {