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

Specialization for keyword arguments of the form Type{T} #39866

Closed
thofma opened this issue Mar 1, 2021 · 3 comments
Closed

Specialization for keyword arguments of the form Type{T} #39866

thofma opened this issue Mar 1, 2021 · 3 comments

Comments

@thofma
Copy link
Contributor

thofma commented Mar 1, 2021

I observed some unexpected specialization behavior with keyword arguments.

julia> function f(x; as::Type{T} = Int) where {T}
         return T(x)
       end

Then

julia> @code_warntype f(1, as = Float64)

seems to indicate that it is not really specializing, but juggling around with DataType. I would have expected this with just as::Type = Int because of some specialization heuristic I know from positional arguments.

Is this intended? Or maybe @code_warntype is lying to me? Reproduced on julia 1.5 and 6 days old master.

Edit: Maybe this is #37797? Don't know enough about the internals.

@jakobnissen
Copy link
Contributor

jakobnissen commented Mar 1, 2021

Duplicate of #32834, I think. Edit: Nope, I mistook this issue.

@thofma
Copy link
Contributor Author

thofma commented Mar 2, 2021

Why? OP in #32834 says

@code_typed shows you fully specialized code, even though in reality this call will not be specialized

On the other hand I am asking why my example does not specialize (in my example @code_warntype is not showing fully specialized code as in #32834).

@vtjnash
Copy link
Sponsor Member

vtjnash commented Mar 10, 2023

This is just classic code_warntype misuse. It infers fine.

julia> code_warntype(()) do;  f(1, as = Float64); end
MethodInstance for (::var"#6#7")()
  from (::var"#6#7")() @ Main REPL[3]:1
Arguments
  #self#::Core.Const(var"#6#7"())
Body::Float64
    @ REPL[3]:1 within `#6`
1 ─ %1 = (:as,)::Core.Const((:as,))
│   %2 = Core.apply_type(Core.NamedTuple, %1)::Core.Const(NamedTuple{(:as,)})
│   %3 = Core.tuple(Main.Float64)::Core.Const((Float64,))
│   %4 = (%2)(%3)::Core.Const((as = Float64,))
│   %5 = Core.kwcall(%4, Main.f, 1)::Core.Const(1.0)
└──      return %5

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