Parser bug regarding parametric function expressions?

Some weird behavior here.

Suppose we have this:

SomeType = Tuple{Int8, Int16}
f(::Type{Tuple{A, B}}) where {A <: Any, B <: Any} = B

If we now do f(SomeType), we get Int16 in return as expected.

The following however, results in ERROR: UndefVarError: B not defined on both 1.7.3 and on 1.8.0-beta3:

(function(::Type{Tuple{A, B}}) where {A <: Any, B <: Any}
   B
 end)(SomeType)

EDIT: Github issue: UndefVarError when trying to use parametric function expression · Issue #45506 · JuliaLang/julia · GitHub

Try this:

julia> SomeType = Tuple{Int8, Int16}
Tuple{Int8, Int16}

julia> (function (::Type{Tuple{A, B}}) where A where B
          B
        end)(SomeType)
Int16

or this:

julia> (function (::Type{Tuple{A, B}} where A) where B
          B
        end)(SomeType)
Int16

It possibly a bug, since adding an function identifier also makes this work, but I’m also suspecting a potential operator precedence issue.

julia> (function f(::Type{Tuple{A, B}}) where {A,B}
          B
        end)(SomeType)
Int16

This does look suspciously like a bug though.

julia> :(function(::Type{Tuple{A, B}}) where {A, B}
          B
        end)
:(function (::Type{Tuple{A, B}},) where A
      #= REPL[24]:1 =#
      #= REPL[24]:2 =#
      B
  end)
1 Like

Reportedly this issue is fixed now.

The bugfix is tiny too: it just adds two characters in a single line: