Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #25678: return matters for generated functions #40778

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
Copy link
Contributor

@devmotion devmotion Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simeonschaub I guess generally it's a bad idea to name a module Core but this caused an error in Turing: TuringLang/Turing.jl#1756 (comment) and discussion in the issue. I think this could be fixed by interpolating Core.CodeInfo. I could prepare a PR (my first to base actually 😮) if you consider this to be a bug that should be fixed in base (I'll submit a PR to Turing in any case that renames the offending module).

Copy link
Member Author

@simeonschaub simeonschaub Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's funny, I was just staring at code generated by this today and was wondering whether having a call to getproperty in here was the right thing! It's generally more idiomatic to handle this as $(GlobalRef(Core, :CodeInfo)) instead of just interpolating the object though since that's what lowering would generate, but that sounds like the right thing to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ref: #43823

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