fix: resolve distinct type factory backings

This commit is contained in:
2026-09-19 23:10:16 +02:00
parent 378eb637c6
commit 5dac5baa05
3 changed files with 94 additions and 5 deletions
+70
View File
@@ -5442,6 +5442,76 @@ main func() void {
testing.expect(t, unrecoverable >= 3)
}
@(test)
distinct_type_factory_backings_reject_generic_calls_without_stalling :: proc(t: ^testing.T) {
text := `ArrayList func($T type) type {
return struct { items []mut T }
}
append func($T type, list @mut ArrayList(T), value T) void {
_ = list
_ = value
}
Item :: struct { value i32 }
Items :: distinct ArrayList(Item)
main func() void {
items Items := undefined
append(&items, Item { value = 1 })
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
ast_module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
defer hir.destroy_module(&hir_module)
found_mismatch := false
found_backing_error := false
found_comptime_error := false
for diagnostic in diagnostics.items {
message := diagnostic.message
found_mismatch = found_mismatch || strings.contains(message, "cannot implicitly convert")
found_backing_error = found_backing_error || strings.contains(message, "requires a concrete runtime backing type")
found_comptime_error = found_comptime_error || strings.contains(message, "invalid comptime argument")
}
testing.expect(t, found_mismatch)
testing.expect(t, !found_backing_error)
testing.expect(t, !found_comptime_error)
}
@(test)
distinct_type_factory_backings_expose_record_fields :: proc(t: ^testing.T) {
directory := "/tmp/brolang-test-distinct-factory-fields"
main_path := "/tmp/brolang-test-distinct-factory-fields/main.bro"
output := "/tmp/brolang-test-distinct-factory-fields-output"
_ = os2.remove_all(directory)
defer _ = os2.remove_all(directory)
defer _ = os.remove(output)
testing.expect(t, os2.make_directory_all(directory) == nil)
text := `Box func($T type) type {
return struct { value T }
}
IntBox :: distinct Box(i32)
increment func(box @mut IntBox) void {
box.value += 1
}
main func() i32 {
box IntBox := IntBox(Box(i32) { value = 41 })
increment(&box)
return box.value - 42
}
`
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)
string_literals_are_contextual_comptime_type_inference_evidence :: proc(t: ^testing.T) {
text := `pair func($T type, expected, actual T) T {