Commit db12b8a7

db12b8a76fa0Merged

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

Description

Add identity methods (freeze/itself/dup/clone) and Array#each_with_index

  • freeze/itself/dup/clone (no args) -> the receiver itself (value-type no-ops; freeze as a statement is a no-op).
  • array.each_with_index { |x, i| } -> for-loop binding element + int index (block param types inferred).

make test: 24.

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

Details

Commit
db12b8a76fa0
Parent
236f0f17387d

src/analyze.cmodifiedView file

@@ -45,6 +45,12 @@
4545 TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;
4646 TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;
4747
48+ /* identity methods: return the receiver unchanged */
49+ if (recv >= 0 && argc == 0 &&
50+ (!strcmp(name, "freeze") || !strcmp(name, "itself") ||
51+ !strcmp(name, "dup") || !strcmp(name, "clone")))
52+ return rt;
53+
4854 /* Class.new(...) -> an instance of that class */
4955 if (recv >= 0 && !strcmp(name, "new")) {
5056 const char *rty = nt_type(nt, recv);
@@ -889,6 +895,21 @@
889895 ty_is_array(rt))
890896 pt = ty_array_elem(rt);
891897
898+ /* array.each_with_index { |x, i| } binds element + int index */
899+ if (!strcmp(name, "each_with_index") && ty_is_array(rt)) {
900+ Scope *es = comp_scope_of(c, block);
901+ LocalVar *ep = scope_local_intern(es, p0); ep->is_block_param = 1;
902+ TyKind em = ty_unify(ep->type, ty_array_elem(rt));
903+ if (em != ep->type) { ep->type = em; changed = 1; }
904+ const char *p1 = block_param_name(c, block, 1);
905+ if (p1) {
906+ LocalVar *ip = scope_local_intern(es, p1); ip->is_block_param = 1;
907+ TyKind im = ty_unify(ip->type, TY_INT);
908+ if (im != ip->type) { ip->type = im; changed = 1; }
909+ }
910+ continue;
911+ }
912+
892913 /* hash.each / each_pair { |k, v| } binds two params */
893914 if ((!strcmp(name, "each") || !strcmp(name, "each_pair")) && ty_is_hash(rt)) {
894915 Scope *hs = comp_scope_of(c, block);