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.closeends in@pool = nil(typedDbPool*, nullable) but an earlyreturn if @pool.nil?emitted a barereturn;. - 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 explicitreturn 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
} buf_printf(b, " return _t%d; }\n", ta); } else if (n > 0 && g_ret_type == TY_VOID) { /* void function: a `return <expr>` (typically `return nil`) discards its value. Evaluate a non-literal expr for side effects, then a bare return, so the generated C doesn't `return <value>` from a void function. */ const char *aty = nt_type(c->nt, a[0]); int trivial = aty && (!strcmp(aty, "NilNode") || !strcmp(aty, "IntegerNode") || !strcmp(aty, "FloatNode") || !strcmp(aty, "StringNode") || !strcmp(aty, "SymbolNode") || !strcmp(aty, "TrueNode") || !strcmp(aty, "FalseNode")); if (!trivial) { buf_puts(b, "(void)("); emit_expr(c, a[0], b); buf_puts(b, "); "); } buf_puts(b, "return;\n"); } else if (n > 0) { buf_puts(b, "return "); TyKind r0 = comp_ntype(c, a[0]); buf_puts(b, ";\n"); } else if (g_ret_type == TY_POLY) buf_puts(b, "return sp_box_nil();\n"); else buf_puts(b, "return;\n"); else if (g_ret_type == TY_VOID || g_ret_type == TY_UNKNOWN) buf_puts(b, "return;\n"); /* bare `return` is Ruby nil: emit the return type's nil representation, not a bare `return;` (illegal in a non-void C function -- e.g. `Db.close` returns a nullable DbPool*, where nil is NULL). */ else if (g_ret_type == TY_INT) buf_puts(b, "return SP_INT_NIL;\n"); else if (g_ret_type == TY_FLOAT) buf_puts(b, "return sp_float_nil();\n"); else if (g_ret_type == TY_STRING) buf_puts(b, "return NULL;\n"); else buf_printf(b, "return %s;\n", default_value(g_ret_type));}void emit_stmt_inner(Compiler *c, int id, Buf *b, int indent);test/return_void_and_nullable_object.rbaddedView file
Loading diff…
test/return_void_and_nullable_object.rb.expectedaddedView file
Loading diff…