Commit fa8898f0

fa8898f08625Merged

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

Description

codegen: reconcile bare/value returns with the inferred return type

emit_return emitted a literal return; for an argless return and a return <value>; for a value return regardless of the function's inferred C return type, producing -Wreturn-mismatch errors in the transpiled blog:

  • non-void function 'sp_Db_s_close' should return a value Db.close ends in @pool = nil (typed DbPool*, nullable) but an early return if @pool.nil? emitted a bare return;.
  • void function 'sp_Transport_broadcast' should not return a value
  • void function 'sp_Main_s_serve_static' should not return a value both return nil in every path (typed void) but emitted return <nil-value>; for their explicit return nil.

Now: an argless return in a non-void function emits the return type's nil representation (NULL for a nullable object/pointer/string, the scalar nil sentinel otherwise); a return <expr> in a void function evaluates a non-literal expr for its side effects and then a bare return;.

Adds a regression test covering both shapes. Full suite: 893 pass.

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

Details

Commit
fa8898f08625
Parent
482d4237e7ac

src/codegen_stmt.cmodifiedView file

@@ -1711,6 +1711,18 @@
17111711 }
17121712 buf_printf(b, " return _t%d; }\n", ta);
17131713 }
1714+ else if (n > 0 && g_ret_type == TY_VOID) {
1715+ /* void function: a `return <expr>` (typically `return nil`) discards its
1716+ value. Evaluate a non-literal expr for side effects, then a bare return,
1717+ so the generated C doesn't `return <value>` from a void function. */
1718+ const char *aty = nt_type(c->nt, a[0]);
1719+ int trivial = aty && (!strcmp(aty, "NilNode") || !strcmp(aty, "IntegerNode") ||
1720+ !strcmp(aty, "FloatNode") || !strcmp(aty, "StringNode") ||
1721+ !strcmp(aty, "SymbolNode") || !strcmp(aty, "TrueNode") ||
1722+ !strcmp(aty, "FalseNode"));
1723+ if (!trivial) { buf_puts(b, "(void)("); emit_expr(c, a[0], b); buf_puts(b, "); "); }
1724+ buf_puts(b, "return;\n");
1725+ }
17141726 else if (n > 0) {
17151727 buf_puts(b, "return ");
17161728 TyKind r0 = comp_ntype(c, a[0]);
@@ -1723,7 +1735,14 @@
17231735 buf_puts(b, ";\n");
17241736 }
17251737 else if (g_ret_type == TY_POLY) buf_puts(b, "return sp_box_nil();\n");
1726- else buf_puts(b, "return;\n");
1738+ else if (g_ret_type == TY_VOID || g_ret_type == TY_UNKNOWN) buf_puts(b, "return;\n");
1739+ /* bare `return` is Ruby nil: emit the return type's nil representation, not a
1740+ bare `return;` (illegal in a non-void C function -- e.g. `Db.close` returns
1741+ a nullable DbPool*, where nil is NULL). */
1742+ else if (g_ret_type == TY_INT) buf_puts(b, "return SP_INT_NIL;\n");
1743+ else if (g_ret_type == TY_FLOAT) buf_puts(b, "return sp_float_nil();\n");
1744+ else if (g_ret_type == TY_STRING) buf_puts(b, "return NULL;\n");
1745+ else buf_printf(b, "return %s;\n", default_value(g_ret_type));
17271746 }
17281747
17291748 void emit_stmt_inner(Compiler *c, int id, Buf *b, int indent);