Skip to content

Commit 92c84bf

Browse files
fix #25678: return matters for generated functions (#40778)
Just explicitely check for `CodeInfo` objects and use an explicit `return` in this case inside the `@generated` macro. Co-authored-by: Jeff Bezanson <[email protected]>
1 parent 3d058b2 commit 92c84bf

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

base/expr.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,10 @@ macro generated(f)
464464
Expr(:block,
465465
lno,
466466
Expr(:if, Expr(:generated),
467-
body,
467+
# https://github.com/JuliaLang/julia/issues/25678
468+
Expr(:block,
469+
:(local tmp = $body),
470+
:(if tmp isa Core.CodeInfo; return tmp; else tmp; end)),
468471
Expr(:block,
469472
Expr(:meta, :generated_only),
470473
Expr(:return, nothing))))))

test/syntax.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,3 +2959,10 @@ end
29592959
ex.args = fill!(Vector{Any}(undef, 600000), 1)
29602960
@test_throws ErrorException("syntax: expression too large") eval(ex)
29612961
end
2962+
2963+
# issue 25678
2964+
@generated f25678(x::T) where {T} = code_lowered(sin, Tuple{x})[]
2965+
@test f25678(pi/6) === sin(pi/6)
2966+
2967+
@generated g25678(x) = return :x
2968+
@test g25678(7) === 7

0 commit comments

Comments
 (0)