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

Incorrect inferred variable value inside ensure block when method body has return statements #7750

Open
ipvalverde opened this issue Mar 6, 2024 · 0 comments
Labels
bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team

Comments

@ipvalverde
Copy link

Input

→ View on sorbet.run

# typed: strict
extend T::Sig

sig { returns(T.any(Integer, String)) }
def do_some_stuff
  result = T.let(nil, T.nilable(T.any(Integer, String)))

  value = rand(10)
  if value.even?
    return result = value
  end

  result = "Invalid value generated"
ensure

  case result
  when nil
    puts "Nil returned"
  when Integer
    puts "Integer returned"
  when String
    puts "String returned"
  else
    T.absurd(result)
  end
end

Observed output

editor.rb:20: This code is unreachable https://srb.help/7006
    20 |    puts "Integer returned"
            ^^^^^^^^^^^^^^^^^^^^^^^
    editor.rb:19: This condition was always falsy (FalseClass)
    19 |  when Integer
               ^^^^^^^
  Got FalseClass originating from:
    editor.rb:19:
    19 |  when Integer
               ^^^^^^^
Errors: 1

Expected behavior

The code should work as expected since, when the code execution reaches the ensure block the variable can have either String or Integer value (even nil in case an error happens). Apparently the current code just takes the last assignment to the variable and treat that as the truth about the variable's value.


@ipvalverde ipvalverde added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team
Projects
None yet
Development

No branches or pull requests

1 participant