Commit 86f0ecad

86f0ecad079fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Type poly << as poly so shift and append stay consistent

x << y on a poly receiver is ambiguous: Integer#<< (bit shift) vs Array#push (append). The runtime sp_poly_shl already dispatches on the value's tag and returns a boxed result either way -- a boxed int for a shift, the array for an append -- so the static type is simply poly. A downstream bitwise op coerces the boxed int back via sp_poly_to_i, and an append keeps its (boxed) array.

Previously inference left poly << unknown, so (@data << 1) & 0xff (with a poly-typed register) failed to type the & receiver. Typing it poly and adding the expression-context sp_poly_shl emit fixes that without disturbing the append form, which already returns poly.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
86f0ecad079f
Parent
bfc8ed7bf537

src/analyze_infer.cmodifiedView file

@@ -1785,12 +1785,16 @@
17851785 (!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^") ||
17861786 !strcmp(name, "<<") || !strcmp(name, ">>")))
17871787 return TY_INT;
1788- /* poly recv bitwise op: result is int (sp_poly_to_i applied). `<<` is left out
1789- because it is ambiguous on a poly (Integer#<< shift vs Array#push append),
1790- and the append form must keep its array result type. */
1788+ /* poly recv bitwise op: result is int (sp_poly_to_i applied). */
17911789 if (recv >= 0 && argc == 1 && rt == TY_POLY &&
17921790 (!strcmp(name, ">>") || !strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^")))
17931791 return TY_INT;
1792+ /* poly recv `<<` is ambiguous (Integer#<< shift vs Array#push append); the
1793+ runtime sp_poly_shl dispatches on the tag and returns a boxed result either
1794+ way, so the static type is poly. A downstream bitwise op coerces it back to
1795+ int, and an append keeps its (boxed) array -- both stay consistent. */
1796+ if (recv >= 0 && argc == 1 && rt == TY_POLY && !strcmp(name, "<<"))
1797+ return TY_POLY;
17941798 /* boolean &/|/^ */
17951799 if (recv >= 0 && argc == 1 && rt == TY_BOOL &&
17961800 (!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^")))