Skip to content

Commit

Permalink
- ruby27.y: reject invalid lvar in pattern matching (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 26, 2020
1 parent 7d89250 commit 5a85967
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit 5a85967

Please sign in to comment.