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

Fix errors caused by non-ascii variable names #703

Merged
merged 1 commit into from
Mar 9, 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/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({ :Dz => 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?(:dz) # Lowercase_Letter
assert_equal false, env.local_variable_name?(:DZ) # Uppercase_Letter
assert_equal false, env.local_variable_name?(:Dz) # Titlecase_Letter
end
end

Expand Down