Skip to content

Commit abe66bb

Browse files
committed
Remove wrap throughout desugaring
As far as I'm aware, all this plumbing was just to support `const a,b,c = 1,2,3` having `b` and `c` inherit the `const`.
1 parent 437e74a commit abe66bb

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

src/desugaring.jl

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end
103103
# constructed followed by destructuring. In particular, any side effects due to
104104
# evaluating the individual terms in the right hand side tuple must happen in
105105
# order.
106-
function tuple_to_assignments(ctx, ex, wrap)
106+
function tuple_to_assignments(ctx, ex)
107107
lhs = ex[1]
108108
rhs = ex[2]
109109

@@ -182,23 +182,23 @@ function tuple_to_assignments(ctx, ex, wrap)
182182
# (x, ys...) = (a,b,c)
183183
# (x, ys...) = (a,bs...)
184184
# (ys...) = ()
185-
push!(stmts, wrap(@ast ctx ex [K"=" lh[1] middle]))
185+
push!(stmts, @ast ctx ex [K"=" lh[1] middle])
186186
else
187187
# (x, ys..., z) = (a, b, c, d)
188188
# (x, ys..., z) = (a, bs...)
189189
# (xs..., y) = (a, bs...)
190-
push!(stmts, wrap(@ast ctx ex [K"=" [K"tuple" lhs[il:jl]...] middle]))
190+
push!(stmts, @ast ctx ex [K"=" [K"tuple" lhs[il:jl]...] middle])
191191
end
192192
# Continue with the remainder of the list of non-splat terms
193193
il = jl
194194
ir = jr
195195
else
196196
rh = rhs_tmps[ir]
197197
if kind(rh) == K"..."
198-
push!(stmts, wrap(@ast ctx ex [K"=" [K"tuple" lhs[il:end]...] rh[1]]))
198+
push!(stmts, @ast ctx ex [K"=" [K"tuple" lhs[il:end]...] rh[1]])
199199
break
200200
else
201-
push!(stmts, wrap(@ast ctx ex [K"=" lh rh]))
201+
push!(stmts, @ast ctx ex [K"=" lh rh])
202202
end
203203
end
204204
end
@@ -252,14 +252,14 @@ end
252252

