diff --git a/base/util.jl b/base/util.jl index 4022167baf206..23a4e7f2ee870 100644 --- a/base/util.jl +++ b/base/util.jl @@ -227,7 +227,7 @@ macro timed(ex) local elapsedtime = time_ns() local val = $(esc(ex)) elapsedtime = time_ns() - elapsedtime - diff = GC_Diff(gc_num(), stats) + local diff = GC_Diff(gc_num(), stats) val, elapsedtime/1e9, diff.total_allocd + diff.allocd, diff.total_time/1e9, diff end end diff --git a/src/jlfrontend.scm b/src/jlfrontend.scm index 6e5187e5d3d6c..dcd5e73380950 100644 --- a/src/jlfrontend.scm +++ b/src/jlfrontend.scm @@ -65,9 +65,7 @@ ;; vars assigned anywhere, if they have been defined as global (filter defined-julia-global (find-possible-globals e)))) (append - (if (null? (find-decls 'local e)) - '() - (error "local declaration in global scope")) + (find-decls 'local e) (find-decls 'local! e)))) ;; return a lambda expression representing a thunk for a top-level expression diff --git a/test/misc.jl b/test/misc.jl index 72da0aa902139..04b9ea07e3515 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -142,3 +142,28 @@ let l = ReentrantLock() unlock(l) @test_throws ErrorException unlock(l) end + +# timing macros + +# test that they don't introduce global vars +global v11801, t11801, names_before_timing +names_before_timing = names(current_module(), true) + +let t = @elapsed 1+1 + @test isa(t, Real) && t >= 0 +end + +let + val, t = @timed sin(1) + @test val == sin(1) + @test isa(t, Real) && t >= 0 +end + +# problem after #11801 - at global scope +t11801 = @elapsed 1+1 +@test isa(t11801,Real) && t11801 >= 0 +v11801, t11801 = @timed sin(1) +@test v11801 == sin(1) +@test isa(t11801,Real) && t11801 >= 0 + +@test names(current_module(), true) == names_before_timing