broaden type inference from context (assignment statements)

This commit is contained in:
2026-06-26 17:47:14 +02:00
parent cfc1b2cb42
commit f9ea044169
3 changed files with 35 additions and 2 deletions
+27
View File
@@ -6603,6 +6603,33 @@ main :: func() void {
testing.expect(t, types.equal(hir_module.globals[0].type, types.U16))
}
@(test)
contextual_inference_flows_through_compound_assignment :: proc(t: ^testing.T) {
text := `main :: func() void {
s :: 5
v u16 = 0
v += s
_ = v
}
`
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)
// `s` is used only as the RHS of `v += s`. The assignment target's type (u16) is
// demanded backward onto `s`, resolving the open constant; without it the compound
// assignment would report "arithmetic requires compatible numeric operands".
testing.expect_value(t, len(diagnostics.items), 0)
}
@(test)
contextual_inference_rejects_local_constant_that_does_not_fit :: proc(t: ^testing.T) {
text := `main :: func() void {