Skip to content

Commit 8e7bc39

Browse files
committed
Move most latestworlds to linearization
From the docs: ``` The following statements raise the current world age: 1. An explicit invocation of Core.@latestworld 2. The start of every top-level statement 3. The start of every REPL prompt 4. Any type or struct definition 5. Any method definition 6. Any constant declaration 7. Any global variable declaration (but not a global variable assignment) 8. Any using, import, export or public statement 9. Certain other macros like eval (depends on the macro implementation) ``` This commit handles each case as follows: ``` 1. = 9 2. I'm not sure this actually happens (or needs to happen, unless we're being defensive? Doing it after each world-changing operation should suffice). But if we need it, this would just be emitting once at the beginning of every lowered output. 3. = 2 4. = 6 5. Emit seeing `method` in linearize 6. Emit seeing `constdecl` in linearize 7. Emit seeing `global` or `globaldecl` in linearize 8. We just defer to `eval`, but should probably go in desugaring later - using/import recently became builtin calls, and I haven't updated JL to use them yet. Base._import_using has an expr-based API that may change, and our importpath destructuring is worth keeping. - export and public (special forms) are handled in toplevel.c 9. Done for us ``` Other quirks: - `JuliaLowering.eval_closure_type` calls eval to assign a const, so we still need to deal with that in closure conversion. - The `include` hack isn't mentioned in the docs, but can stay in desugaring. I'm not certain why we don't do the same for non-macro `eval`.
1 parent 89fdf02 commit 8e7bc39

File tree

9 files changed

+526
-552
lines changed

9 files changed

+526
-552
lines changed

src/desugaring.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,6 @@ function expand_unionall_def(ctx, srcref, lhs, rhs, is_const=true)
11741174
@ast ctx srcref [K"block"
11751175
rr := [K"where" rhs lhs[2:end]...]
11761176
[is_const ? K"constdecl" : K"assign_or_constdecl_if_global" name rr]
1177-
[K"latestworld_if_toplevel"]
11781177
[K"removable" rr]
11791178
]
11801179
)
@@ -1229,7 +1228,6 @@ function expand_assignment(ctx, ex, is_const=false)
12291228
@ast ctx ex [K"block"
12301229
rr := expand_forms_2(ctx, rhs)
12311230
[K"constdecl" lhs rr]
1232-
[K"latestworld"]
12331231
[K"removable" rr]
12341232
]
12351233
else
@@ -2293,7 +2291,6 @@ function method_def_expr(ctx, srcref, callex_srcref, method_table,
22932291
ret_var # might be `nothing` and hence removed
22942292
]
22952293
]
2296-
[K"latestworld"]
22972294
[K"removable" method_metadata]
22982295
]
22992296
end
@@ -2422,12 +2419,10 @@ function expand_function_generator(ctx, srcref, callex_srcref, func_name, func_n
24222419
# Code generator definition
24232420
gen_func_method_defs = @ast ctx srcref [K"block"
24242421
[K"function_decl" gen_name]
2425-
[K"latestworld_if_toplevel"]
24262422
[K"scope_block"(scope_type=:hard)
24272423
[K"method_defs"
24282424
gen_name
24292425
[K"block"
2430-
[K"latestworld_if_toplevel"]
24312426
method_def_expr(ctx, srcref, callex_srcref, nothing, SyntaxList(ctx),
24322427
gen_arg_names, gen_arg_types, gen_body, nothing)
24332428
]
@@ -2761,7 +2756,6 @@ function keyword_function_defs(ctx, srcref, callex_srcref, name_str, typevar_nam
27612756

27622757
kw_func_method_defs = @ast ctx srcref [K"block"
27632758
[K"function_decl" body_func_name]
2764-
[K"latestworld"]
27652759
[K"scope_block"(scope_type=:hard)
27662760
[K"method_defs"
27672761
body_func_name
@@ -2831,7 +2825,6 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
28312825
end
28322826
return @ast ctx ex [K"block"
28332827
[K"function_decl" name]
2834-
[K"latestworld"]
28352828
name
28362829
]
28372830
end
@@ -3068,7 +3061,6 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
30683061
end
30693062
gen_func_method_defs
30703063
kw_func_method_defs
3071-
[K"latestworld_if_toplevel"]
30723064
[K"scope_block"(scope_type=:hard)
30733065
[K"method_defs"
30743066
isnothing(bare_func_name) ? "nothing"::K"core" : bare_func_name
@@ -3386,7 +3378,6 @@ function expand_abstract_or_primitive_type(ctx, ex)
33863378
nothing_(ctx, ex)
33873379
[K"constdecl" name newtype_var]
33883380
]
3389-
[K"latestworld"]
33903381
nothing_(ctx, ex)
33913382
]
33923383
end
@@ -3961,7 +3952,6 @@ function expand_struct_def(ctx, ex, docs)
39613952
global_struct_name
39623953
newdef
39633954
]
3964-
[K"latestworld"]
39653955
# Default constructors
39663956
if isempty(inner_defs)
39673957
default_inner_constructors(ctx, ex, global_struct_name,

src/linear_ir.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
657657
end
658658
elseif k == K"=" || k == K"constdecl"
659659
lhs = ex[1]
660-
if kind(lhs) == K"Placeholder"
660+
res = if kind(lhs) == K"Placeholder"
661661
compile(ctx, ex[2], needs_value, in_tail_pos)
662662
else
663663
rhs = compile(ctx, ex[2], true, false)
@@ -674,6 +674,8 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
674674
emit_assignment(ctx, ex, lhs, rhs, k)
675675
end
676676
end
677+
k == K"constdecl" && emit_latestworld(ctx, ex)
678+
res
677679
elseif k == K"block" || k == K"scope_block"
678680
nc = numchildren(ex)
679681
if nc == 0
@@ -782,7 +784,7 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
782784
# TODO
783785
# throw(LoweringError(ex,
784786
# "Global method definition needs to be placed at the top level, or use `eval`"))
785-
if numchildren(ex) == 1
787+
res = if numchildren(ex) == 1
786788
if in_tail_pos
787789
emit_return(ctx, ex)
788790
elseif needs_value
@@ -807,6 +809,8 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
807809
@assert !needs_value && !in_tail_pos
808810
nothing
809811
end
812+
emit_latestworld(ctx, ex)
813+
res
810814
elseif k == K"opaque_closure_method"
811815
@ast ctx ex [K"opaque_closure_method"
812816
ex[1]

test/assignments_ir.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ end
161161
7 (call core.apply_type %%%₆)
162162
8 (call core.UnionAll %%₇)
163163
9 (= slot₁/X %₈)
164-
10 latestworld
165-
11 (return %₈)
164+
10 (return %₈)
166165

167166
########################################
168167
# Error: Invalid lhs in `=`

0 commit comments

Comments
 (0)