Handle void expressions in defined?

[Bug #21029]
This commit is contained in:
Kevin Newton 2025-03-18 13:40:49 -04:00
parent b003d40194
commit adaaa7878e
Notes: git 2025-03-18 18:44:49 +00:00
3 changed files with 22 additions and 1 deletions

View File

@ -4213,7 +4213,7 @@ pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
// 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) {
else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
// 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], node_location, ret, popped, scope_node, in_condition, lfinish, false);

View File

@ -0,0 +1 @@
exclude(:test_defined_paren_void_stmts, "defined? (x;)")

View File

@ -243,6 +243,26 @@ class TestDefined < Test::Unit::TestCase
assert_nil(defined?(p () + 1))
end
def test_defined_paren_void_stmts
assert_equal("expression", defined? (;x))
assert_equal("expression", defined? (x;))
assert_nil(defined? (
x
))
x = 1
assert_equal("expression", defined? (;x))
assert_equal("expression", defined? (x;))
assert_equal("local-variable", defined? (
x
))
end
def test_defined_impl_specific
feature7035 = '[ruby-core:47558]' # not spec
assert_predicate(defined?(Foo), :frozen?, feature7035)