Commit 70f7ed22

70f7ed22e881Merged

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

Description

codegen: follow-up to #1423 — emit_return polish from review

  • Extract the literal-node check into node_is_pure_literal and unwrap parentheses first, so return (nil) in a void function collapses to a bare return; rather than (void)((nil)); return;.
  • A bare return in a TY_UNKNOWN method now emits return 0; (TY_UNKNOWN is emitted as an int C return type, e.g. main()), not the void return; it shared with TY_VOID.
  • default_value already yields the by-value nil form for struct return types (e.g. (sp_Range){0}), so the final branch needs no special case; noted in the comment.
  • Extend the regression test with a void method whose discarded return <call> must still run the call ((void)(bump()); return;).

Full suite: 894 pass.

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

Details

Commit
70f7ed22e881
Parent
d40bc31b97a3

src/codegen_stmt.cmodifiedView file

@@ -1691,6 +1691,18 @@
16911691 emit_expr(c, node, b);
16921692 }
16931693
1694+/* A literal whose evaluation has no side effects -- used to decide whether a
1695+ discarded `return <expr>` in a void function needs `(void)(<expr>)` to keep
1696+ the side effects or can collapse to a bare `return;`. Callers unwrap parens
1697+ first. */
1698+static int node_is_pure_literal(const NodeTable *nt, int node) {
1699+ const char *ty = nt_type(nt, node);
1700+ return ty && (!strcmp(ty, "NilNode") || !strcmp(ty, "IntegerNode") ||
1701+ !strcmp(ty, "FloatNode") || !strcmp(ty, "StringNode") ||
1702+ !strcmp(ty, "SymbolNode") || !strcmp(ty, "TrueNode") ||
1703+ !strcmp(ty, "FalseNode"));
1704+}
1705+
16941706 void emit_return(Compiler *c, int id, Buf *b, int indent) {
16951707 int args = nt_ref(c->nt, id, "arguments");
16961708 int n = 0;
@@ -1739,12 +1751,8 @@
17391751 /* void function: a `return <expr>` (typically `return nil`) discards its
17401752 value. Evaluate a non-literal expr for side effects, then a bare return,
17411753 so the generated C doesn't `return <value>` from a void function. */
1742- const char *aty = nt_type(c->nt, a[0]);
1743- int trivial = aty && (!strcmp(aty, "NilNode") || !strcmp(aty, "IntegerNode") ||
1744- !strcmp(aty, "FloatNode") || !strcmp(aty, "StringNode") ||
1745- !strcmp(aty, "SymbolNode") || !strcmp(aty, "TrueNode") ||
1746- !strcmp(aty, "FalseNode"));
1747- if (!trivial) { buf_puts(b, "(void)("); emit_expr(c, a[0], b); buf_puts(b, "); "); }
1754+ int vn = unwrap_parens(c, a[0]);
1755+ if (!node_is_pure_literal(c->nt, vn)) { buf_puts(b, "(void)("); emit_expr(c, vn, b); buf_puts(b, "); "); }
17481756 buf_puts(b, "return;\n");
17491757 }
17501758 else if (n > 0) {
@@ -1759,10 +1767,14 @@
17591767 buf_puts(b, ";\n");
17601768 }
17611769 else if (g_ret_type == TY_POLY) buf_puts(b, "return sp_box_nil();\n");
1762- else if (g_ret_type == TY_VOID || g_ret_type == TY_UNKNOWN) buf_puts(b, "return;\n");
1770+ else if (g_ret_type == TY_VOID) buf_puts(b, "return;\n");
1771+ /* TY_UNKNOWN is emitted as an int C return type (main(), or a never-inferred
1772+ method), so its bare return is `return 0;`, not the void `return;`. */
1773+ else if (g_ret_type == TY_UNKNOWN) buf_puts(b, "return 0;\n");
17631774 /* bare `return` is Ruby nil: emit the return type's nil representation, not a
17641775 bare `return;` (illegal in a non-void C function -- e.g. `Db.close` returns
1765- a nullable DbPool*, where nil is NULL). */
1776+ a nullable DbPool*, where nil is NULL). default_value already yields the
1777+ by-value nil form for struct types (e.g. (sp_Range){0}). */
17661778 else if (g_ret_type == TY_INT) buf_puts(b, "return SP_INT_NIL;\n");
17671779 else if (g_ret_type == TY_FLOAT) buf_puts(b, "return sp_float_nil();\n");
17681780 else if (g_ret_type == TY_STRING) buf_puts(b, "return NULL;\n");