Commit 4c942578

4c9425787398Merged

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

Description

Fix #1417: coerce poly values into RBS-pinned types at return/arg boundaries

Once RBS seeds reach module-nested classes (#1419), a method's declared return or parameter type can be narrower than the poly value its body produces or its call site supplies. Codegen emitted the raw sp_RbVal into the concrete slot, so the generated C failed to compile:

error: returning 'sp_RbVal' from a function with incompatible result
       type 'const char *'                      (ViewHelpers.content_for_get / get_slot)
error: passing 'sp_RbVal' to parameter of incompatible type
       'sp_Article *'                           (Views::Articles.article_json)

Mirror the existing poly->int/float coercion to strings and objects:

  • emit_return / the implicit tail path: a poly value feeding a narrower return slot is unboxed via a new emit_unbox_node helper. A string uses .v.s (a nil box has a zeroed union, so this is NULL and String? round-trips nil -> NULL -> nil); an object is cast out via .v.p.
  • emit_arg_or_default: a poly argument into an object parameter is unboxed with the same (sp_<Class> *)(...).v.p form (the string/scalar arg cases already existed).

Extends rbs-seed-test with a boundary fixture that compiles, links, and runs (including the nil round-trip); it fails to compile before this change.

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

Details

Commit
4c9425787398
Parent
482d4237e7ac

MakefilemodifiedView file

@@ -336,18 +336,22 @@
336336 rbs-seed-test:
337337 @echo "rbs-seed-test: skipped (vendor/rbs not fetched; run 'make deps')"
338338 else
339-rbs-seed-test: $(SPINEL) spinel_rbs_extract$(EXE)
339+rbs-seed-test: $(SPINEL) spinel_rbs_extract$(EXE) $(SP_RT_LIB)
340340 @cp -f spinel_rbs_extract$(EXE) $(dir $(SPINEL))spinel_rbs_extract$(EXE)
341- @tmp=$$(mktemp -d /tmp/spinel-rbsseed.XXXXXX); \
341+ @tmp=$$(mktemp -d /tmp/spinel-rbsseed.XXXXXX); ok=1; \
342342 $(SPINEL) test/rbs-seed/nested_ivar.rb --rbs test/rbs-seed/sig \
343343 -c --no-line-map -o "$$tmp/out.c" 2>/dev/null; \
344- ok=1; \
345- grep -Eq 'const char[[:space:]]+\*[[:space:]]*iv_label' "$$tmp/out.c" || ok=0; \
346- if grep -Eq 'sp_RbVal[[:space:]]+iv_label' "$$tmp/out.c"; then ok=0; fi; \
347- $(CC) -fsyntax-only -Ilib "$$tmp/out.c" 2>/dev/null || ok=0; \
344+ grep -Eq 'const char[[:space:]]+\*[[:space:]]*iv_label' "$$tmp/out.c" || { echo "rbs-seed-test: FAIL (#1417: module-nested-class seed not applied)"; ok=0; }; \
345+ if grep -Eq 'sp_RbVal[[:space:]]+iv_label' "$$tmp/out.c"; then echo "rbs-seed-test: FAIL (#1417: ivar stayed poly)"; ok=0; fi; \
346+ $(CC) -fsyntax-only -Ilib "$$tmp/out.c" 2>/dev/null || { echo "rbs-seed-test: FAIL (nested_ivar C invalid)"; ok=0; }; \
347+ $(SPINEL) test/rbs-seed/boundary.rb --rbs test/rbs-seed/sig \
348+ -c --no-line-map -o "$$tmp/b.c" 2>/dev/null; \
349+ if $(CC) -O0 -Ilib "$$tmp/b.c" $(SP_RT_LIB) -lm -o "$$tmp/b" 2>"$$tmp/b.err"; then \
350+ "$$tmp/b" > "$$tmp/b.out" 2>/dev/null; \
351+ 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; }; \
352+ else echo "rbs-seed-test: FAIL (#1417 boundary coercion C did not compile)"; ok=0; fi; \
348353 rm -rf "$$tmp"; \
349- if [ $$ok -eq 1 ]; then echo "rbs-seed-test: pass"; \
350- else echo "rbs-seed-test: FAIL (#1417: module-nested-class seed not applied)"; exit 1; fi
354+ if [ $$ok -eq 1 ]; then echo "rbs-seed-test: pass"; else exit 1; fi
351355 endif
352356
353357 # Analyze-fail fixtures test the legacy analyzer's rejection diagnostics;