Skip to content

Commit a825f56

Browse files
authored
Truncate struct def blocks prior to final :global (#60)
Fixes timholy/Revise.jl#599
1 parent 428c018 commit a825f56

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LoweredCodeUtils"
22
uuid = "6f1432cf-f94c-5a45-995e-cdbf5db27b0b"
33
authors = ["Tim Holy <tim.holy@gmail.com>"]
4-
version = "1.2.7"
4+
version = "1.2.8"
55

66
[deps]
77
JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"

src/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ end
117117
function typedef_range(src::CodeInfo, idx)
118118
stmt = src.code[idx]
119119
istypedef(stmt) || error(stmt, " is not a typedef")
120+
stmt = stmt::Expr
120121
isanonymous_typedef(stmt) && return idx:idx
121122
# Search backwards to the previous :global
122123
istart = idx
@@ -125,11 +126,17 @@ function typedef_range(src::CodeInfo, idx)
125126
istart -= 1
126127
end
127128
istart >= 1 || error("no initial :global found")
129+
stmt.head structheads && return istart:idx
128130
iend, n = idx, length(src.code)
129131
while iend <= n
130132
stmt = src.code[iend]
131133
if isa(stmt, Expr)
132134
(stmt.head === :global || stmt.head === :return) && break
135+
if stmt.head === :call && (is_global_ref(stmt.args[1], Core, :_typebody!) ||
136+
isdefined(Core, :_typebody!) && is_quotenode(stmt.args[1], Core._typebody!))
137+
iend += 1 # compensate for the `iend-1` in the return
138+
break
139+
end
133140
end
134141
is_return(stmt) && break
135142
iend += 1

test/codeedges.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ end
289289
idx = findfirst(stmt->Meta.isexpr(stmt, :leave), src.code)
290290
@test lr[idx]
291291

292+
# https://github.com/timholy/Revise.jl/issues/599
293+
thk = Meta.lower(Main, quote
294+
mutable struct A
295+
x::Int
296+
297+
A(x) = new(f(x))
298+
f(x) = x^2
299+
end
300+
end)
301+
src = thk.args[1]
302+
edges = CodeEdges(src)
303+
idx = findfirst(stmt->Meta.isexpr(stmt, :method), src.code)
304+
lr = lines_required(idx, src, edges; exclude_named_typedefs=true)
305+
idx = findfirst(stmt->Meta.isexpr(stmt, :(=)) && Meta.isexpr(stmt.args[2], :call) && is_global_ref(stmt.args[2].args[1], Core, :Box), src.code)
306+
@test lr[idx]
307+
292308
@testset "Display" begin
293309
# worth testing because this has proven quite crucial for debugging and
294310
# ensuring that these structures are as "self-documenting" as possible.

0 commit comments

Comments
 (0)