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

@@ -8563,6 +8563,8 @@
85638563 ["SystemCallError", "StandardError"],
85648564 ["LocalJumpError", "StandardError"],
85658565 ["FiberError", "StandardError"],
8566+ ["NoMatchingPatternError", "StandardError"],
8567+ ["NoMatchingPatternKeyError", "NoMatchingPatternError"],
85668568 ["Exception", ""],
85678569 ]
85688570 bp = 0
@@ -32876,6 +32878,17 @@
3287632878 @indent = @indent + 1
3287732879 compile_stmts_body(@nd_body[ec])
3287832880 @indent = @indent - 1
32881+ elsif conds.length > 0
32882+ # No `else` clause: CRuby raises NoMatchingPatternError when no
32883+ # `in` arm matched. The message includes the inspected scrutinee
32884+ # value (we drop the `=== val does not return true` tail since
32885+ # spinel doesn't carry the pattern's source text at codegen).
32886+ ins_msg = compile_inspect_for(pred_type, tmp)
32887+ if ins_msg != ""
32888+ emit(" } else { sp_raise_cls(\"NoMatchingPatternError\", " + ins_msg + ");")
32889+ else
32890+ emit(" } else { sp_raise_cls(\"NoMatchingPatternError\", \"no matching pattern\");")
32891+ end
3287932892 end
3288032893 if conds.length > 0
3288132894 emit(" }")