253253
# Lower `(lhss...) = rhs` in contexts where `rhs` must be a tuple at runtime
254254
# by assuming that `getfield(rhs, i)` works and is efficient.
255-
function lower_tuple_assignment(ctx, assignment_srcref, lhss, rhs, wrap=(x::SyntaxTree,i::Int)->x)
255+
function lower_tuple_assignment(ctx, assignment_srcref, lhss, rhs)
256256
stmts = SyntaxList(ctx)
257257
tmp = emit_assign_tmp(stmts, ctx, rhs, "rhs_tmp")
258258
for (i, lh) in enumerate(lhss)
259-
stmt = @ast ctx assignment_srcref [
260-
K"=" lh [K"call" "getfield"::K"core" tmp i::K"Integer"]
261-
]
262-
push!(stmts, wrap(stmt, i))
259+
push!(stmts, @ast ctx assignment_srcref [K"="
260+
lh
261+
[K"call" "getfield"::K"core" tmp i::K"Integer"]
262+
])
263263
end
264264
makenode(ctx, assignment_srcref, K"block", stmts)
265265
end
@@ -270,10 +270,7 @@ end
270270
# Destructuring in this context is done via the iteration interface, though
271271
# calls `Base.indexed_iterate()` to allow for a fast path in cases where the
272272
# right hand side is directly indexable.
273-
#
274-
# The `wrap` argument is a callback that will be called on all assignments to
275-
# symbols `lhss`, e.g. to insert a `const` declaration.
276-
function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
273+
function _destructure(ctx, assignment_srcref, stmts, lhs, rhs)
277274
n_lhs = numchildren(lhs)
278275
if n_lhs > 0
279276
iterstate = new_local_binding(ctx, rhs, "iterstate")
@@ -284,24 +281,20 @@ function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
284281
i = 0
285282
for lh in children(lhs)
286283
i += 1
287-
local lh1
288284
if kind(lh) == K"..."
289-
if is_identifier_like(lh[1])
290-
wrap_subassign = wrap
291-
lh1 = lh[1]
285+
lh1 = if is_identifier_like(lh[1])
286+
lh[1]
292287
else
293-
wrap_subassign = identity
294-
lh1 = ssavar(ctx, lh[1], "lh1")
295-
push!(end_stmts, expand_forms_2(ctx, wrap(@ast ctx lh[1] [K"=" lh[1] lh1])))
288+
lhs_tmp = ssavar(ctx, lh[1], "lhs_tmp")
289+
push!(end_stmts, expand_forms_2(ctx, @ast ctx lh[1] [K"=" lh[1] lhs_tmp]))
290+
lhs_tmp
296291
end
297292
if i == n_lhs
298293
# Slurping as last lhs, eg, for `zs` in
299294
# (x, y, zs...) = rhs
300295
if kind(lh1) != K"Placeholder"
301-
push!(stmts, expand_forms_2(
302-
ctx,
303-
wrap_subassign(
304-
@ast ctx assignment_srcref [K"="
296+
push!(stmts, expand_forms_2(ctx,
297+
@ast ctx assignment_srcref [K"="
305298
lh1
306299
[K"call"
307300
"rest"::K"top"
@@ -310,7 +303,7 @@ function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
310303
iterstate
311304
end
312305
]
313-
])
306+
]
314307
))
315308
end
316309
else
@@ -325,15 +318,15 @@ function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
325318
lower_tuple_assignment(ctx,
326319
assignment_srcref,
327320
(lh1, tail),
328-
(@ast ctx assignment_srcref [K"call"
321+
@ast ctx assignment_srcref [K"call"
329322
"split_rest"::K"top"
330323
rhs
331324
(n_lhs - i)::K"Integer"
332325
if i > 1
333326
iterstate
334327
end
335-
]),
336-
(x,i) -> i == 1 ? wrap_subassign(x) : x)
328+
]
329+
)
337330
)
338331
)
339332
rhs = tail
@@ -343,29 +336,28 @@ function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
343336
else
344337
# Normal case, eg, for `y` in
345338
# (x, y, z) = rhs
346-
if is_identifier_like(lh)
347-
lh1 = lh
348-
wrap_subassign = wrap
339+
lh1 = if is_identifier_like(lh)
340+
lh
349341
# elseif is_eventually_call(lh) (TODO??)
350342
else
351-
lh1 = ssavar(ctx, lh, "lh1")
352-
wrap_subassign = identity
353-
push!(end_stmts, expand_forms_2(ctx, wrap(@ast ctx lh [K"=" lh lh1])))
343+
lhs_tmp = ssavar(ctx, lh, "lhs_tmp")
344+
push!(end_stmts, expand_forms_2(ctx, @ast ctx lh [K"=" lh lhs_tmp]))
345+
lhs_tmp
354346
end
355347
push!(stmts,
356348
expand_forms_2(ctx,
357349
lower_tuple_assignment(ctx,
358350
assignment_srcref,
359351
i == n_lhs ? (lh1,) : (lh1, iterstate),
360-
(@ast ctx assignment_srcref [K"call"
352+
@ast ctx assignment_srcref [K"call"
361353
"indexed_iterate"::K"top"
362354
rhs
363355
i::K"Integer"
364356
if i > 1
365357
iterstate
366358
end
367-
]),
368-
(x,i) -> i == 1 ? wrap_subassign(x) : x)
359+
]
360+
)
369361
)
370362
)
371363
end
@@ -377,7 +369,7 @@ function _destructure(ctx, assignment_srcref, stmts, lhs, rhs, wrap)
377369
end
378370

