Commit 3d99fa63

3d99fa63c024Merged

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

Description

feat(codegen): emit <obj>.class as a class-name string literal

Spinel doesn't allocate runtime Class objects, so obj.class on a obj_<C> receiver had no resolution and emitted a cannot resolve call warning + literal 0. The two real consumers in practice are interpolation in inspect/to_s and the .to_s.split("::").last introspection shape — both work fine if .class simply yields the class name as a string.

Two sites updated:

  • infer_call_type: obj_<C>.class infers as string.
  • compile_object_method_expr: emit c_string_literal(cname) for the call.

Cosmetic note: nested module classes render with the underscore-flattened name (Optcarrot_NES) rather than the original Optcarrot::NES. Not lossless, but sufficient for the optcarrot warning sites.

Suppresses 7 of the optcarrot make optcarrot warnings.

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

Details

Commit
3d99fa63c024
Parent
82d5cace65d9

spinel_codegen.rbmodifiedView file

@@ -2934,6 +2934,18 @@
29342934 end
29352935 end
29362936
2937+ # `<obj>.class` returns the class name as a string (Spinel
2938+ # collapses Class/Module objects to their textual name; the
2939+ # only consumers in practice are interpolation
2940+ # `"#<#{self.class}>"` and `.to_s.split` chains, both of
2941+ # which work fine on a plain string).
2942+ if recv >= 0 && mname == "class"
2943+ rt = infer_type(recv)
2944+ if is_obj_type(rt) == 1
2945+ return "string"
2946+ end
2947+ end
2948+
29372949 # `recv.__sp_ieval_<N>(...)`: the rewritten form of an
29382950 # `recv.instance_eval { ... }` call. v1 only fired on top-level call
29392951 # sites, where the call's value was always discarded — so its return
@@ -27878,6 +27890,16 @@
2787827890 if @cls_is_value_type[ci] == 1
2787927891 arrow = "."
2788027892 end
27893+ # `<obj>.class` — collapse to the class name as a string
27894+ # literal. Spinel doesn't allocate runtime Class objects,
27895+ # but the typical consumers (interpolation in inspect,
27896+ # `.to_s.split("::").last` shape) all treat the result as
27897+ # a string. Underscore-flattened nested module names
27898+ # (`Optcarrot_NES`) render as-is rather than the original
27899+ # `Optcarrot::NES` form; the loss is cosmetic.
27900+ if mname == "class"
27901+ return c_string_literal(cname)
27902+ end
2788127903 # attr_reader
2788227904 readers = @cls_attr_readers[ci].split(";")
2788327905 j = 0