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

Trace Proc execution to avoid unions of captured variables (Was: Type inference issue (with Benchmark.ips)) #1493

Closed
pgkos opened this issue Sep 16, 2015 · 3 comments

Comments

@pgkos
Copy link
Contributor

pgkos commented Sep 16, 2015

The following code:

require "benchmark"

def foo(ary : Array(Float64))
  return ary
end

ary = [1, 2, 3]
ary = ary.map { |n| n.to_f }

Benchmark.ips do |x|
  x.report("foo") { foo(ary) }
end

fails to compile with an error:

no overload matches 'foo' with types (Array(Int32) | Array(Float64))

I thought it may be related to invoking the function inside a block, but no, I could only produce this error while using Benchmark.ips, e.g. the following code compiles as expected:

(0..10).each do |i|
  10.times do
    foo(ary)
  end
end
@jhass
Copy link
Member

jhass commented Sep 16, 2015

A smaller example is

def foo(a : Int32)
  pp a
end

var = "bar"
var = var.size

-> { foo(var) }

http://carc.in/#/r/flm

Benchmark.ips captures the block, so it gets turned into a Proc. So now we have a Proc referencing var. We (the compiler) don't know when it will run, so we have to consider all types var might be as valid. In your second example the second assignment overrides the type of the first assignment since there it's guaranteed that foo is called after it happened.

Since it's quite impossible to do such analysis to the level where the compiler understands that some asynchronous execution may only happen after a certain event happened, this is probably a won't fix. Easy workaround: use another variable name so you never get a union: http://carc.in/#/r/flt

@asterite
Copy link
Member

I'll leave this open, it's not a bug (it works because the variable is captured) but we can definitely try to improve this.

@jhass jhass changed the title Type inference issue (with Benchmark.ips) Trace Proc execution to avoid unions of captured variables (Was: Type inference issue (with Benchmark.ips)) Sep 18, 2015
@asterite
Copy link
Member

Duplicate of #3093

@asterite asterite marked this as a duplicate of #3093 Sep 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants