From efa543bd36eb115f08f0e30de3a9832616913786 Mon Sep 17 00:00:00 2001 From: Ilya Bylich Date: Thu, 28 Dec 2023 17:20:30 +0100 Subject: [PATCH] + ruby33.y: reject invalid gvar as symbol (#974) This commit tracks upstream commit ruby/ruby@8980207. --- lib/parser/lexer.rl | 8 +++++++- lib/parser/messages.rb | 2 +- test/test_parser.rb | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/parser/lexer.rl b/lib/parser/lexer.rl index 52efae944..9a089c67e 100644 --- a/lib/parser/lexer.rl +++ b/lib/parser/lexer.rl @@ -1387,7 +1387,13 @@ class Parser::Lexer ':' ( bareword | global_var | class_var | instance_var | operator_fname | operator_arithmetic | operator_rest ) => { - emit(:tSYMBOL, tok(@ts + 1), @ts) + gvar_name = tok(@ts + 1) + + if @version >= 33 && gvar_name.start_with?('$0') && gvar_name.length > 2 + diagnostic :error, :gvar_name, { :name => gvar_name }, range(@ts + 1, @te) + end + + emit(:tSYMBOL, gvar_name, @ts) fnext expr_end; fbreak; }; diff --git a/lib/parser/messages.rb b/lib/parser/messages.rb index 6d143abb1..e6a5c6dac 100644 --- a/lib/parser/messages.rb +++ b/lib/parser/messages.rb @@ -21,6 +21,7 @@ module Parser :regexp_options => 'unknown regexp options: %{options}', :cvar_name => "`%{name}' is not allowed as a class variable name", :ivar_name => "`%{name}' is not allowed as an instance variable name", + :gvar_name => "`%{name}' is not allowed as a global variable name", :trailing_in_number => "trailing `%{character}' in number", :empty_numeric => 'numeric literal without digits', :invalid_octal => 'invalid octal digit', @@ -72,7 +73,6 @@ module Parser :circular_argument_reference => 'circular argument reference %{var_name}', :pm_interp_in_var_name => 'symbol literal with interpolation is not allowed', :lvar_name => "`%{name}' is not allowed as a local variable name", - :gvar_name => '%{name} is not allowed as a global variable name', :undefined_lvar => "no such local variable: `%{name}'", :duplicate_variable_name => 'duplicate variable name %{name}', :duplicate_pattern_key => 'duplicate hash pattern key %{name}', diff --git a/test/test_parser.rb b/test/test_parser.rb index 6676d3d69..1391c557d 100644 --- a/test/test_parser.rb +++ b/test/test_parser.rb @@ -7784,6 +7784,12 @@ def test_numparam_as_symbols %q{:@@1}, %q{ ^^^ location}, SINCE_2_7) + + assert_diagnoses( + [:error, :gvar_name, { :name => '$01234' }], + %q{:$01234}, + %q{ ^^^^^^ location}, + SINCE_3_3) end def test_csend_inside_lhs_of_masgn__since_27