Commit cd4d3f6b
cd4d3f6bf26fMerged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
codegen: case/in raises NoMatchingPatternError on exhaustive miss
case 5; in 3; end previously did nothing when no arm matched
and no else clause was present — the codegen chain just closed
its trailing } and fell through. CRuby raises
NoMatchingPatternError with a message containing the inspected
scrutinee value.
In compile_case_match_stmt (after the per-arm if/elsif chain),
detect the "no else, at least one in" shape and emit
sp_raise_cls("NoMatchingPatternError", <scrutinee inspect>) as
the else branch. Use compile_inspect_for for the scrutinee
message so we get a CRuby-shaped prefix (5, "x", :sym, ...);
fall back to the literal "no matching pattern" string for types
whose inspect isn't wired.
Register NoMatchingPatternError + NoMatchingPatternKeyError in
the built-in exception hierarchy table at sp_exc_parent_of so a
rescue NoMatchingPatternError (or its parent StandardError /
Exception) catches the raise. Per CRuby 3.0:
NoMatchingPatternError < StandardError
NoMatchingPatternKeyError < NoMatchingPatternError
Class-pattern shape (case x in String) and the full CRuby
message format (<val>: <pattern> === <val> does not return true)
are separate gaps — class-pattern always-matches because
ConstantReadNode in compile_in_pattern doesn't ===-check, and
the pattern source text isn't carried at codegen.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- cd4d3f6bf26f
- Parent
- e4d2ffaa5870
spinel_codegen.rbmodifiedView file
["SystemCallError", "StandardError"], ["LocalJumpError", "StandardError"], ["FiberError", "StandardError"], ["NoMatchingPatternError", "StandardError"], ["NoMatchingPatternKeyError", "NoMatchingPatternError"], ["Exception", ""], ] bp = 0 @indent = @indent + 1 compile_stmts_body(@nd_body[ec]) @indent = @indent - 1 elsif conds.length > 0 # No `else` clause: CRuby raises NoMatchingPatternError when no # `in` arm matched. The message includes the inspected scrutinee # value (we drop the `=== val does not return true` tail since # spinel doesn't carry the pattern's source text at codegen). ins_msg = compile_inspect_for(pred_type, tmp) if ins_msg != "" emit(" } else { sp_raise_cls(\"NoMatchingPatternError\", " + ins_msg + ");") else emit(" } else { sp_raise_cls(\"NoMatchingPatternError\", \"no matching pattern\");") end end if conds.length > 0 emit(" }")test/case_in_no_match_error.rbaddedView file
Loading diff…
test/case_in_no_match_error.rb.expectedaddedView file
Loading diff…