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

- ruby27.y: reject invalid lvar in pattern matching #680

Merged
merged 2 commits into from Apr 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/parser/builders/default.rb
Expand Up @@ -1239,8 +1239,10 @@ def unless_guard(unless_t, unless_body)

def match_var(name_t)
name = value(name_t).to_sym
name_l = loc(name_t)

check_duplicate_pattern_variable(name, loc(name_t))
check_lvar_name(name, name_l)
check_duplicate_pattern_variable(name, name_l)
@parser.static_env.declare(name)

n(:match_var, [ name ],
Expand All @@ -1253,6 +1255,7 @@ def match_hash_var(name_t)
expr_l = loc(name_t)
name_l = expr_l.adjust(end_pos: -1)

check_lvar_name(name, name_l)
check_duplicate_pattern_variable(name, name_l)
@parser.static_env.declare(name)

Expand Down Expand Up @@ -1293,6 +1296,9 @@ def match_hash_var_from_str(begin_t, strings, end_t)
Source::Map::Variable.new(name_l, expr_l))
when :begin
match_hash_var_from_str(begin_t, string.children, end_t)
else
# we only can get here if there is an interpolation, e.g., ``in "#{ a }":`
diagnostic :error, :pm_interp_in_var_name, nil, loc(begin_t).join(loc(end_t))
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/test_parser.rb
Expand Up @@ -8934,6 +8934,22 @@ def test_pattern_matching_hash_with_string_interpolation_keys
%q{ ~~~~~~~ location},
SINCE_2_7
)

assert_diagnoses(
[:error, :pm_interp_in_var_name],
%q{case a; in "#{a}": 1; end},
%q{ ~~~~~~~ location},
SINCE_2_7
)
end

def test_pattern_matching_invalid_lvar_name
assert_diagnoses(
[:error, :lvar_name, { name: :a? }],
%q{case a; in a?:; end},
%q{ ~~ location},
SINCE_2_7
)
end

def test_pattern_matching_keyword_variable
Expand Down