@@ -63,6 +63,92 @@ let exename = `$(Base.julia_cmd()) --compiled-modules=yes --startup-file=no --co
6363 @test ! endswith (s_dir, Base. Filesystem. path_separator)
6464end
6565
66+ @testset " Tests for @__FUNCTION__" begin
67+ let
68+ @testset " Basic usage" begin
69+ test_function_basic () = @__FUNCTION__
70+ @test test_function_basic () === test_function_basic
71+ end
72+
73+ @testset " Factorial function" begin
74+ factorial_function (n) = n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 )
75+ @test factorial_function (5 ) == 120
76+ end
77+
78+ @testset " Prevents boxed closures" begin
79+ function make_closure ()
80+ fib (n) = n <= 1 ? 1 : (@__FUNCTION__ )(n - 1 ) + (@__FUNCTION__ )(n - 2 )
81+ return fib
82+ end
83+ Test. @inferred make_closure ()
84+ closure = make_closure ()
85+ @test closure (5 ) == 8
86+ Test. @inferred closure (5 )
87+ end
88+
89+ @testset " Complex closure of closures" begin
90+ function f1 ()
91+ function f2 ()
92+ function f3 ()
93+ return @__FUNCTION__
94+ end
95+ return (@__FUNCTION__ ), f3 ()
96+ end
97+ return (@__FUNCTION__ ), f2 ()...
98+ end
99+ Test. @inferred f1 ()
100+ @test f1 ()[1 ] === f1
101+ @test f1 ()[2 ] != = f1
102+ @test f1 ()[3 ] != = f1
103+ @test f1 ()[3 ]() === f1 ()[3 ]
104+ @test f1 ()[2 ]()[2 ]() === f1 ()[3 ]
105+ end
106+
107+ @testset " Anonymous function" begin
108+ @test (n -> n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 ))(5 ) == 120
109+ end
110+
111+ @testset " Do block" begin
112+ function test_do_block ()
113+ result = map ([1 , 2 , 3 ]) do x
114+ return (@__FUNCTION__ , x)
115+ end
116+ # All should refer to the same do-block function
117+ @test all (r -> r[1 ] === result[1 ][1 ], result)
118+ # Values should be different
119+ @test [r[2 ] for r in result] == [1 , 2 , 3 ]
120+ # It should be different than `test_do_block`
121+ @test result[1 ][1 ] != = test_do_block
122+ end
123+ test_do_block ()
124+ end
125+
126+ @testset " Compatibility with kwargs" begin
127+ foo (; n) = n <= 1 ? 1 : n * (@__FUNCTION__ )(; n = n - 1 )
128+ @test foo (n = 5 ) == 120
129+ end
130+
131+ @testset " Error upon misuse" begin
132+ @gensym A
133+ @test_throws (
134+ " @__FUNCTION__ can only be used within a function" ,
135+ @eval (module $ A; @__FUNCTION__ ; end )
136+ )
137+ end
138+
139+ @testset " Callable structs" begin
140+ @gensym A
141+ @eval module $ A
142+ struct CallableStruct{T}; val:: T ; end
143+ (c:: CallableStruct )() = @__FUNCTION__
144+ end
145+ @eval using .$ A: CallableStruct
146+ c = CallableStruct (5 )
147+ @test c () === c
148+ end
149+ end
150+ end
151+
66152@test Base. in_sysimage (Base. PkgId (Base. UUID (" 8f399da3-3557-5675-b5ff-fb832c97cbdb" ), " Libdl" ))
67153@test Base. in_sysimage (Base. PkgId (Base. UUID (" 3a7fdc7e-7467-41b4-9f64-ea033d046d5b" ), " NotAPackage" )) == false
68154
0 commit comments