Commit 616df163

616df163df80Merged

Sam Ruby <[email protected]> · 5 months ago · 5 files · +3 added · ~2 modified

Description

codegen: emit a void-typed proc tail as a statement, not return

A { () -> void } block whose tail expression is a void-typed call could emit return <void-call>; from the mrb_int proc trampoline, failing to compile: "returning 'void' from a function with incompatible result type 'mrb_int'".

emit_proc_literal only treated ret == TY_VOID as "no usable value". But a method whose tail has no value is often inferred TY_NIL rather than TY_VOID -- e.g. a yield if block (if-modifier) tail, or any -> void method whose RBS seed lowers void to nil. method_is_void() keys on !is_scalar_ret(), which excludes TY_NIL, so such a method is emitted as a C void function; returning its result from the trampoline (which returns mrb_int) is then invalid C.

A TY_NIL proc returns nil (0) regardless of its tail expression's value, so treat TY_NIL like TY_VOID: run the body for effect and return 0. This mirrors how the regular method emitter handles a method_is_void() body.

Adds a test/rbs-seed regression fixture (void_block_tail) that compiles a { () -> void } block tail under --rbs and runs it.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
616df163df80
Parent
a0c358afb94e

MakefilemodifiedView file

@@ -354,6 +354,12 @@
354354 "$$tmp/b" > "$$tmp/b.out" 2>/dev/null; \
355355 cmp -s "$$tmp/b.out" test/rbs-seed/boundary.expected || { echo "rbs-seed-test: FAIL (#1417 boundary output mismatch)"; diff -u test/rbs-seed/boundary.expected "$$tmp/b.out" || true; ok=0; }; \
356356 else echo "rbs-seed-test: FAIL (#1417 boundary coercion C did not compile)"; ok=0; fi; \
357+ $(SPINEL) test/rbs-seed/void_block_tail.rb --rbs test/rbs-seed/sig \
358+ -c --no-line-map -o "$$tmp/v.c" 2>/dev/null; \
359+ if $(CC) -O0 -Ilib "$$tmp/v.c" $(SP_RT_LIB) -lm -o "$$tmp/v" 2>"$$tmp/v.err"; then \
360+ "$$tmp/v" > "$$tmp/v.out" 2>/dev/null; \
361+ cmp -s "$$tmp/v.out" test/rbs-seed/void_block_tail.expected || { echo "rbs-seed-test: FAIL (void block tail output mismatch)"; diff -u test/rbs-seed/void_block_tail.expected "$$tmp/v.out" || true; ok=0; }; \
362+ else echo "rbs-seed-test: FAIL (void-returning call as proc tail: C did not compile)"; ok=0; fi; \
357363 rm -rf "$$tmp"; \
358364 if [ $$ok -eq 1 ]; then echo "rbs-seed-test: pass"; else exit 1; fi
359365 endif