From 55071d94b39c5dac4d249644b26357a17b5f04ec Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 18 Jul 2019 11:09:41 -0700 Subject: [PATCH] Do not ignore @ after :! or :~ These @ were ignored as a side effect of !@ and ~@ being treated like ! and ~ in method names, which matz confirmed is by design. However, there is no reason to ignore the @ in a symbol. Fixes [Bug #10463] --- parse.y | 9 +++++++-- test/ruby/test_syntax.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/parse.y b/parse.y index a950208e52..2afdf5c614 100644 --- a/parse.y +++ b/parse.y @@ -8779,7 +8779,7 @@ parser_yylex(struct parser_params *p) c = nextc(p); if (IS_AFTER_OPERATOR()) { SET_LEX_STATE(EXPR_ARG); - if (c == '@') { + if (c == '@' && !IS_lex_state_for(last_state, EXPR_FITEM)) { return '!'; } } @@ -9144,6 +9144,11 @@ parser_yylex(struct parser_params *p) case '"': p->lex.strterm = NEW_STRTERM(str_dsym, c, 0); break; + case '!': + case '~': + pushback(p, c); + SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM); + return tSYMBEG; default: pushback(p, c); break; @@ -9191,7 +9196,7 @@ parser_yylex(struct parser_params *p) case '~': if (IS_AFTER_OPERATOR()) { - if ((c = nextc(p)) != '@') { + if ((c = nextc(p)) != '@' || IS_lex_state_for(last_state, EXPR_FITEM)) { pushback(p, c); } SET_LEX_STATE(EXPR_ARG); diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index c5c3737b30..64610c1256 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -341,6 +341,20 @@ def test_warn_balanced end end + def test_bang_tilde_atmark + assert_raise(SyntaxError) { eval ':!@' } + assert_raise(SyntaxError) { eval ':~@' } + bang, tilde = nil + klass = Class.new do + bang = def !@; 1 end + tilde = def ~@; 2 end + end + assert_equal(:!, bang) + assert_equal(:~, tilde) + assert_equal(1, !klass.new) + assert_equal(2, ~klass.new) + end + def test_cmd_symbol_after_keyword bug6347 = '[ruby-dev:45563]' assert_not_label(:foo, 'if true then not_label:foo end', bug6347) -- 2.21.0