Skip to content

Commit 9514ab0

Browse files
authored
Fix IR generation for return in value position (JuliaLang/JuliaLowering.jl#47)
`return` in value position is allowed in Julia so that ASTs compose more easily.
1 parent 66dde2f commit 9514ab0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

JuliaLowering/src/linear_ir.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function emit_assignment(ctx, srcref, lhs, rhs, op=K"=")
342342
else
343343
# in unreachable code (such as after return); still emit the assignment
344344
# so that the structure of those uses is preserved
345-
emit_simple_assignment(ctx, srcref, lhs, @ast ctx srcref "nothing"::K"core", op)
345+
emit_simple_assignment(ctx, srcref, lhs, nothing_(ctx, srcref), op)
346346
nothing
347347
end
348348
end

JuliaLowering/test/functions.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ begin
159159
end
160160
""")
161161

162+
@test JuliaLowering.include_string(test_mod, """
163+
x = 0
164+
function f_return_in_value_pos()
165+
global x
166+
x = return 42
167+
end
168+
169+
(f_return_in_value_pos(), x)
170+
""") === (42, 0)
171+
162172
@testset "Default positional arguments" begin
163173
@test JuliaLowering.include_string(test_mod, """
164174
begin

JuliaLowering/test/functions_ir.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,28 @@ end
995995
11 TestMod.f
996996
12 (return %₁₁)
997997

998+
########################################
999+
# Function return in value position is allowed
1000+
function f()
1001+
x = return 1
1002+
end
1003+
#---------------------
1004+
1 (method TestMod.f)
1005+
2 latestworld
1006+
3 TestMod.f
1007+
4 (call core.Typeof %₃)
1008+
5 (call core.svec %₄)
1009+
6 (call core.svec)
1010+
7 SourceLocation::1:10
1011+
8 (call core.svec %%%₇)
1012+
9 --- method core.nothing %
1013+
slots: [slot₁/#self#(!read) slot₂/x(!read)]
1014+
1 (return 1)
1015+
2 (= slot₂/x core.nothing)
1016+
10 latestworld
1017+
11 TestMod.f
1018+
12 (return %₁₁)
1019+
9981020
########################################
9991021
# Binding docs to functions
10001022
"""

0 commit comments

Comments
 (0)