8000 Incorrect unreachable code error when using `Class.new` inside method body · Issue #6568 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Incorrect unreachable code error when using Class.new inside method body #6568

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

Open
mwudka opened this issue Nov 16, 2022 · 1 comment
Open

Comments

@mwudka
Copy link
mwudka commented Nov 16, 2022

Input

→ View on sorbet.run

# typed: true
require 'sorbet-runtime'

extend T::Sig

# With this commented out, sorbet reports no errors. Either
# way, the script prints Created record #<MyARModel:0x000000011f125d70>
# as expected
def stuff
  Class.new do
    def call
    end
  end
end

class MyARModel
  extend T::Sig
  sig { params(attributes: T.untyped, block: T.nilable(T.proc.params(object: ::MyARModel).void)).returns(::MyARModel) }
  def self.find_or_create_by(attributes, &block)
    model = MyARModel.new
    unless block.nil? 
      yield model
    end
    model
  end
end

MyARModel.find_or_create_by(foo: "bar", baz: "baf") do |record|
  puts "Created record #{record}"
end

Observed output

editor.rb:28: This code is unreachable https://srb.help/7006
    28 |MyARModel.find_or_create_by(foo: "bar", baz: "baf") do |record|
                                                                ^^^^^^
    editor.rb:28: This expression always raises or can never be computed
    28 |MyARModel.find_or_create_by(foo: "bar", baz: "baf") do |record|
    29 |  puts "Created record #{record}"
    30 |end
Errors: 1

Expected behavior

Sorbet shouldn't report any errors for this code.


Discussion in Slack. Context from @jez:

this looks like a bug. i’m guessing this regression was caused by this change #5503

@jez jez changed the title Incorrect unreachable code error when codebase contains certain call method implementations Incorrect unreachable code error when using Class.new inside method body Nov 17, 2022
@fcheung
Copy link
fcheung commented Nov 18, 2022

In #6448 I was told this is a duplicate of #3609

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

2 participants
0