fix(checker): infer return match calls

This commit is contained in:
2026-08-01 23:33:52 +02:00
parent f267e8c3cb
commit 91aa601464
2 changed files with 39 additions and 0 deletions
+4
View File
@@ -5977,6 +5977,10 @@ infer_statements :: proc(
_ = infer_expr(checker, statement.expr, locals^[:], pkg, file, demanded, local_types)
}
case .Return:
if statement.value_control_flow {
infer_statements(checker, statement.body, locals, local_types, pkg, file, demanded, result, result_hint)
continue
}
if statement.expr != ast.INVALID_EXPR {
returned := infer_expr(checker, statement.expr, locals^[:], pkg, file, demanded, local_types, result_hint)
if is_runtime_type(checker, result_hint) {
+35
View File
@@ -6367,6 +6367,41 @@ main func() i32 {
testing.expect(t, len(hir_module.functions) > 1)
}
@(test)
return_match_inference_visits_arm_calls :: proc(t: ^testing.T) {
text := `Kind :: enum {
a
b
}
identity func(value i32) i32 {
return value
}
choose func(kind Kind) i32 {
return match kind {
.a: identity(41)
.b: identity(42)
}
}
main func() i32 {
return choose(.a) - 41
}
`
source_file := source.Source{path="return_match_call.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)
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect(t, len(hir_module.functions) > 2)
}
@(test)
fallible_ergonomics_rejects_bad_try_and_catch_blocks :: proc(t: ^testing.T) {
text := `A :: enum {