Commit 4c33106c
4c33106cd653Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · +1 added
Description
Merge pull request #154 from ryanseys/rs-endless-method-rescue
test(codegen): pin endless method with rescue modifier
Details
- Commit
- 4c33106cd653
- Parent
- f4040cbf622b
- Merge parent
- b2ebdba46ecb
test/endless_method_rescue.rbaddedView file
1+
# Endless method with rescue modifier:2+
# def name(args) = expr rescue fallback3+
# Combines `def foo(x) = expr` (already supported) with the postfix4+
# `rescue` modifier. This test5+
# pins int-typed-return shapes.6+
7+
# Plain endless + rescue with Integer() conversion8+
def parse_int(s) = Integer(s) rescue 09+
puts parse_int("42")10+
puts parse_int("abc")11+
puts parse_int("0")12+
puts parse_int("-7")13+
14+
# Explicit-raise rescue trigger. Pre-fix the test was `half(n) = n / 2`,15+
# which never raises (0/2 == 0) and so never exercised the rescue path.16+
# Using raise() rather than `a / b` because Spinel's int-div on b==0 is17+
# C undefined behaviour (SIGFPE on x86) — outside the longjmp net the18+
# rescue keyword unwinds. The raise lives inside a helper so the endless19+
# body is a single call expression — keeps codegen happy and exercises20+
# the cross-frame rescue path.21+
def assert_pos(n)22+
n < 0 ? raise("negative") : n23+
end24+
def safe(n) = assert_pos(n) rescue -125+
puts safe(10)26+
puts safe(-5)27+
puts safe(0)28+
29+
# Nested call with rescue30+
def chain(s) = s.to_i + 1 rescue 031+
puts chain("99")32+
puts chain("zero")33+
puts chain("-1")