From 12cce25a75f79e41e39f7c0bde54425d7e91afb7 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 10 May 2021 22:26:55 +0200 Subject: [PATCH] fix #25678: return matters for generated functions Just explicitely check for `CodeInfo` objects and use an explicit `return` in this case inside the `@generated` macro. Co-authored-by: Jeff Bezanson --- base/expr.jl | 5 ++++- test/syntax.jl | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/expr.jl b/base/expr.jl index 4d6401b002a76..7c303ebfeb001 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -434,7 +434,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)))))) diff --git a/test/syntax.jl b/test/syntax.jl index d934e9358baac..779ac0665f54e 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -2787,3 +2787,10 @@ macro m_nospecialize_unnamed_hygiene() end @test @m_nospecialize_unnamed_hygiene()(1) === Any + +# 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