Skip to content

Commit 2fbe680

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 1408e11 commit 2fbe680

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
@@ -73,11 +73,11 @@ end
7373
function _prof_expr(expr, opts)
7474
quote
7575
$start(; $(esc(opts)))
76-
try
76+
Base.@__tryfinally(
7777
$(esc(expr))
78-
finally
78+
,
7979
$stop()
80-
end
80+
)
8181
end
8282
end
8383

stdlib/Profile/src/Profile.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ appended to an internal buffer of backtraces.
3939
"""
4040
macro profile(ex)
4141
return quote
42-
try
43-
start_timer()
42+
start_timer()
43+
Base.@__tryfinally(
4444
$(esc(ex))
45-
finally
45+
,
4646
stop_timer()
47-
end
47+
)
4848
end
4949
end
5050

stdlib/Profile/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ end
117117
@test z == 10
118118
end
119119

120+
@testset "@profile no scope" begin
121+
@profile no_scope_57858_1 = 1
122+
@test @isdefined no_scope_57858_1
123+
Profile.clear()
124+
125+
@profile_walltime no_scope_57858_1 = 1
126+
@test @isdefined no_scope_57858_1
127+
Profile.clear()
128+
129+
Profile.Allocs.@profile no_scope_57858_2 = 1
130+
@test @isdefined no_scope_57858_2
131+
Profile.Allocs.clear()
132+
end
133+
120134
@testset "setting sample count and delay in init" begin
121135
n_, delay_ = Profile.init()
122136
n_original = n_

0 commit comments

Comments
 (0)