Commit 05f4040b

05f4040b531cMerged

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

Description

fix(codegen,analyze): .encoding stub + Rational/Complex graceful fallback

#723 (.encoding): spinel doesn't model Encoding objects, but the warning "cannot resolve call to 'encoding' on string (emitting 0)" made puts print the literal 0. Now compile_call returns a plain "UTF-8" const char* and infer_type returns "string", so puts s.encoding prints "UTF-8" -- matching the implicit invariant that spinel treats all source as UTF-8 anyway.

#728 (Rational / Complex / Imaginary): RationalNode + ComplexNode used to hard-exit ("Spinel: cannot compile RationalNode at line N" followed by exit(1)), preventing the rest of the file from compiling at all. Switch to a soft fallback: warn + emit 1 (integer). 1 -- not 0 -- so the very common N / Mr shape doesn't turn into a runtime divide-by-zero. Result is still wrong arithmetic for the literal but the program continues; users unintentionally hitting one of these literals get a clear warning instead of a build failure. compile_stmt mirrors the same softer path (warn + no-op).

Tests: test/string_encoding.rb covers the three .encoding entry points (plain, after .encode, after .b).

462 tests pass; optcarrot checksum 59662.

Closes #723 Closes #728

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

Details

Commit
05f4040b531c
Parent
662525bbe536

spinel_analyze.rbmodifiedView file

@@ -4068,6 +4068,16 @@
40684068 # colliding with user-defined methods of the same name (`b` is
40694069 # the shortest collision target; struct field accessors etc.
40704070 # commonly take that single-letter shape).
4071+ if mname == "encoding"
4072+ # spinel doesn't model Encoding objects; treat the return as a
4073+ # plain string label (`"UTF-8"`). Issue #723.
4074+ if recv >= 0
4075+ rt_enc = infer_type(recv)
4076+ if rt_enc == "string" || rt_enc == "mutable_str"
4077+ return "string"
4078+ end
4079+ end
4080+ end
40714081 if mname == "force_encoding" || mname == "encode" || mname == "b"
40724082 if recv >= 0
40734083 rt_fe = infer_type(recv)