Skip to content

Commit 0d4901b

Browse files
committed
More const fixes, refresh IR tests, handwrite some tests
1 parent 171e55c commit 0d4901b

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This work is intended to
2828

2929
Note this is a work in progress; many types of syntax are not yet handled.
3030

31-
1. You need a 1.13.0-DEV build of Julia: At least 1.13.0-DEV.408. Commit `ff0a9313de` is currently known to work. Note that JuliaLowering relies on Julia internals and may be broken on the latest Julia dev version from time to time.
31+
1. You need a 1.13.0-DEV build of Julia: At least 1.13.0-DEV.408. Commit `3864b18af6` is currently known to work. Note that JuliaLowering relies on Julia internals and may be broken on the latest Julia dev version from time to time.
3232
2. Check out the main branch of [JuliaSyntax](https://github.com/JuliaLang/JuliaSyntax.jl)
3333
3. Get the latest version of [JuliaSyntaxFormatter](https://github.com/c42f/JuliaSyntaxFormatter.jl)
3434
4. Run the demo `include("test/demo.jl")`

src/desugaring.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,9 @@ function expand_assignment(ctx, ex, is_const=false)
12131213
tmp_rhs = ssavar(ctx, rhs, "rhs")
12141214
rr = tmp_rhs
12151215
end
1216-
for i in 1:length(stmts)
1216+
# In const a = b = c, only a is const
1217+
stmts[1] = @ast ctx ex [(is_const ? K"constdecl" : K"=") stmts[1] rr]
1218+
for i in 2:length(stmts)
12171219
stmts[i] = @ast ctx ex [K"=" stmts[i] rr]
12181220
end
12191221
if !isnothing(tmp_rhs)
@@ -1274,11 +1276,12 @@ function expand_assignment(ctx, ex, is_const=false)
12741276
x = lhs[1]
12751277
T = lhs[2]
12761278
res = if is_const
1277-
expand_forms_2(ctx, ex, @ast ctx ex [
1278-
K"constdecl"
1279-
lhs[1]
1280-
convert_for_type_decl(ctx, ex, rhs, T, true)
1281-
])
1279+
expand_forms_2(ctx, @ast ctx ex [
1280+
K"const"
1281+
[K"="
1282+
lhs[1]
1283+
convert_for_type_decl(ctx, ex, rhs, T, true)
1284+
]])
12821285
elseif is_identifier_like(x)
12831286
# Identifer in lhs[1] is a variable type declaration, eg
12841287
# x::T = rhs
@@ -2190,7 +2193,7 @@ function expand_const_decl(ctx, ex)
21902193
k = kind(ex[1])
21912194
if numchildren(ex) == 2
21922195
@ast ctx ex [
2193-
K"const"
2196+
K"constdecl"
21942197
ex[1]
21952198
expand_forms_2(ctx, ex[2])
21962199
]
@@ -2206,7 +2209,9 @@ function expand_const_decl(ctx, ex)
22062209
expand_assignment(ctx, ex[1], true)
22072210
]
22082211
elseif k == K"="
2209-
check_assignment(ex[1])
2212+
if numchildren(ex[1]) >= 1 && kind(ex[1][1]) == K"tuple"
2213+
throw(LoweringError(ex[1][1], "unsupported `const` tuple"))
2214+
end
22102215
expand_assignment(ctx, ex[1], true)
22112216
elseif k == K"local"
22122217
throw(LoweringError(ex, "unsupported `const local` declaration"))

test/decls.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ end
4949
@test Core.get_binding_type(test_mod, :a_typed_global_2) === Int
5050
@test test_mod.a_typed_global_2 === 10
5151

52+
# Const and tuple assignments
53+
@test JuliaLowering.include_string(test_mod, "(a0, a1, a2) = [1,2,3]") == [1,2,3]
54+
55+
@test JuliaLowering.include_string(test_mod, "const abc::Int = 9") === 9
56+
57+
# redeclaration of the same value used to be allowed
58+
@test_throws ErrorException JuliaLowering.include_string(test_mod, "abc = 9")
59+
@test_throws ErrorException JuliaLowering.include_string(test_mod, "abc = 10")
60+
# redeclaration with const should be OK
61+
@test JuliaLowering.include_string(test_mod, "const abc::Int = 0") === 0
62+
63+
# Unsupported for now
64+
@test_throws LoweringError JuliaLowering.include_string(test_mod, "const a,b,c = 1,2,3")
65+
5266
end

test/decls_ir.jl

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,59 @@ const xx = 10
3030
# Typed const
3131
const xx::T = 10
3232
#---------------------
33-
1 (globaldecl TestMod.xx TestMod.T)
34-
2 (latestworld)
35-
3 (globaldecl TestMod.xx)
33+
1 TestMod.T
34+
2 (= slot₁/tmp 10)
35+
3 slot₁/tmp
36+
4 (call core.isa %%₁)
37+
5 (gotoifnot %₄ label₇)
38+
6 (goto label₁₀)
39+
7 slot₁/tmp
40+
8 (call top.convert %%₇)
41+
9 (= slot₁/tmp (call core.typeassert %%₁))
42+
10 slot₁/tmp
43+
11 (constdecl TestMod.xx %₁₀)
44+
12 (latestworld)
45+
13 (return %₁₀)
46+
47+
########################################
48+
# Error: Const tuple
49+
const xxx,xxxx,xxxxx = 10,20,30
50+
#---------------------
51+
LoweringError:
52+
const xxx,xxxx,xxxxx = 10,20,30
53+
# └─────────────┘ ── unsupported `const` tuple
54+
55+
########################################
56+
# Const in chain: only first is const
57+
const c0 = v0 = v1 = 123
58+
#---------------------
59+
1 123
60+
2 (constdecl TestMod.c0 %₁)
61+
3 (globaldecl TestMod.v0)
3662
4 (latestworld)
37-
5 (call core.get_binding_type TestMod :xx)
38-
6 (= slot₁/tmp 10)
63+
5 (call core.get_binding_type TestMod :v0)
64+
6 (= slot₁/tmp %)
3965
7 slot₁/tmp
4066
8 (call core.isa %%₅)
4167
9 (gotoifnot %₈ label₁₁)
4268
10 (goto label₁₃)
4369
11 slot₁/tmp
4470
12 (= slot₁/tmp (call top.convert %%₁₁))
4571
13 slot₁/tmp
46-
14 (call top.setglobal! TestMod :xx %₁₃)
47-
15 (return 10)
72+
14 (call top.setglobal! TestMod :v0 %₁₃)
73+
15 (globaldecl TestMod.v1)
74+
16 (latestworld)
75+
17 (call core.get_binding_type TestMod :v1)
76+
18 (= slot₂/tmp %₁)
77+
19 slot₂/tmp
78+
20 (call core.isa %₁₉ %₁₇)
79+
21 (gotoifnot %₂₀ label₂₃)
80+
22 (goto label₂₅)
81+
23 slot₂/tmp
82+
24 (= slot₂/tmp (call top.convert %₁₇ %₂₃))
83+
25 slot₂/tmp
84+
26 (call top.setglobal! TestMod :v1 %₂₅)
85+
27 (return %₁)
4886

4987
########################################
5088
# Global assignment

test/misc_ir.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,12 @@ LoweringError:
390390
x...
391391
└──┘ ── `...` expression outside call
392392

393+
########################################
394+
# `include` should increment world age
395+
include("hi.jl")
396+
#---------------------
397+
1 TestMod.include
398+
2 (call %"hi.jl")
399+
3 (latestworld)
400+
4 (return %₂)
401+

0 commit comments

Comments
 (0)