8000 flow-sensitivity should understand instance_of? (or we should warn about it) · Issue #48 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

flow-sensitivity should understand instance_of? (or we should warn about it) #48

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

Closed
DarkDimius opened this issue Jan 16, 2019 · 2 comments · Fixed by #8868
Closed

flow-sensitivity should understand instance_of? (or we should warn about it) #48

DarkDimius opened this issue Jan 16, 2019 · 2 comments · Fixed by #8868
Labels
good first issue Good for newcomers

Comments

@DarkDimius
Copy link
Collaborator

https://ruby-doc.org/core-2.4.3/Object.html#method-i-instance_of-3F looks to be a synonym for {{is_a?}}

@btidor-stripe just ran into this and was confused

Originally reported by @nelhage-stripe at https://jira.corp.stripe.com/browse/RUBYPLAT-1371

@DarkDimius DarkDimius added the good first issue Good for newcomers label Jan 16, 2019
marianosimone added a commit that referenced this issue Aug 15, 2020
This makes sure that, after calling `instance_of?` on an object, Sorbet
knows about the type.

Fixes #48
@marianosimone marianosimone self-assigned this Aug 15, 2020
@marianosimone
Copy link
Collaborator

Taking a stab at this...

For completeness-sake, this is how I reproduced the issue:

view on sorbet.run

# typed: true
class A; end

def main
  a = T.let(A.new, T.any(A, Float))
  if a.is_a?(A)
    T.assert_type!(a, A)
  end
    if a.instance_of?(A)
    T.assert_type!(a, A)
  end
end

Output:

editor.rb:10: Argument does not have asserted type A https://srb.help/7007
    10 |    T.assert_type!(a, A)
            ^^^^^^^^^^^^^^^^^^^^
  Got T.any(Float, A) originating from:
    editor.rb:5:
     5 |  a = T.let(A.new, T.any(A, Float))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    editor.rb:6:
     6 |  if a.is_a?(A)
             ^^^^^^^^^^
Errors: 1

Expected: No errors

Note that instance_of? is NOT a synonym of is_a?, as the latter takes inheritance into account, while the former does not.

@marianosimone marianosimone removed their assignment Aug 15, 2020
@marianosimone
Copy link
Collaborator

My original attempt won't work (as seen in #3364).

The basic reason is that we can't make assumptions for instance_of? the same way it doe about is_a?: is_a? ensures that something is of a particular type or any of its descendants, while instance_of? tests for a specific type.

elliottt pushed a commit that referenced this issue Jul 30, 2021
* Add two extern special-cases (rb_int_powm, rb_f_send)

* Explicit whitelist of methods to export

* Ensure sorted output order

* Better printing information

* Ignore -2 calling convention

* Don't clang-format WrappedIntrinsics.h

* Add whitelist of valid SymbolRefs

* Make patch regex more strict

* (cd compiler/IREmitter/Intrinsics/ && make)
@jez jez closed this as completed in #8868 May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
2 participants
0