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
+7 -1
View File
@@ -1727,7 +1727,7 @@ infer_statements :: proc(
case .Assignment:
value_type := infer_expr(checker, statement.expr, locals^[:], pkg, file, demanded, local_types)
if statement.target != ast.INVALID_EXPR {
_ = infer_expr(checker, statement.target, locals^[:], pkg, file, demanded, local_types)
target_type := infer_expr(checker, statement.target, locals^[:], pkg, file, demanded, local_types)
target_expr := checker.ast_module.exprs[statement.target]
if target_expr.kind == .Name && !symbol.is_valid(target_expr.qualifier) {
if local_index, ok := find_infer_local_index(locals^[:], target_expr.name); ok &&
@@ -1735,10 +1735,16 @@ infer_statements :: proc(
_ = merge_infer_local_type(checker, &locals^[local_index], value_type, local_types)
}
}
// Push the target's concrete type backward onto the RHS so a const used
// only in an assignment (e.g. `x += speed`) resolves, mirroring how
// declarations and returns demand their context (record_demand self-gates
// on a concrete demand and only touches fitting open slots).
_ = record_demand(checker, statement.expr, target_type, locals^[:], local_types, pkg, file)
} else if statement.name != checker.sink_symbol {
if local_index, ok := find_infer_local_index(locals^[:], statement.name); ok &&
locals^[local_index].mutable {
_ = merge_infer_local_type(checker, &locals^[local_index], value_type, local_types)
_ = record_demand(checker, statement.expr, locals^[local_index].type, locals^[:], local_types, pkg, file)
}
}
case .Expression: