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

Type refinement with method calls #590

Closed
soutaro opened this issue Jun 22, 2022 · 2 comments
Closed

Type refinement with method calls #590

soutaro opened this issue Jun 22, 2022 · 2 comments
Assignees
Milestone

Comments

@soutaro
Copy link
Owner

soutaro commented Jun 22, 2022

Proposal: https://hackmd.io/cQ75yJxcT_Ka5nHjQySWtg?view

Assume we have a class with the following RBS.

class Account
  attr_reader email: String
  
  attr_reader phone: String?
end

email always exists, but phone may be nil. So, a Ruby code accesing phone number needs to test if phone number exists explicitly.

account = Account.find(email)

if account.phone                # Test if `phone` returns a `String`
    send_sms_to(account.phone)  # Sending a SMS to the phone number
end

This doesn't work with Steep right now, because account.phone is a method call that may return different value in future. This proposal is to make it work by assuming pure methods returns the same value next time.

@soutaro soutaro added the spec label Jun 22, 2022
@soutaro soutaro added this to the Steep 1.1 milestone Jun 22, 2022
@soutaro soutaro self-assigned this Jun 22, 2022
@soutaro
Copy link
Owner Author

soutaro commented Jul 11, 2022

I found that the invalidation of type refinements on method call cannot be safe.

a = [0]
b = a

if a[0].is_a?(Integer)        # This refines the type to `Integer`
  a[0] = ""                   # This invalidates a[0] refinement
end

if a[0].is_a?(String)         # This refines the type to `String`
  b[0] = :foo                 # How about this!?!?
end

I will consider dropping type refinements invalidation by method calls. 🤔
Will keep the invalidation on local variable assignments.

@soutaro soutaro changed the title Flexible flow-sensitive typing Type refinement with method calls Jul 25, 2022
@soutaro soutaro closed this as completed Jul 26, 2022
@soutaro soutaro reopened this Jul 26, 2022
@soutaro
Copy link
Owner Author

soutaro commented Jul 27, 2022

As of Steep 1.1,

  • It invalidates on non pure method call of the same receiver node
  • We don't provide an option to disable the feature

@soutaro soutaro closed this as completed Jul 27, 2022
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

No branches or pull requests

1 participant