Commit 6b9d39bc

6b9d39bc23e3Merged

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

Description

Nullable integers via the SP_INT_NIL sentinel

A nullable int (an int-valued hash miss, String#index miss, ...) is kept as a plain mrb_int carrying the SP_INT_NIL sentinel rather than widening to a boxed poly, so arithmetic on the payload still lowers directly.

The nil-aware operators are now sentinel-aware on TY_INT:

  • x.nil? / x == nil / x != nil -> compares against SP_INT_NIL (a plain int is never the sentinel, so 5.nil? constant-folds to false)
  • x || fallback -> the sentinel reads falsy (0 stays truthy)
  • inspect / to_s / puts / p -> render nil ("nil" / "" / empty line) at the sentinel String#index now returns the sentinel on a miss (sp_str_index_opt). With the sentinel-aware nil?, a return -1 if h.nil? guard then narrows the value to a usable int with no flow analysis, since int and nullable-int share the same C representation.

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

Details

Commit
6b9d39bc23e3
Parent
59bbbe93decc

src/analyze.cmodifiedView file

@@ -655,6 +655,7 @@
655655 return ty_is_numeric(rt) ? rt : TY_UNKNOWN;
656656 if (!strcmp(name, "!")) return TY_BOOL;
657657 if (!strcmp(name, "respond_to?") && recv >= 0) return TY_BOOL;
658+ if (!strcmp(name, "nil?") && recv >= 0 && argc == 0) return TY_BOOL;
658659 if ((!strcmp(name, "match?") || !strcmp(name, "!~")) && recv >= 0) return TY_BOOL;
659660 if (!strcmp(name, "=~") && recv >= 0 && argc == 1) {
660661 const char *rrt = nt_type(nt, recv), *art = nt_type(nt, argv[0]);