Scoping in nested functions - Why am I not getting an error?

I just noticed that this isn’t type stable.

function baz()
    local x
    function bar()
        x = 2
    end
    bar()
    x
end

baz() # returns 2

image

Is there a better way of doing this?