Skip to content

Commit 58f4699

Browse files
IanButterworthKristofferC
authored andcommitted
Profile: remove scope from profile macros (#57858)
Fixes this, which breaks expectations from the way `@time` doesn't introduce a new scope. ``` julia> using Profile julia> @Profile x = 1 1 julia> x ERROR: UndefVarError: `x` not defined in `Main` Suggestion: check for spelling errors or missing imports. ``` (cherry picked from commit d6af199)
1 parent 65d937a commit 58f4699

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

stdlib/Profile/src/Allocs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ end
7979
function _prof_expr(expr, opts)
8080
quote
8181
$start(; $(esc(opts)))
82-
try
82+
Base.@__tryfinally(
8383
$(esc(expr))
84-
finally
84+
,
8585
$stop()
86-
end
86+
)
8787
end
8888
end
8989

stdlib/Profile/src/Profile.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ appended to an internal buffer of backtraces.
5454
"""
5555
macro profile(ex)
5656
return quote
57-
try
58-
start_timer()
57+
start_timer()
58+
Base.@__tryfinally(
5959
$(esc(ex))
60-
finally
60+
,
6161
stop_timer()
62-
end
62+
)
6363
end
6464
end
6565

stdlib/Profile/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ end
107107
@test z == 10
108108
end
109109

110+
@testset "@profile no scope" begin
111+
@profile no_scope_57858_1 = 1
112+
@test @isdefined no_scope_57858_1
113+
Profile.clear()
114+
115+
@profile_walltime no_scope_57858_1 = 1
116+
@test @isdefined no_scope_57858_1
117+
Profile.clear()
118+
119+
Profile.Allocs.@profile no_scope_57858_2 = 1
120+
@test @isdefined no_scope_57858_2
121+
Profile.Allocs.clear()
122+
end
123+
110124
@testset "setting sample count and delay in init" begin
111125
n_, delay_ = Profile.init()
112126
n_original = n_

0 commit comments

Comments
 (0)