Commit ff4346fc
ff4346fcad7cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Widen a parameter when its body assigns a conflicting type
A param typed from call sites (int) whose body reassigns a different type (x = "forever") now widens to poly, instead of storing the string in an int slot and printing garbage. Body writes only widen params, never narrow or reset them.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- ff4346fcad7c
- Parent
- 90305354fe5f
src/analyze.cmodifiedView file
@@ -1070,7 +1070,17 @@
10701070
}10711071
if (!nm) continue;10721072
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);1073-
if (!lv || lv->is_param || lv->is_block_param) continue;1073+
if (!lv || lv->is_block_param) continue;1074+
/* Params are typed from call sites (monotonic widen); a body assignment1075+
of a different type widens them too (e.g. `x = "s"` in an int param's1076+
body -> poly). Only widen -- never let an unknown RHS reset them. */1077+
if (lv->is_param) {1078+
if (newt != TY_UNKNOWN) {1079+
TyKind m2 = ty_unify(lv->type, newt);1080+
if (m2 != lv->type) { lv->type = m2; changed = 1; }1081+
}1082+
continue;1083+
}10741084
lv->type = ty_unify(lv->type, newt);10751085
}10761086