Commit ef843816

ef8438164a86Merged

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

Description

Add a float? nullable type so FloatArray#first/#last return nil when empty

CRuby's Array#first/#last return nil on an empty array, but a float_array's first/last returned 0.0 (sp_FloatArray_get's OOB value) because the static result type was the non-nullable float. The int? sentinel (SP_INT_NIL) can't be reused -- 0.0 is a valid float -- so this adds the float? sibling the nullable infrastructure was designed for.

  • runtime (sp_types.h): SP_FLOAT_NIL, a reserved quiet-NaN payload, with sp_float_nil() / sp_float_is_nil() (bit-compare, since NaN != NaN). A real Float element with that exact bit pattern reads as nil -- the same documented compromise SP_INT_NIL makes. sp_runtime.h adds sp_float_opt_inspect / _to_s and sp_FloatArray_first_opt / _last_opt (nil when empty; [i] stays non-nullable 0.0).
  • analyze: float_array#first/#last (no-arg) type as float?; is_scalar_nullable_type recognizes float?; a scalar-nullable operand normalizes to its base in infer_operator_type so float? arithmetic yields a float (e.g. a.last - a.first), not the int fallback.
  • codegen: is_scalar_nullable_type recognizes float?; every scalar-nullable site now branches int-vs-float via helpers (scalar_nil_sentinel / _is_nil_call / _opt_inspect_call) so a float? is never silently treated as an int?. inspect / puts / box-to-poly grow float? arms; first/last emit the opt getters.

fa = [1.0, 2.0]; fa.pop; fa.pop fa.first # => nil (was 0.0) [3.0,4.0].last # => 4.0 [10.5,4.5].last - [10.5,4.5].first # => -6.0 (float, not int)

Full suite green (713) and optcarrot checksum unchanged (59662) -- the float? rides as a plain double through optcarrot's @times.last - @times.first timing path, so the hot float arithmetic stays transparent.

Fixes #1093

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
ef8438164a86
Parent
e14cdc29fa26

lib/sp_runtime.hmodifiedView file

@@ -955,6 +955,11 @@
955955 static inline mrb_int sp_FloatArray_length(sp_FloatArray*a){return a->len;}
956956 static inline mrb_bool sp_FloatArray_empty(sp_FloatArray*a){return a->len==0;}
957957 static inline mrb_float sp_FloatArray_get(sp_FloatArray*a,mrb_int i){if(!a)return 0.0;if(i<0)i+=a->len;if(i<0||i>=a->len)return 0.0;return a->data[i];}
958+/* first/last as float? : nil (sentinel) when empty, else the element.
959+ `[i]` stays non-nullable (0.0 for OOB) -- only first/last produce nil,
960+ matching CRuby where Array#first/#last on an empty array return nil. */
961+static inline mrb_float sp_FloatArray_first_opt(sp_FloatArray*a){return (!a||a->len<=0)?sp_float_nil():sp_FloatArray_get(a,0);}
962+static inline mrb_float sp_FloatArray_last_opt(sp_FloatArray*a){return (!a||a->len<=0)?sp_float_nil():sp_FloatArray_get(a,a->len-1);}
958963 /* a[start, len] / a[start..end] for FloatArray. Same negative-start
959964 * and length-clamping semantics as sp_IntArray_slice. */
960965 static sp_FloatArray*sp_FloatArray_slice(sp_FloatArray*a,mrb_int start,mrb_int len){if(start<0)start+=a->len;if(start<0)start=0;sp_FloatArray*b=sp_FloatArray_new();if(start>=a->len||len<=0)return b;if(start+len>a->len)len=a->len-start;if(len>b->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)b-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_float)*b->cap;h->size-=sizeof(mrb_float)*b->cap;b->cap=len;b->data=(mrb_float*)realloc(b->data,sizeof(mrb_float)*b->cap);h->size+=sizeof(mrb_float)*b->cap;sp_gc_bytes+=sizeof(mrb_float)*b->cap;}memcpy(b->data,a->data+start,sizeof(mrb_float)*len);b->len=len;return b;}
@@ -2372,6 +2377,11 @@
23722377 form). Two wrappers keep call-site emit local. */
23732378 static const char *sp_int_opt_inspect(mrb_int v) { return sp_int_is_nil(v) ? "nil" : sp_int_to_s(v); }
23742379 static const char *sp_int_opt_to_s(mrb_int v) { return sp_int_is_nil(v) ? "" : sp_int_to_s(v); }
2380+/* float? (nullable float) counterparts: a non-nil value formats exactly
2381+ like a plain Float (delegates to sp_float_to_s), nil renders "nil"
2382+ (inspect) / "" (to_s). */
2383+static const char *sp_float_opt_inspect(mrb_float v) { return sp_float_is_nil(v) ? "nil" : sp_float_to_s(v); }
2384+static const char *sp_float_opt_to_s(mrb_float v) { return sp_float_is_nil(v) ? "" : sp_float_to_s(v); }
23752385 /* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union
23762386 (max 8 bytes). When a Range crosses into a poly slot (heterogeneous
23772387 hash / array / param / ivar), copy it onto the GC heap and box the