summaryrefslogtreecommitdiff
path: root/prism_compile.c
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-02-06 09:22:30 -0500
committerKevin Newton <[email protected]>2024-02-06 09:51:25 -0500
commitd6b7eae58e71f69b70a4782c9fc204d8379c1ea5 (patch)
tree455c50e484ba1e12ac14e4c83e5df83355954c38 /prism_compile.c
parent4f4f3a6dec253af7d8f138877cc80fb70c617654 (diff)
[PRISM] Correct spec for defined? parentheses
Diffstat (limited to 'prism_compile.c')
-rw-r--r--prism_compile.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/prism_compile.c b/prism_compile.c
index 4ba1f9a336..4ae0056a5d 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -2557,6 +2557,7 @@ pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *co
{
// in_condition is the same as compile.c's needstr
enum defined_type dtype = DEFINED_NOT_DEFINED;
+
switch (PM_NODE_TYPE(node)) {
case PM_ARGUMENTS_NODE: {
const pm_arguments_node_t *cast = (pm_arguments_node_t *) node;
@@ -2577,14 +2578,25 @@ pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *co
dtype = DEFINED_NIL;
break;
case PM_PARENTHESES_NODE: {
- pm_parentheses_node_t *parentheses_node = (pm_parentheses_node_t *) node;
+ const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
- if (parentheses_node->body == NULL) {
- dtype = DEFINED_NIL;
- } else {
- dtype = DEFINED_EXPR;
- }
- break;
+ if (cast->body == NULL) {
+ // If we have empty parentheses, then we want to return "nil".
+ dtype = DEFINED_NIL;
+ }
+ else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
+ // If we have a parentheses node that is wrapping a single statement
+ // then we want to recurse down to that statement and compile it.
+ pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], ret, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, explicit_receiver);
+ return;
+ }
+ else {
+ // Otherwise, we have parentheses wrapping multiple statements, in
+ // which case this is defined as "expression".
+ dtype = DEFINED_EXPR;
+ }
+
+ break;
}
case PM_SELF_NODE:
dtype = DEFINED_SELF;
@@ -2596,13 +2608,16 @@ pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *co
dtype = DEFINED_FALSE;
break;
case PM_ARRAY_NODE: {
- pm_array_node_t *array_node = (pm_array_node_t *) node;
- if (!(array_node->base.flags & PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT)) {
- for (size_t index = 0; index < array_node->elements.size; index++) {
- pm_compile_defined_expr0(iseq, array_node->elements.nodes[index], ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
+ pm_array_node_t *cast = (pm_array_node_t *) node;
+
+ if (!PM_NODE_FLAG_P(cast, PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT)) {
+ for (size_t index = 0; index < cast->elements.size; index++) {
+ pm_compile_defined_expr0(iseq, cast->elements.nodes[index], ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
+
if (!lfinish[1]) {
lfinish[1] = NEW_LABEL(lineno);
}
+
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
}
}