Skip to content

Commit

Permalink
+ ruby33.y: reject invalid gvar as symbol (#974)
Browse files Browse the repository at this point in the history
This commit tracks upstream commit ruby/ruby@8980207.
  • Loading branch information
iliabylich authored Dec 28, 2023
1 parent f01a39d commit efa543b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/parser/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/parser/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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}',
Expand Down
6 changes: 6 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efa543b

Please sign in to comment.