diff --git a/compile.c b/compile.c index b73b9854f4..55a8c3a1df 100644 --- a/compile.c +++ b/compile.c @@ -5972,16 +5972,22 @@ compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no l1 = NEW_LABEL(line); ADD_LABEL(body_seq, l1); - ADD_INSN(body_seq, line, pop); - ADD_TRACE_BRANCH_COVERAGE( - body_seq, - node->nd_body ? nd_first_lineno(node->nd_body) : lineno, - node->nd_body ? nd_first_column(node->nd_body) : column, - node->nd_body ? nd_last_lineno(node->nd_body) : last_lineno, - node->nd_body ? nd_last_column(node->nd_body) : last_column, - "in", - branches); - CHECK(COMPILE_(body_seq, "in body", node->nd_body, popped)); + if (node->nd_body) { + ADD_INSN(body_seq, line, pop); + ADD_TRACE_BRANCH_COVERAGE( + body_seq, + nd_first_lineno(node->nd_body), + nd_first_column(node->nd_body), + nd_last_lineno(node->nd_body), + nd_last_column(node->nd_body), + "in", + branches); + CHECK(COMPILE_(body_seq, "in body", node->nd_body, popped)); + } + else { + /* Single line pattern matching(`expr in pat`). Returns the value of expr. */ + ADD_TRACE_BRANCH_COVERAGE(body_seq, lineno, column, last_lineno, last_column, "in", branches); + } ADD_INSNL(body_seq, line, jump, endlabel); pattern = node->nd_head; diff --git a/parse.y b/parse.y index 9277897f83..f76a41e4f9 100644 --- a/parse.y +++ b/parse.y @@ -1576,7 +1576,7 @@ expr : command_call { p->in_kwarg = !!$3; /*%%%*/ - $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$); + $$ = NEW_CASE3($1, NEW_IN($5, Qnull, Qnull, &@5), &@$); rb_warn0L(nd_line($$), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!"); /*% %*/ /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/ diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb index 889f8dd519..84b6964979 100644 --- a/test/ruby/test_pattern_matching.rb +++ b/test/ruby/test_pattern_matching.rb @@ -1261,7 +1261,10 @@ def test_struct ################################################################ def test_modifier_in - assert_equal true, (1 in a) + assert_equal 1, (1 in a) + assert_raise(NoMatchingPatternError) do + 1 in 2 + end assert_equal 1, a assert_valid_syntax "p(({} in {a:}), a:\n 1)" assert_syntax_error(%q{