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

Add @inline for anonymous functions #34953

Merged
merged 8 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Language changes

* The syntax `(;)` (which was deprecated in v1.4) now creates an empty named tuple ([#30115]).

* `@inline` macro can now be applied to anonymous functions ([#34953]).
ssikdar1 marked this conversation as resolved.
Show resolved Hide resolved

Multi-threading changes
-----------------------

Expand Down
2 changes: 1 addition & 1 deletion base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function is_short_function_def(ex)
end

function findmeta(ex::Expr)
if ex.head === :function || is_short_function_def(ex)
if ex.head === :function || is_short_function_def(ex) || ex.head === :->
body::Expr = ex.args[2]
body.head === :block || error(body, " is not a block expression")
return findmeta_block(ex.args)
Expand Down
6 changes: 6 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function test_outer(a)
end
test_inlined_symbols(test_outer, Tuple{Int64})

# Test case 1 for anonymous functions (issue #34939)
function test_outer2(x)
@inline x -> x^2 + 2x - 1
end
test_inlined_symbols(test_outer2, Tuple{Int64})
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't test that the inline declaration has been applied to the inner function; it tests that the inlining pass produced valid output for test_outer2.

Copy link
Contributor Author

@ssikdar1 ssikdar1 Mar 2, 2020

Choose a reason for hiding this comment

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

@JeffBezanson
makes sense! How do I test if the inline declaration has been applied to the function?
Do I have to look at the Expr produced when using the inline macro?

julia> Base.parse_input_line("x -> x^2 + 2x - 1")
:($(Expr(:toplevel, :(#= none:1 =#), :((x->begin
          #= none:1 =#
          (x ^ 2 + 2x) - 1
      end)))))

julia> Base.parse_input_line("@inline x -> x^2 + 2x - 1")
:($(Expr(:toplevel, :(#= none:1 =#), :(#= none:1 =# @inline((x->begin
              #= none:1 =#
              (x ^ 2 + 2x) - 1
          end))))))

Something like this?

Copy link
Member

Choose a reason for hiding this comment

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

This is an easy way to test whether the inlining flag is set:

julia> ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(x->x)).source)
false

Copy link
Contributor Author

@ssikdar1 ssikdar1 Mar 2, 2020

Choose a reason for hiding this comment

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

@JeffBezanson
thanks! I added some tests to check :jl_ast_flag_inlineable for both anonymous and normal functions in b8ddf2a

Copy link
Member

Choose a reason for hiding this comment

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

This test can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed in 57ecb07


# Test case 2:
# Make sure that an error is thrown for the undeclared
# y in the else branch.
Expand Down