Skip to content

Commit

Permalink
Merge pull request #703 from ksss/local_var
Browse files Browse the repository at this point in the history
Fix errors caused by non-ascii variable names
  • Loading branch information
soutaro authored Mar 9, 2023
2 parents c9668e7 + fb31c8f commit f0653e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/steep/type_inference/type_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,13 @@ def invalidated_pure_nodes(invalidated_node)
end

def local_variable_name?(name)
name.start_with?(/[a-z_]/) && name != :_ && name != :__skip__ && name != :__any__
# Ruby constants start with Uppercase_Letter or Titlecase_Letter in the unicode property.
# If name start with `@`, it is instance variable or class instance variable.
# If name start with `$`, it is global variable.
return false if name.start_with?(/[\p{Uppercase_Letter}\p{Titlecase_Letter}@$]/)
return false if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)

true
end

def local_variable_name!(name)
Expand Down
26 changes: 25 additions & 1 deletion test/type_env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,34 @@ def test_local_variable
with_factory do
env = TypeEnv.new(constant_env)

env = env.assign_local_variables({ :x => parse_type("::String") })
env = env.assign_local_variables({
:x => parse_type("::String"),
=> parse_type("::Integer")
})
assert_raises(RuntimeError) do
env.assign_local_variables({ => parse_type("::Integer") })
end

assert_equal parse_type("::String"), env[:x]
assert_equal parse_type("::Integer"), env[]
assert_nil env.enforced_type(:x)
assert_nil env.enforced_type()
end
end

def test_local_variable_name_p
with_factory do
env = TypeEnv.new(constant_env)
assert_equal true, env.local_variable_name?(:abc)
assert_equal true, env.local_variable_name?(:_abc)
assert_equal false, env.local_variable_name?(:@abc)
assert_equal false, env.local_variable_name?(:$abc)
assert_equal false, env.local_variable_name?(:_)
assert_equal false, env.local_variable_name?(:__skip__)
assert_equal false, env.local_variable_name?(:__any__)
assert_equal true, env.local_variable_name?() # Lowercase_Letter
assert_equal false, env.local_variable_name?() # Uppercase_Letter
assert_equal false, env.local_variable_name?() # Titlecase_Letter
end
end

Expand Down

0 comments on commit f0653e1

Please sign in to comment.