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 fallback
3+# Combines `def foo(x) = expr` (already supported) with the postfix
4+# `rescue` modifier. This test
5+# pins int-typed-return shapes.
6+
7+# Plain endless + rescue with Integer() conversion
8+def parse_int(s) = Integer(s) rescue 0
9+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 is
17+# C undefined behaviour (SIGFPE on x86) — outside the longjmp net the
18+# rescue keyword unwinds. The raise lives inside a helper so the endless
19+# body is a single call expression — keeps codegen happy and exercises
20+# the cross-frame rescue path.
21+def assert_pos(n)
22+ n < 0 ? raise("negative") : n
23+end
24+def safe(n) = assert_pos(n) rescue -1
25+puts safe(10)
26+puts safe(-5)
27+puts safe(0)
28+
29+# Nested call with rescue
30+def chain(s) = s.to_i + 1 rescue 0
31+puts chain("99")
32+puts chain("zero")
33+puts chain("-1")