Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ builder.rb: reject multi-char gvar names starting with 0 #972

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,13 @@ def ivar(token)
end

def gvar(token)
n(:gvar, [ value(token).to_sym ],
gvar_name = value(token)

if gvar_name.start_with?('$0') && gvar_name.length > 2
diagnostic :error, :gvar_name, { :name => gvar_name }, loc(token)
end

n(:gvar, [ gvar_name.to_sym ],
variable_map(token))
end

Expand Down
1 change: 1 addition & 0 deletions lib/parser/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ 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
12 changes: 2 additions & 10 deletions lib/parser/ruby33.y
Original file line number Diff line number Diff line change
Expand Up @@ -2520,17 +2520,9 @@ regexp_contents: # nothing

string_dend: tSTRING_DEND

string_dvar: tGVAR
string_dvar: nonlocal_var
{
result = @builder.gvar(val[0])
}
| tIVAR
{
result = @builder.ivar(val[0])
}
| tCVAR
{
result = @builder.cvar(val[0])
result = @builder.accessible(val[0])
}
| backref

Expand Down
14 changes: 14 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11370,4 +11370,18 @@ def test_ruby_bug_19281
%q{},
SINCE_3_3)
end

def test_ungettable_gvar
assert_diagnoses(
[:error, :gvar_name, { :name => '$01234' }],
'$01234',
'^^^^^^ location',
ALL_VERSIONS)

assert_diagnoses(
[:error, :gvar_name, { :name => '$01234' }],
'"#$01234"',
' ^^^^^^ location',
ALL_VERSIONS)
end
end