Skip to content

Commit 6f3d29e

Browse files
committed
Random lowering bugfixes
- `init` not provided for possibly-empty `sum()`s in desugaring causing failures for empty ctors, empty tuple destruct - linearize: break_block type error - missing desugaring when if-condition is a block - boundserror in `add_ir_debug_info!` - undef var `ex` in expand_arrow_arglist
1 parent 1609982 commit 6f3d29e

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

src/desugaring.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ function expand_tuple_destruct(ctx, ex)
414414
end
415415

416416
if kind(rhs) == K"tuple"
417-
num_splat = sum(kind(rh) == K"..." for rh in children(rhs))
417+
num_splat = sum(kind(rh) == K"..." for rh in children(rhs); init=0)
418418
if num_splat == 0 && (numchildren(lhs) - num_slurp) > numchildren(rhs)
419419
throw(LoweringError(ex, "More variables on left hand side than right hand in tuple assignment"))
420420
end
@@ -1402,7 +1402,7 @@ function expand_condition(ctx, ex)
14021402
if isblock
14031403
# Special handling so that the rules for `&&` and `||` can be applied
14041404
# to the last statement of a block
1405-
@ast ctx ex [K"block" ex[1:end-1]... test]
1405+
@ast ctx ex [K"block" map(e->expand_forms_2(ctx,e), ex[1:end-1])... test]
14061406
else
14071407
test
14081408
end
@@ -3094,13 +3094,13 @@ function expand_arrow_arglist(ctx, arglist, arrowname)
30943094
if k == K"block"
30953095
@chk numchildren(arglist) == 2
30963096
arglist = @ast ctx arglist [K"tuple"
3097-
ex[1]
3098-
[K"parameters" ex[2]]
3097+
arglist[1]
3098+
[K"parameters" arglist[2]]
30993099
]
31003100
elseif k != K"tuple"
31013101
# `x::Int -> body`
31023102
arglist = @ast ctx arglist [K"tuple"
3103-
ex[1]
3103+
arglist[1]
31043104
]
31053105
end
31063106
@ast ctx arglist [K"call"
@@ -3665,7 +3665,7 @@ function _rewrite_ctor_new_calls(ctx, ex, struct_name, global_struct_name, ctor_
36653665
full_struct_type = if kind(ex[1]) == K"curly"
36663666
# new{A,B}(...)
36673667
new_type_params = ex[1][2:end]
3668-
n_type_splat = sum(kind(t) == K"..." for t in new_type_params)
3668+
n_type_splat = sum(kind(t) == K"..." for t in new_type_params; init=0)
36693669
n_type_nonsplat = length(new_type_params) - n_type_splat
36703670
if n_type_splat == 0 && n_type_nonsplat < length(struct_typevars)
36713671
throw(LoweringError(ex[1], "too few type parameters specified in `new{...}`"))
@@ -3685,7 +3685,7 @@ function _rewrite_ctor_new_calls(ctx, ex, struct_name, global_struct_name, ctor_
36853685
end
36863686
end
36873687
new_args = ex[2:end]
3688-
n_splat = sum(kind(t) == K"..." for t in new_args)
3688+
n_splat = sum(kind(t) == K"..." for t in new_args; init=0)
36893689
n_nonsplat = length(new_args) - n_splat
36903690
n_fields = length(field_types)
36913691
function throw_n_fields_error(desc)

src/eval.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function add_ir_debug_info!(current_codelocs_stack, stmt)
8484
current_codelocs_stack[j][1] != locstk[j][1])
8585
while length(current_codelocs_stack) >= j
8686
info = pop!(current_codelocs_stack)
87-
push!(last(current_codelocs_stack)[2], info)
87+
!isempty(current_codelocs_stack) && push!(last(current_codelocs_stack)[2], info)
8888
end
8989
end
9090
if j > length(locstk)

src/linear_ir.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
703703
if isnothing(outer_target)
704704
delete!(ctx.break_targets, name)
705705
else
706-
ctx.break_targets = outer_target
706+
ctx.break_targets[name] = outer_target
707707
end
708708
emit(ctx, end_label)
709709
if needs_value

test/destructuring.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ let
3636
end
3737
""") == ([1, 2, 3], 4)
3838

39+
@test_throws JuliaLowering.LoweringError JuliaLowering.include_string(test_mod, """
40+
let
41+
(x,) = ()
42+
end
43+
""") == ([1, 2, 3], 4)
44+
@test_throws JuliaLowering.LoweringError JuliaLowering.include_string(test_mod, """
45+
let
46+
(x,y,ys...) = (1,)
47+
end
48+
""")
49+
3950
# Case where indexed_iterate is just iteration
4051
@test JuliaLowering.include_string(test_mod, """
4152
let

test/loops.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ let
9595
a
9696
end
9797
""") == [1, 2]
98+
@test JuliaLowering.include_string(test_mod, """
99+
let
100+
a = []
101+
for i in 1:2
102+
for j in 3:4
103+
push!(a, (i, j))
104+
j == 6 && break
105+
end
106+
end
107+
a
108+
end
109+
""") == [(1, 3), (1, 4), (2, 3), (2, 4)]
98110

99111
# continue
100112
@test JuliaLowering.include_string(test_mod, """

0 commit comments

Comments
 (0)