Commit 18a3b76e
18a3b76e5669Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Merge pull request #1426 from rubys/fix/1423-followup-review
codegen: emit_return follow-up to #1423 (review polish)
Details
- Commit
- 18a3b76e5669
- Parent
- 5e9fa43e31c2
- Merge parent
- e93dc94ec48b
src/codegen_stmt.cmodifiedView file
@@ -1691,6 +1691,19 @@
16911691
emit_expr(c, node, b);16921692
}16931693
1694+
/* A literal whose evaluation has no side effects -- used to decide whether a1695+
discarded `return <expr>` in a void function needs `(void)(<expr>)` to keep1696+
the side effects or can collapse to a bare `return;`. Callers unwrap parens1697+
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") || !strcmp(ty, "RationalNode") ||1704+
!strcmp(ty, "ImaginaryNode"));1705+
}1706+
16941707
void emit_return(Compiler *c, int id, Buf *b, int indent) {16951708
int args = nt_ref(c->nt, id, "arguments");16961709
int n = 0;@@ -1739,12 +1752,8 @@
17391752
/* void function: a `return <expr>` (typically `return nil`) discards its17401753
value. Evaluate a non-literal expr for side effects, then a bare return,17411754
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, "); "); }1755+
int vn = unwrap_parens(c, a[0]);1756+
if (!node_is_pure_literal(c->nt, vn)) { buf_puts(b, "(void)("); emit_expr(c, vn, b); buf_puts(b, "); "); }17481757
buf_puts(b, "return;\n");17491758
}17501759
else if (n > 0) {@@ -1759,10 +1768,14 @@
17591768
buf_puts(b, ";\n");17601769
}17611770
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");1771+
else if (g_ret_type == TY_VOID) buf_puts(b, "return;\n");1772+
/* TY_UNKNOWN is emitted as an int C return type (main(), or a never-inferred1773+
method), so its bare return is `return 0;`, not the void `return;`. */1774+
else if (g_ret_type == TY_UNKNOWN) buf_puts(b, "return 0;\n");17631775
/* bare `return` is Ruby nil: emit the return type's nil representation, not a17641776
bare `return;` (illegal in a non-void C function -- e.g. `Db.close` returns1765-
a nullable DbPool*, where nil is NULL). */1777+
a nullable DbPool*, where nil is NULL). default_value already yields the1778+
by-value nil form for struct types (e.g. (sp_Range){0}). */17661779
else if (g_ret_type == TY_INT) buf_puts(b, "return SP_INT_NIL;\n");17671780
else if (g_ret_type == TY_FLOAT) buf_puts(b, "return sp_float_nil();\n");17681781
else if (g_ret_type == TY_STRING) buf_puts(b, "return NULL;\n");test/return_void_and_nullable_object.rbmodifiedView file
Loading diff…
test/return_void_and_nullable_object.rb.expectedmodifiedView file
Loading diff…