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

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")