379371
# Expands cases of property destructuring
380-
function expand_property_destruct(ctx, ex, wrap=identity)
372+
function expand_property_destruct(ctx, ex)
381373
@assert numchildren(ex) == 2
382374
lhs = ex[1]
383375
@assert kind(lhs) == K"tuple"
@@ -393,25 +385,22 @@ function expand_property_destruct(ctx, ex, wrap=identity)
393385
propname = kind(prop) == K"Identifier" ? prop :
394386
kind(prop) == K"::" && kind(prop[1]) == K"Identifier" ? prop[1] :
395387
throw(LoweringError(prop, "invalid assignment location"))
396-
push!(stmts, expand_forms_2(
397-
ctx,
398-
wrap(@ast ctx rhs1 [
399-
K"="
400-
prop
401-
[K"call"
402-
"getproperty"::K"top"
403-
rhs1
404-
propname=>K"Symbol"
405-
]
406-
])))
388+
push!(stmts, expand_forms_2(ctx, @ast ctx rhs1 [K"="
389+
prop
390+
[K"call"
391+
"getproperty"::K"top"
392+
rhs1
393+
propname=>K"Symbol"
394+
]
395+
]))
407396
end
408397
push!(stmts, @ast ctx rhs1 [K"removable" rhs1])
409398
makenode(ctx, ex, K"block", stmts)
410399
end
411400

412401
# Expands all cases of general tuple destructuring, eg
413402
# (x,y) = (a,b)
414-
function expand_tuple_destruct(ctx, ex, wrap=identity)
403+
function expand_tuple_destruct(ctx, ex)
415404
lhs = ex[1]
416405
@assert kind(lhs) == K"tuple"
417406
rhs = ex[2]
@@ -432,7 +421,7 @@ function expand_tuple_destruct(ctx, ex, wrap=identity)
432421

433422
if !any_assignment(children(rhs)) && !has_parameters(rhs) &&
434423
_tuple_sides_match(children(lhs), children(rhs))
435-
return expand_forms_2(ctx, tuple_to_assignments(ctx, ex, wrap))
424+
return expand_forms_2(ctx, tuple_to_assignments(ctx, ex))
436425
end
437426
end
438427

@@ -445,7 +434,7 @@ function expand_tuple_destruct(ctx, ex, wrap=identity)
445434
else
446435
emit_assign_tmp(stmts, ctx, expand_forms_2(ctx, rhs))
447436
end
448-
_destructure(ctx, ex, stmts, lhs, rhs1, wrap)
437+
_destructure(ctx, ex, stmts, lhs, rhs1)
449438
push!(stmts, @ast ctx rhs1 [K"removable" rhs1])
450439
makenode(ctx, ex, K"block", stmts)
451440
end
@@ -1277,7 +1266,7 @@ function expand_assignment(ctx, ex, is_const=false)
12771266
]
12781267
elseif kl == K"tuple"
12791268
if has_parameters(lhs)
1280-
expand_property_destruct(ctx, ex, maybe_wrap_const)
1269+
expand_property_destruct(ctx, ex)
12811270
else
12821271
expand_tuple_destruct(ctx, ex)
12831272
end
@@ -1297,10 +1286,9 @@ function expand_assignment(ctx, ex, is_const=false)
12971286
elseif is_identifier_like(x)
12981287
# Identifer in lhs[1] is a variable type declaration, eg
12991288
# x::T = rhs
1300-
@ast ctx ex [
1301-
K"block"
1289+
@ast ctx ex [K"block"
13021290
[K"decl" lhs[1] lhs[2]]
1303-
maybe_wrap_const([K"=" lhs[1] rhs])
1291+
is_const ? [K"const" [K"=" lhs[1] rhs]] : [K"=" lhs[1] rhs]
13041292
]
13051293
else
13061294
# Otherwise just a type assertion, eg
@@ -1312,7 +1300,7 @@ function expand_assignment(ctx, ex, is_const=false)
13121300
# needs to be detected somewhere but won't be detected here. Maybe
13131301
# it shows that remove_argument_side_effects() is not the ideal
13141302
# solution here?
1315-
# TODO: handle underscore
1303+
# TODO: handle underscore?
13161304
@ast ctx ex [K"block"
13171305
stmts...
13181306
[K"::" l1 lhs[2]]

0 commit comments

Comments
 (0)