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
end end # Fallback: class name (matches `RuntimeError.new.message`). "(&(\"\\xff\" \"" + cname + "\")[1])" # Convert C-mangled `Foo_Bar` into Ruby's `Foo::Bar` when the # prefix matches a registered module so namespaced exceptions # report the user-visible name. Underscores that don't bridge # a module boundary stay as-is (`My_Helper` etc.). "(&(\"\\xff\" \"" + cls_name_to_ruby(cname) + "\")[1])" end # Reverse the `_`-as-module-separator mangling applied by # collect_class_with_prefix. Walks the longest prefix that matches # a registered module (recursively, so a nested `Outer_Inner` # module-prefix unmangles further to `Outer::Inner`) and rejoins # the path with `::`. Underscores that don't bridge a module # boundary stay as-is (`My_Helper` etc.). def cls_name_to_ruby(cname) return cname unless cname.include?("_") parts = cname.split("_") best_len = 0 try_len = parts.length - 1 while try_len >= 1 candidate = parts[0, try_len].join("_") if module_name_exists(candidate) == 1 best_len = try_len try_len = 0 else try_len = try_len - 1 end end if best_len == 0 return cname end prefix_ruby = cls_name_to_ruby(parts[0, best_len].join("_")) tail = parts[best_len, parts.length - best_len].join("_") prefix_ruby + "::" + tail end # Walk the AST from `nid` looking for a `class <cname> ... end`test/exception_namespaced_default_message.rbaddedView file
Loading diff…
test/exception_namespaced_default_message.rb.expectedaddedView file
Loading diff…