Commit a9c8fa52

a9c8fa520accMerged

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

Description

fix(codegen): namespaced exception default message uses Ruby :: separator

ActiveRecord::RecordNotFound.new.message returned the C-mangled form ActiveRecord_RecordNotFound. spinel stores class names with _ as the module-prefix separator (collect_class_with_prefix's mangling) and the exc_default_msg_expr fallback emitted the cname verbatim into the default-message string literal.

Add cls_name_to_ruby which walks the longest underscore-prefix that matches a registered module name and recursively unmangles further prefixes -- so Outer_Inner_DeepError correctly becomes Outer::Inner::DeepError. Underscores that don't bridge a module boundary stay as-is (My_Helper remains a single identifier).

Tests: default 461/0/0, promote 461/0/0, optcarrot checksum 59662. New regression in test/exception_namespaced_default_message.rb covers single-level, two-level, three-level namespaced exceptions and the negative case where an unrelated underscore-bearing class name shouldn't be split.

Closes #692

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

Details

Commit
a9c8fa520acc
Parent
c942dc5086b3

spinel_codegen.rbmodifiedView file

@@ -996,7 +996,39 @@
996996 end
997997 end
998998 # Fallback: class name (matches `RuntimeError.new.message`).
999- "(&(\"\\xff\" \"" + cname + "\")[1])"
999+ # Convert C-mangled `Foo_Bar` into Ruby's `Foo::Bar` when the
1000+ # prefix matches a registered module so namespaced exceptions
1001+ # report the user-visible name. Underscores that don't bridge
1002+ # a module boundary stay as-is (`My_Helper` etc.).
1003+ "(&(\"\\xff\" \"" + cls_name_to_ruby(cname) + "\")[1])"
1004+ end
1005+
1006+ # Reverse the `_`-as-module-separator mangling applied by
1007+ # collect_class_with_prefix. Walks the longest prefix that matches
1008+ # a registered module (recursively, so a nested `Outer_Inner`
1009+ # module-prefix unmangles further to `Outer::Inner`) and rejoins
1010+ # the path with `::`. Underscores that don't bridge a module
1011+ # boundary stay as-is (`My_Helper` etc.).
1012+ def cls_name_to_ruby(cname)
1013+ return cname unless cname.include?("_")
1014+ parts = cname.split("_")
1015+ best_len = 0
1016+ try_len = parts.length - 1
1017+ while try_len >= 1
1018+ candidate = parts[0, try_len].join("_")
1019+ if module_name_exists(candidate) == 1
1020+ best_len = try_len
1021+ try_len = 0
1022+ else
1023+ try_len = try_len - 1
1024+ end
1025+ end
1026+ if best_len == 0
1027+ return cname
1028+ end
1029+ prefix_ruby = cls_name_to_ruby(parts[0, best_len].join("_"))
1030+ tail = parts[best_len, parts.length - best_len].join("_")
1031+ prefix_ruby + "::" + tail
10001032 end
10011033
10021034 # Walk the AST from `nid` looking for a `class <cname> ... end`