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

Numbered parameters are valid for pattern matching pinning #1060

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/parser/ruby27.y
Original file line number Diff line number Diff line change
Expand Up @@ -2152,11 +2152,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ruby30.y
Original file line number Diff line number Diff line change
Expand Up @@ -2225,11 +2225,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ruby31.y
Original file line number Diff line number Diff line change
Expand Up @@ -2323,11 +2323,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ruby32.y
Original file line number Diff line number Diff line change
Expand Up @@ -2332,11 +2332,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ruby33.y
Original file line number Diff line number Diff line change
Expand Up @@ -2278,11 +2278,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
3 changes: 2 additions & 1 deletion lib/parser/ruby34.y
Original file line number Diff line number Diff line change
Expand Up @@ -2285,11 +2285,12 @@ opt_block_args_tail:
p_var_ref: tCARET tIDENTIFIER
{
name = val[1][0]
lvar = @builder.accessible(@builder.ident(val[1]))

unless static_env.declared?(name)
diagnostic :error, :undefined_lvar, { :name => name }, val[1]
end

lvar = @builder.accessible(@builder.ident(val[1]))
result = @builder.pin(val[0], lvar)
}

Expand Down
50 changes: 50 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9567,6 +9567,56 @@ def test_pattern_matching_blank_else
)
end

def test_pattern_matching_numbered_parameter
assert_parses(
s(:numblock,
s(:send,
s(:int, 1), :then), 1,
s(:match_pattern,
s(:int, 1),
s(:pin,
s(:lvar, :_1)))),
%q{1.then { 1 in ^_1 }},
%q{},
%w(2.7)
)

assert_parses(
s(:numblock,
s(:send,
s(:int, 1), :then), 1,
s(:match_pattern_p,
s(:int, 1),
s(:pin,
s(:lvar, :_1)))),
%q{1.then { 1 in ^_1 }},
%q{},
SINCE_3_0
)

assert_parses(
s(:case_match,
s(:int, 0),
s(:in_pattern,
s(:match_var, :_1), nil, nil), nil),
%q{case 0; in _1; end},
%q{},
%w(2.7)
)

assert_diagnoses(
[:error, :reserved_for_numparam, { :name => '_1' }],
%q{case 0; in _1; end},
%q{ ^^ location},
SINCE_3_0)

assert_diagnoses(
[:error, :undefined_lvar, { :name => '_1' }],
%q{case 0; in ^_1; end},
%q{ ^^ location},
SINCE_2_7)
end

def assert_pattern_matching_defines_local_variables(match_code, lvar_names, versions = SINCE_2_7)
code = "case 1; #{match_code}; then [#{lvar_names.join(', ')}]; end"

Expand Down
Loading