Skip to content

Commit

Permalink
fix JuliaLang#25678: return matters for generated functions (JuliaLan…
Browse files Browse the repository at this point in the history
…g#40778)

Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
  • Loading branch information
2 people authored and LilithHafner committed Feb 22, 2022
1 parent f7cd801 commit 03eb434
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ macro generated(f)
Expr(:block,
lno,
Expr(:if, Expr(:generated),
body,
# https://github.com/JuliaLang/julia/issues/25678
Expr(:block,
:(local tmp = $body),
:(if tmp isa Core.CodeInfo; return tmp; else tmp; end)),
Expr(:block,
Expr(:meta, :generated_only),
Expr(:return, nothing))))))
Expand Down
7 changes: 7 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2959,3 +2959,10 @@ end
ex.args = fill!(Vector{Any}(undef, 600000), 1)
@test_throws ErrorException("syntax: expression too large") eval(ex)
end

# issue 25678
@generated f25678(x::T) where {T} = code_lowered(sin, Tuple{x})[]
@test f25678(pi/6) === sin(pi/6)

@generated g25678(x) = return :x
@test g25678(7) === 7

0 comments on commit 03eb434

Please sign in to comment.