structs as errors in fallibles

This commit is contained in:
2026-08-08 22:49:31 +02:00
parent e0f1d2d7cb
commit 729488e702
5 changed files with 80 additions and 5 deletions
+45
View File
@@ -6953,6 +6953,51 @@ main func() i32 {
testing.expect_value(t, state.exit_code, 0)
}
@(test)
struct_error_channels_compile_and_run_at_runtime_and_comptime :: proc(t: ^testing.T) {
text := `ScanError :: struct {
code i32
start usize
end usize
}
scan func(fail bool) i32 ! ScanError {
if fail {
return ScanError { code = 5, start = 7, end = 11 }
}
return 41
}
scan_local func() i32 ! ScanError {
err ScanError := ScanError { code = 3, start = 13, end = 17 }
return err
}
forward func(fail bool) i32 ! ScanError {
return try scan(fail)
}
score_comptime func() i32 {
return scan(true) catch |err| err.code + i32(err.start) + i32(err.end)
}
main func() i32 {
direct :: scan(true) catch |err| err.code + i32(err.start) + i32(err.end)
local :: scan_local() catch |err| err.code + i32(err.start) + i32(err.end)
forwarded :: forward(true) catch |err| err.code + i32(err.start) + i32(err.end)
success :: forward(false) catch 0
comptime_score i32 :: $score_comptime()
return direct + local + forwarded + success + comptime_score - 143
}
`
directory := "/tmp/brolang-test-struct-error-channel"
main_path := "/tmp/brolang-test-struct-error-channel/main.bro"
output := "/tmp/brolang-test-struct-error-channel-output"
_ = 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)
matched_error_residuals_return_at_runtime_and_comptime :: proc(t: ^testing.T) {
text := `KeyError :: enum { key_exists }