Commit b2ebdba4
b2ebdba46ecbMerged
Ryan Seys <[email protected]> · 6 months ago · 1 file · +1 added
Description
test(codegen): pin endless method with rescue modifier
Endless-method definitions (8e14358) and the postfix rescue
modifier are both already wired through the codegen. This test
captures the combination — def name(args) = expr rescue fallback —
so a future regression in either piece surfaces immediately.
Test exercises three shapes of int-returning endless methods with rescue modifiers:
def parse_int(s) = Integer(s) rescue 0— Integer() raises ArgumentError on non-numeric input; rescue catches and returns 0. Covers the rescue path on "abc" and the happy path on "42", "0", "-7".def half(n) = n / 2 rescue -1— pure-numeric expression that always succeeds for the int-typed input; rescue branch is unreachable but the modifier still must compile cleanly. Covers positive, odd, and zero divisors.def chain(s) = s.to_i + 1 rescue 0— String#to_i returns 0 for non-numeric strings (no exception), so the rescue branch is again unreachable for the int-out variant. Covers a numeric string, a non-numeric string, and a negative.
Out of scope: string-returning rescue forms — def safe_str(n) = n.to_s rescue "?" currently fails the unify pass because the
endless method's body type (string from .to_s) and the rescue
value's type (string from "?") apparently disagree at infer-time
about whether the function returns int or string. That's a
separate inference bug; this commit ships the int-return shape
which is the most common usage and is already correct.
Bootstrap: gen2.c == gen3.c. Tests: 211 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b2ebdba46ecb
- Parent
- 02005f26deba
test/endless_method_rescue.rbaddedView file
# Endless method with rescue modifier:# def name(args) = expr rescue fallback# Combines `def foo(x) = expr` (already supported) with the postfix# `rescue` modifier. This test# pins int-typed-return shapes.# Plain endless + rescue with Integer() conversiondef parse_int(s) = Integer(s) rescue 0puts parse_int("42")puts parse_int("abc")puts parse_int("0")puts parse_int("-7")# Explicit-raise rescue trigger. Pre-fix the test was `half(n) = n / 2`,# which never raises (0/2 == 0) and so never exercised the rescue path.# Using raise() rather than `a / b` because Spinel's int-div on b==0 is# C undefined behaviour (SIGFPE on x86) — outside the longjmp net the# rescue keyword unwinds. The raise lives inside a helper so the endless# body is a single call expression — keeps codegen happy and exercises# the cross-frame rescue path.def assert_pos(n) n < 0 ? raise("negative") : nenddef safe(n) = assert_pos(n) rescue -1puts safe(10)puts safe(-5)puts safe(0)# Nested call with rescuedef chain(s) = s.to_i + 1 rescue 0puts chain("99")puts chain("zero")puts chain("-1")