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

Weak type inference in a special case #13082

Closed
cdsousa opened this issue Sep 11, 2015 · 5 comments
Closed

Weak type inference in a special case #13082

cdsousa opened this issue Sep 11, 2015 · 5 comments

Comments

@cdsousa
Copy link
Contributor

cdsousa commented Sep 11, 2015

Sorry for the not so good title. I'm not sure about the right words to describe this issue.
Hence I'll put it as an auto-explainable (I hope) example. I found this issue due to JuliaImages/Images.jl#356.

Let's begin with the following definitions

type A{N}; end
type B{N}; end

getN{N}(::A{N}) = N

f(a) = B{getN(a)}()

In case we call f with a variable whose type has a Datatype as parameter,

@code_warntype f(A{Int}())
Variables:
  a::A{Int64}

Body:
  begin  # In[2], line 3:
      return $(Expr(:new, B{Int64}))
  end::B{Int64}

the final type is correctly inferred.

However, I we call f with a variable whose type has an integer value as parameter,

@code_warntype f(A{123}())
Variables:
  a::A{123}

Body:
  begin  # In[2], line 3:
      return call((top(apply_type))(Main.B,123)::TYPE{_<:B{N}})::B{N}
  end::B{N}

the return type cannot be correctly inferred (end::B{N}).
Can this be considered a bug, or is there any technical difficulty with this?

Moreover, something stranger occurs. If we wrap the integer value within a parametric type, e.g. a Tuple, the inference works:

@code_warntype f(A{Tuple{123}}())
Variables:
  a::A{Tuple{123}}

Body:
  begin  # In[2], line 3:
      return $(Expr(:new, B{Tuple{123}}))
  end::B{Tuple{123}}
@johnmyleswhite
Copy link
Member

What is a in your first codeblock?

@yuyichao
Copy link
Contributor

I think this is just because julia is not doing any local constant propagation except for types right now. Dup of #11159 and covered by #3440

@cdsousa
Copy link
Contributor Author

cdsousa commented Sep 12, 2015

Thanks @yuyichao.
(@johnmyleswhite, a was meant as a variable of type A.)

@cdsousa
Copy link
Contributor Author

cdsousa commented Sep 12, 2015

So this resumes to

t() = Val{123}
n() = 456
f_t() = Val{t()}
f_n() = Val{n()}
@code_warntype f_t()  # --->   end::Type{Val{Val{123}}}  type inferred
@code_warntype f_n()  # --->   end::TYPE{_<:VAL{T}}      type not inferred

@yuyichao, are you sure this is the same issue of #11159? If so, which line of #3440 covers it?

@yuyichao
Copy link
Contributor

are you sure this is the same issue of #11159?

Yes. At least almost.

If so, which line of #3440 covers it?

See Jameson's comment #11159 (comment) .

cdsousa added a commit to cdsousa/Images.jl that referenced this issue Sep 26, 2015
johnnychen94 pushed a commit to johnnychen94/Images.jl that referenced this issue May 21, 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

3 participants