Commit ad646248

ad64624868d9Merged

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

Description

spinel-c: Numeric#coerce, block param from Class.new, empty-array ivar guard

  • analyze: Integer#coerce(float) -> float_array, #coerce(int) -> int_array Float#coerce -> float_array; codegen: emit inline compound statement
  • analyze: Class.new { |x| } block params take types from #initialize yields
  • analyze: extend empty-@ivar guard to cover empty [] / {} literal writes, preventing @free[0] read from incorrectly promoting @free to poly_poly_hash

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
ad64624868d9
Parent
c794a9829a78

src/analyze.cmodifiedView file

@@ -1616,9 +1616,14 @@
16161616 if (!strcmp(name, "gcdlcm") && argc == 1) return TY_INT_ARRAY; /* [gcd, lcm] */
16171617 if (!strcmp(name, "digits")) return TY_INT_ARRAY;
16181618 if (!strcmp(name, "to_s") && argc == 1) return TY_STRING;
1619+ if (!strcmp(name, "coerce") && argc == 1) {
1620+ TyKind a0 = infer_type(c, argv[0]);
1621+ return (a0 == TY_FLOAT) ? TY_FLOAT_ARRAY : TY_INT_ARRAY;
1622+ }
16191623 }
16201624 /* float receiver methods */
16211625 if (recv >= 0 && rt == TY_FLOAT) {
1626+ if (!strcmp(name, "coerce") && argc == 1) return TY_FLOAT_ARRAY; /* [Float(other), self] */
16221627 if (!strcmp(name, "divmod") && argc == 1) return TY_POLY_ARRAY; /* [Integer, Float] */
16231628 if (!strcmp(name, "infinite?")) return TY_INT; /* nil / 1 / -1 (nullable int) */
16241629 if (!strcmp(name, "nan?") || !strcmp(name, "finite?") ||
@@ -3712,11 +3717,11 @@
37123717 int iv = inm ? comp_ivar_index(ci, inm) : -1;
37133718 if (iv < 0) continue;
37143719 slot = &ci->ivar_types[iv];
3715- /* If the slot is TY_UNKNOWN but has a direct non-empty InstanceVariableWriteNode
3716- (e.g. @buf = [nil]*7), skip usage-driven promotion — infer_ivar_types will
3717- set the correct type from the direct assignment. Without this guard,
3718- @buf[0] = -1 in the first fixpoint iteration promotes @buf to an int hash
3719- before @buf = [nil]*7 has been processed, causing ty_unify to widen to TY_POLY. */
3720+ /* If the slot is TY_UNKNOWN but has a direct InstanceVariableWriteNode
3721+ that assigns a typed value OR an empty array/hash literal (e.g.
3722+ @buf = [nil]*7 or @free = []), skip usage-driven hash promotion.
3723+ Without this guard, @free[0] read promotes @free to poly_poly_hash
3724+ before @free = [] has been processed as an array. */
37203725 if (*slot == TY_UNKNOWN && inm) {
37213726 int has_typed_write = 0;
37223727 for (int _wi = 0; _wi < nt->count && !has_typed_write; _wi++) {
@@ -3728,7 +3733,12 @@
37283733 int _wval = nt_ref(nt, _wi, "value");
37293734 if (_wval < 0) continue;
37303735 TyKind _wt = infer_type(c, _wval);
3731- if (_wt != TY_UNKNOWN && _wt != TY_NIL) has_typed_write = 1;
3736+ if (_wt != TY_UNKNOWN && _wt != TY_NIL) { has_typed_write = 1; break; }
3737+ /* @ivar = [] or @ivar = {} literal: this slot is an array/hash, not
3738+ subject to hash-promotion from [] read or [0]= write. */
3739+ const char *_wvty = nt_type(nt, _wval);
3740+ if (_wvty && (!strcmp(_wvty, "ArrayNode") || !strcmp(_wvty, "HashNode")))
3741+ has_typed_write = 1;
37323742 }
37333743 if (has_typed_write) continue;
37343744 }
@@ -4517,6 +4527,13 @@
45174527 else {
45184528 TyKind rt0 = infer_type(c, recv);
45194529 if (ty_is_object(rt0)) mi = comp_method_in_chain(c, ty_object_class(rt0), name, NULL);
4530+ /* Class.new { |...| }: the yielding method is Class#initialize */
4531+ if (mi < 0 && !strcmp(name, "new") &&
4532+ nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode")) {
4533+ const char *cname = nt_str(nt, recv, "name");
4534+ int cid = cname ? comp_class_index(c, cname) : -1;
4535+ if (cid >= 0) mi = comp_method_in_chain(c, cid, "initialize", NULL);
4536+ }
45204537 }
45214538 if (mi >= 0 && c->scopes[mi].yields) {
45224539 int yn = first_yield(c, mi);