I am having trouble understanding why fun1 has a type instability, and not fun2, is this a bug?

function fun1(N::Integer, a::Number, test::Bool=false)
if test
b = exp(-abs2(a)/2)
x = [(3b^n+2)/(n+1) for n = 0:N-1]
return x
else
return rand(N)
end
end
function fun2(N::Integer, a::Number, test::Bool=false)
b = exp(-abs2(a)/2)
x = [(3b^n+2)/(n+1) for n = 0:N-1]
return x
end

Helping Julia think about b looks to me like the best option. Consider,

function fun1(N::Integer, a::Number, test::Bool=false)
if test let b = exp(-abs2(a)/2)
x = [(3b^n+2)/(n+1) for n = 0:N-1]
return x
end else
return rand(N)
end
end

This way, not only is fun1 type-stable, but b is not boxed and not allocated when test==false. Also, having the let on the same line as the if and the end with the else makes the code almost un-ugly.