@@ -437,11 +437,8 @@ mutable struct ExprSplitter
437
437
lnn:: Union{LineNumberNode,Nothing}
438
438
end
439
439
function ExprSplitter (mod:: Module , ex:: Expr ; lnn= nothing )
440
- index = Int[]
441
- if ex. head === :block || ex. head === :toplevel
442
- push! (index, 1 )
443
- end
444
- iter = ExprSplitter ([(mod,ex)], index, lnn)
440
+ iter = ExprSplitter (Tuple{Module,Expr}[], Int[], lnn)
441
+ push_modex! (iter, mod, ex)
445
442
queuenext! (iter)
446
443
return iter
447
444
end
@@ -452,7 +449,15 @@ Base.eltype(::Type{ExprSplitter}) = Tuple{Module,Expr}
452
449
function push_modex! (iter:: ExprSplitter , mod:: Module , ex:: Expr )
453
450
push! (iter. stack, (mod, ex))
454
451
if ex. head === :toplevel || ex. head === :block
455
- push! (iter. index, 1 )
452
+ # Issue #427
453
+ modifies_scope = false
454
+ for a in ex. args
455
+ if isa (a, Expr) && a. head ∈ (:local , :global )
456
+ modifies_scope = true
457
+ break
458
+ end
459
+ end
460
+ push! (iter. index, modifies_scope ? 0 : 1 )
456
461
end
457
462
return iter
458
463
end
@@ -504,6 +509,10 @@ function queuenext!(iter::ExprSplitter)
504
509
elseif head === :block || head === :toplevel
505
510
# Container expression
506
511
idx = iter. index[end ]
512
+ if idx == 0
513
+ # return the whole block (issue #427)
514
+ return nothing
515
+ end
507
516
while idx <= length (ex. args)
508
517
a = ex. args[idx]
509
518
if isa (a, LineNumberNode)
@@ -538,6 +547,15 @@ function Base.iterate(iter::ExprSplitter, state=nothing)
538
547
push_modex! (iter, mod, body)
539
548
end
540
549
end
550
+ if ex. head === :block || ex. head === :toplevel
551
+ # This was a block that we couldn't safely descend into (issue #427)
552
+ if ! isempty (iter. index) && iter. index[end ] > length (iter. stack[end ][2 ]. args)
553
+ pop! (iter. stack)
554
+ pop! (iter. index)
555
+ queuenext! (iter)
556
+ end
557
+ return (mod, ex), nothing
558
+ end
541
559
queuenext! (iter)
542
560
# :global expressions can't be lowered. For debugging it might be nice
543
561
# to still return the lnn, but then we have to work harder on detecting them.
0 commit comments