Skip to content

Commit 3c39234

Browse files
committed
Fix desugaring of return without argument
flisp lowering deals with this in the parser by inserting the `nothing` there. JuliaSyntax avoids this in order to more faithfully represent the source, so we need to deal with it in lowering instead.
1 parent cf2b22f commit 3c39234

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/desugaring.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,14 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
42364236
throw(LoweringError(ex, "`...` expression outside call"))
42374237
elseif is_leaf(ex)
42384238
ex
4239+
elseif k == K"return"
4240+
if numchildren(ex) == 0
4241+
@ast ctx ex [K"return" "nothing"::K"core"]
4242+
elseif numchildren(ex) == 1
4243+
mapchildren(e->expand_forms_2(ctx,e), ctx, ex)
4244+
else
4245+
throw(LoweringError(ex, "More than one argument to return"))
4246+
end
42394247
else
42404248
mapchildren(e->expand_forms_2(ctx,e), ctx, ex)
42414249
end

test/functions_ir.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,28 @@ end
896896
9 TestMod.f
897897
10 (return %₉)
898898

899+
########################################
900+
# Function return without arguments
901+
function f()
902+
return
903+
after_return # <- distinguish output from implicit return
904+
end
905+
#---------------------
906+
1 (method TestMod.f)
907+
2 TestMod.f
908+
3 (call core.Typeof %₂)
909+
4 (call core.svec %₃)
910+
5 (call core.svec)
911+
6 SourceLocation::1:10
912+
7 (call core.svec %%%₆)
913+
8 --- method core.nothing %
914+
slots: [slot₁/#self#(!read)]
915+
1 (return core.nothing)
916+
2 TestMod.after_return
917+
3 (return %₂)
918+
9 TestMod.f
919+
10 (return %₉)
920+
899921
########################################
900922
# Binding docs to functions
901923
"""

0 commit comments

Comments
 (0)