Skip to content

Commit d467355

Browse files
committed
Const lowering fixes and some cleanup
Fix `let; const x = 1; end`, which should throw. Moves the check up to scope analysis (lisp has it in `compile`).
1 parent abe66bb commit d467355

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/desugaring.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,6 @@ end
11911191
# * Destructuring
11921192
# * Typed variable declarations
11931193
function expand_assignment(ctx, ex, is_const=false)
1194-
function maybe_wrap_const(ex)
1195-
@ast ctx ex (is_const ? [K"const" ex] : ex)
1196-
end
11971194
@chk numchildren(ex) == 2
11981195
lhs = ex[1]
11991196
rhs = ex[2]
@@ -1229,7 +1226,6 @@ function expand_assignment(ctx, ex, is_const=false)
12291226
]
12301227
)
12311228
elseif is_identifier_like(lhs)
1232-
sink_assignment(ctx, ex, lhs, expand_forms_2(ctx, rhs))
12331229
if is_const
12341230
rr = ssavar(ctx, rhs)
12351231
@ast ctx ex [
@@ -2138,9 +2134,8 @@ function strip_decls!(ctx, stmts, declkind, declkind2, declmeta, ex)
21382134
end
21392135
end
21402136

2141-
# local x, (y=2), z ==> local x; local y; y = 2; local z
2142-
# const x = 1 ==> const x; x = 1
2143-
# global x::T = 1 ==> (block (global x) (decl x T) (x = 1))
2137+
# local x, (y=2), z ==> local x; local z; y = 2
2138+
# Note there are differences from lisp (evaluation order?)
21442139
function expand_decls(ctx, ex)
21452140
declkind = kind(ex)
21462141
declmeta = get(ex, :meta, nothing)
@@ -2189,7 +2184,7 @@ end
21892184

21902185
function expand_const_decl(ctx, ex)
21912186
function check_assignment(asgn)
2192-
@chk (kind(asgn) == K"=") (ex, "expected assignment after \"const\"")
2187+
@chk (kind(asgn) == K"=") (ex, "expected assignment after `const`")
21932188
end
21942189

21952190
k = kind(ex[1])
@@ -2213,8 +2208,10 @@ function expand_const_decl(ctx, ex)
22132208
elseif k == K"="
22142209
check_assignment(ex[1])
22152210
expand_assignment(ctx, ex[1], true)
2211+
elseif k == K"local"
2212+
throw(LoweringError(ex, "unsupported `const local` declaration"))
22162213
else
2217-
throw(LoweringError(ex, "expected assignment after \"const\""))
2214+
throw(LoweringError(ex, "expected assignment after `const`"))
22182215
end
22192216
end
22202217

@@ -3945,8 +3942,9 @@ function expand_struct_def(ctx, ex, docs)
39453942
# See https://github.com/JuliaLang/julia/pull/36121
39463943
@ast ctx ex [K"block"
39473944
[K"assert" "toplevel_only"::K"Symbol" [K"inert" ex] ]
3948-
[K"global" global_struct_name]
39493945
[K"scope_block"(scope_type=:hard)
3946+
# Needed for later constdecl to work, though plain global form may be removed soon.
3947+
[K"global" global_struct_name]
39503948
[K"block"
39513949
[K"local" struct_name]
39523950
[K"always_defined" struct_name]

src/linear_ir.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,6 @@ function compile(ctx::LinearIRContext, ex, needs_value, in_tail_pos)
649649
nothing
650650
end
651651
elseif k == K"=" || k == K"constdecl"
652-
if k == K"constdecl"
653-
check_no_local_bindings(ctx, ex[1], "unsupported `const` declaration on local variable")
654-
# other errors (probably this one too) should be handled in previous passes
655-
end
656652
lhs = ex[1]
657653
if kind(lhs) == K"Placeholder"
658654
compile(ctx, ex[2], needs_value, in_tail_pos)

src/scope_analysis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function _find_scope_vars!(ctx, assignments, locals, destructured_args, globals,
4848
# like v = val, except that if `v` turns out global(either implicitly or
4949
# by explicit `global`), it gains an implicit `const`
5050
_insert_if_not_present!(assignments, NameKey(ex[1]), ex)
51-
elseif k == K"="
51+
elseif k == K"=" || k == K"constdecl"
5252
v = decl_var(ex[1])
5353
if !(kind(v) in KSet"BindingId globalref Placeholder")
5454
_insert_if_not_present!(assignments, NameKey(v), v)

0 commit comments

Comments
 (0)