From 666e8fd2fe6bb7448d6a32730bbe5be2aab81f7b Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Sun, 1 Mar 2020 15:51:51 -0500 Subject: [PATCH 1/6] Add @inline for anonymous functions --- base/expr.jl | 2 +- test/compiler/inline.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/base/expr.jl b/base/expr.jl index 0e7781f64c643..4e7dd798cb21d 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -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) diff --git a/test/compiler/inline.jl b/test/compiler/inline.jl index 5dafed1e422f6..8d59a269ddacd 100644 --- a/test/compiler/inline.jl +++ b/test/compiler/inline.jl @@ -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}) + # Test case 2: # Make sure that an error is thrown for the undeclared # y in the else branch. From be1557ce94ca11b93d297adeedaab0fb92e016c6 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Mon, 2 Mar 2020 09:18:50 -0500 Subject: [PATCH 2/6] add NEWS entry --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index da641a04917ee..35bb69ba6fd85 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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]). + Multi-threading changes ----------------------- From c70bec2c517abd13fecbcd58b0efc572be484cc9 Mon Sep 17 00:00:00 2001 From: ssikdar1 Date: Mon, 2 Mar 2020 13:04:45 -0500 Subject: [PATCH 3/6] Update NEWS.md Co-Authored-By: Rafael Fourquet --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 35bb69ba6fd85..36fa2a7da06b5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,7 +36,7 @@ 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]). +* `@inline` macro can now be applied to short-form anonymous functions ([#34953]). Multi-threading changes ----------------------- From 6f9ef7bb55b6dad95eb3b2be5b8df75ef470541c Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Mon, 2 Mar 2020 18:49:58 -0500 Subject: [PATCH 4/6] add inline jl_ast_flag_inlineable tests --- test/compiler/inline.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/compiler/inline.jl b/test/compiler/inline.jl index 8d59a269ddacd..119b66e8592e2 100644 --- a/test/compiler/inline.jl +++ b/test/compiler/inline.jl @@ -57,6 +57,19 @@ function test_outer2(x) end test_inlined_symbols(test_outer2, Tuple{Int64}) + +@testset "check jl_ast_flag_inlineable for inline macro" begin + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source) + @test ret == true + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source) + @test ret == false + + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source) + @test ret == true + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source) + @test ret == false +end + # Test case 2: # Make sure that an error is thrown for the undeclared # y in the else branch. From b8ddf2a811302a64758836d2f8f3531e1ceac056 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Mon, 2 Mar 2020 18:52:02 -0500 Subject: [PATCH 5/6] move inline jl_ast_flag_inlineable to bottom of testfile --- test/compiler/inline.jl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/compiler/inline.jl b/test/compiler/inline.jl index 119b66e8592e2..4c9dc82d98dfe 100644 --- a/test/compiler/inline.jl +++ b/test/compiler/inline.jl @@ -58,17 +58,6 @@ end test_inlined_symbols(test_outer2, Tuple{Int64}) -@testset "check jl_ast_flag_inlineable for inline macro" begin - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source) - @test ret == true - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source) - @test ret == false - - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source) - @test ret == true - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source) - @test ret == false -end # Test case 2: # Make sure that an error is thrown for the undeclared @@ -294,3 +283,15 @@ let ci = code_typed(f34900, Tuple{Int, Int})[1].first @test length(ci.code) == 1 && isexpr(ci.code[1], :return) && ci.code[1].args[1].id == 2 end + +@testset "check jl_ast_flag_inlineable for inline macro" begin + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source) + @test ret == true + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source) + @test ret == false + + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source) + @test ret == true + ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source) + @test ret == false +end From 57ecb07621e1a5b36401cd9aefdeca6688b2d79b Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Wed, 4 Mar 2020 16:11:31 -0500 Subject: [PATCH 6/6] fix inline tests --- test/compiler/inline.jl | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/test/compiler/inline.jl b/test/compiler/inline.jl index 4c9dc82d98dfe..457f3aef07910 100644 --- a/test/compiler/inline.jl +++ b/test/compiler/inline.jl @@ -51,14 +51,6 @@ 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}) - - - # Test case 2: # Make sure that an error is thrown for the undeclared # y in the else branch. @@ -285,13 +277,8 @@ let ci = code_typed(f34900, Tuple{Int, Int})[1].first end @testset "check jl_ast_flag_inlineable for inline macro" begin - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source) - @test ret == true - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source) - @test ret == false - - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source) - @test ret == true - ret = ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source) - @test ret == false + @test ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline x -> x)).source) + @test !ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods( x -> x)).source) + @test ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(@inline function f(x) x end)).source) + @test !ccall(:jl_ast_flag_inlineable, Bool, (Any,), first(methods(function f(x) x end)).source) end