Skip to content

Support the assignment syntax (=> e) in rescue clauses#303

Merged
mame merged 4 commits intoruby:masterfrom
coord-e:rescue-assign
Apr 22, 2025
Merged

Support the assignment syntax (=> e) in rescue clauses#303
mame merged 4 commits intoruby:masterfrom
coord-e:rescue-assign

Conversation

@coord-e
Copy link
Contributor

@coord-e coord-e commented Apr 17, 2025

TypeProf 264ab6f erroneously reports an undefined method to the following code:

# rescue-assign.rb
def foo(n)
  raise if n != 0
  n.to_s
rescue StandardError => e
  e.message
end

foo(1)
$ bin/typeprof rescue-assign.rb --show-errors
# TypeProf 0.30.1

# rescue-assign.rb
# (6,4)-(6,11):undefined method: nil#message
class Object
  def foo: (Integer) -> String?
end

e in l5 should be recognized as StandardError, not nil. This PR implements the TypeProf support for the assignment syntax => e in rescue clauses, recognizing proper types for the assigned target e.

WIP part

I found a root cause of my concern, and it should be addressed by a separate PR #304.

Details

When there's no exception specified in the clause:

# rescue-assign-no-excs.rb
def foo(n)
  raise
rescue => e
  e
end

foo(1)

I expect foo to be def foo: (Integer) -> untyped because there should be no edge attached to @reference.rhs.ret. However, I get def foo: (Integer) -> nil in the current implementation, and I'm not sure how this can happen...

$ bin/typeprof rescue-assign-no-excs.rb --show-errors
# TypeProf 0.30.1

# rescue-assign-no-excs.rb
class Object
  def foo: (Integer) -> nil
end

devin-ai-integration bot added a commit to x-smasato/typeprof that referenced this pull request Apr 17, 2025
Co-Authored-By: Masato Sugiyama <public@smasato.net>
Copy link
Member

@mame mame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I said at RubyKaigi a (wrong) solution to break a vertex by using "each_type", but yes, InstanceTypeBox is a right solution to fix this issue.

@mame mame merged commit 6bec83a into ruby:master Apr 22, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants