<p dir="auto">I have run into an unexpected problem with (a more complicated version of) the following code:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; function f(x::Bool)
           if x
               function g(y)
                   return y
               end
               return g
           else
               function g(y)
                   return y + 1
               end
               return g
           end
       end
f (generic function with 1 method)

julia&gt; f(true)
(::var&quot;#g#7&quot;) (generic function with 1 method)

julia&gt; f(false)
ERROR: UndefVarError: g not defined
Stacktrace:
 [1] f(::Bool) at ./REPL[1]:11
 [2] top-level scope at REPL[3]:1"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-k">function</span> <span class="pl-en">f</span>(x<span class="pl-k">::</span><span class="pl-c1">Bool</span>)
           <span class="pl-k">if</span> x
               <span class="pl-k">function</span> <span class="pl-en">g</span>(y)
                   <span class="pl-k">return</span> y
               <span class="pl-k">end</span>
               <span class="pl-k">return</span> g
           <span class="pl-k">else</span>
               <span class="pl-k">function</span> <span class="pl-en">g</span>(y)
                   <span class="pl-k">return</span> y <span class="pl-k">+</span> <span class="pl-c1">1</span>
               <span class="pl-k">end</span>
               <span class="pl-k">return</span> g
           <span class="pl-k">end</span>
       <span class="pl-k">end</span>
f (generic <span class="pl-k">function</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-c1">f</span>(<span class="pl-c1">true</span>)
(<span class="pl-k">::</span><span class="pl-c1">var"#g#7"</span>) (generic <span class="pl-k">function</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-c1">f</span>(<span class="pl-c1">false</span>)
ERROR<span class="pl-k">:</span> UndefVarError<span class="pl-k">:</span> g not defined
Stacktrace<span class="pl-k">:</span>
 [<span class="pl-c1">1</span>] <span class="pl-c1">f</span>(<span class="pl-k">::</span><span class="pl-c1">Bool</span>) at <span class="pl-k">./</span>REPL[<span class="pl-c1">1</span>]<span class="pl-k">:</span><span class="pl-c1">11</span>
 [<span class="pl-c1">2</span>] top<span class="pl-k">-</span>level scope at REPL[<span class="pl-c1">3</span>]<span class="pl-k">:</span><span class="pl-c1">1</span></pre></div>
<p dir="auto">I had assumed that closure notation <code class="notranslate">function f(x) ... end</code> was intended to be equivalent to <code class="notranslate">f = (x) -&gt; ...</code> (well, the former may allow you to attach an addition method to the function or closure, also). Lowering has generated the following code:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @code_lowered f(true)
CodeInfo(
1 ─     Core.NewvarNode(:(g))
└──     goto #3 if not x
2 ─     g = %new(Main.:(var&quot;#g#7&quot;))
└──     return g
3 ─     return g
)"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@code_lowered</span> <span class="pl-c1">f</span>(<span class="pl-c1">true</span>)
<span class="pl-c1">CodeInfo</span>(
<span class="pl-c1">1</span> ─     Core<span class="pl-k">.</span><span class="pl-c1">NewvarNode</span>(:(g))
└──     goto <span class="pl-c"><span class="pl-c">#</span>3 if not x</span>
<span class="pl-c1">2</span> ─     g <span class="pl-k">=</span> <span class="pl-k">%</span><span class="pl-c1">new</span>(Main.:(<span class="pl-c1">var"#g#7"</span>))
└──     <span class="pl-k">return</span> g
<span class="pl-c1">3</span> ─     <span class="pl-k">return</span> g
)</pre></div>
<p dir="auto">It seems to have removed the second closure assignment entirely. I expected it to behave like the following assignment form (even with "long" function notation):</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; function f(x::Bool)
           if x
               g = function (y)
                   return y
               end
               return g
           else
               g = function (y)
                   return y + 1
               end
               return g
           end
       end
f (generic function with 1 method)

julia&gt; @code_lowered f(true)
CodeInfo(
1 ─     Core.NewvarNode(:(#8))
│       Core.NewvarNode(:(#9))
│       Core.NewvarNode(:(g))
└──     goto #3 if not x
2 ─     #8 = %new(Main.:(var&quot;#8#10&quot;))
│       g = #8
└──     return g
3 ─     #9 = %new(Main.:(var&quot;#9#11&quot;))
│       g = #9
└──     return g
)
"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-k">function</span> <span class="pl-en">f</span>(x<span class="pl-k">::</span><span class="pl-c1">Bool</span>)
           <span class="pl-k">if</span> x
               g <span class="pl-k">=</span> <span class="pl-k">function</span> (y)
                   <span class="pl-k">return</span> y
               <span class="pl-k">end</span>
               <span class="pl-k">return</span> g
           <span class="pl-k">else</span>
               g <span class="pl-k">=</span> <span class="pl-k">function</span> (y)
                   <span class="pl-k">return</span> y <span class="pl-k">+</span> <span class="pl-c1">1</span>
               <span class="pl-k">end</span>
               <span class="pl-k">return</span> g
           <span class="pl-k">end</span>
       <span class="pl-k">end</span>
f (generic <span class="pl-k">function</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@code_lowered</span> <span class="pl-c1">f</span>(<span class="pl-c1">true</span>)
<span class="pl-c1">CodeInfo</span>(
<span class="pl-c1">1</span> ─     Core<span class="pl-k">.</span><span class="pl-c1">NewvarNode</span>(:(<span class="pl-c"><span class="pl-c">#</span>8))</span>
│       Core<span class="pl-k">.</span><span class="pl-c1">NewvarNode</span>(:(<span class="pl-c"><span class="pl-c">#</span>9))</span>
│       Core<span class="pl-k">.</span><span class="pl-c1">NewvarNode</span>(:(g))
└──     goto <span class="pl-c"><span class="pl-c">#</span>3 if not x</span>
<span class="pl-c1">2</span> ─     <span class="pl-c"><span class="pl-c">#</span>8 = %new(Main.:(var"#8#10"))</span>
│       g <span class="pl-k">=</span> <span class="pl-c"><span class="pl-c">#</span>8</span>
└──     <span class="pl-k">return</span> g
<span class="pl-c1">3</span> ─     <span class="pl-c"><span class="pl-c">#</span>9 = %new(Main.:(var"#9#11"))</span>
│       g <span class="pl-k">=</span> <span class="pl-c"><span class="pl-c">#</span>9</span>
└──     <span class="pl-k">return</span> g
)
</pre></div>
<p dir="auto">Reproduce with</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; run(pipeline(`ls`, stdout=IOBuffer()))
ERROR: MethodError: `uvhandle` has no method matching uvhandle(::Base.AbstractIOBuffer{Array{UInt8,1}})
 in _jl_spawn at process.jl:253
 in anonymous at process.jl:415
 in setup_stdio at process.jl:403
 in spawn at process.jl:414
 in spawn at process.jl:293
 in run at process.jl:530"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">run</span>(<span class="pl-c1">pipeline</span>(<span class="pl-s"><span class="pl-pds">`</span>ls<span class="pl-pds">`</span></span>, <span class="pl-c1">stdout</span><span class="pl-k">=</span><span class="pl-c1">IOBuffer</span>()))
ERROR<span class="pl-k">:</span> MethodError<span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">`</span>uvhandle<span class="pl-pds">`</span></span> has no method matching <span class="pl-c1">uvhandle</span>(<span class="pl-k">::</span><span class="pl-c1">Base.AbstractIOBuffer{Array{UInt8,1}}</span>)
 <span class="pl-k">in</span> _jl_spawn at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">253</span>
 <span class="pl-k">in</span> anonymous at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">415</span>
 <span class="pl-k">in</span> setup_stdio at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">403</span>
 <span class="pl-k">in</span> spawn at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">414</span>
 <span class="pl-k">in</span> spawn at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">293</span>
 <span class="pl-k">in</span> run at process<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">530</span></pre></div>