@@ -4360,7 +4360,7 @@ end
43604360 # @__FUNCTION__ in regular functions
43614361 test_function_basic () = @__FUNCTION__
43624362 @test test_function_basic () === test_function_basic
4363-
4363+
43644364 # Expr(:thisfunction) in regular functions
43654365 @eval regular_func () = $ (Expr (:thisfunction ))
43664366 @test regular_func () === regular_func
@@ -4370,12 +4370,12 @@ end
43704370 # Factorial with @__FUNCTION__
43714371 factorial_function (n) = n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 )
43724372 @test factorial_function (5 ) == 120
4373-
4373+
43744374 # Fibonacci with Expr(:thisfunction)
43754375 struct RecursiveCallableStruct; end
43764376 @eval (:: RecursiveCallableStruct )(n) = n <= 1 ? n : $ (Expr (:thisfunction ))(n- 1 ) + $ (Expr (:thisfunction ))(n- 2 )
43774377 @test RecursiveCallableStruct ()(10 ) === 55
4378-
4378+
43794379 # Anonymous function recursion
43804380 @test (n -> n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 ))(5 ) == 120
43814381 end
@@ -4407,21 +4407,6 @@ end
44074407 @test f1 ()[3 ] != = f1
44084408 @test f1 ()[3 ]() === f1 ()[3 ]
44094409 @test f1 ()[2 ]()[2 ]() === f1 ()[3 ]
4410-
4411- # In closures, var"#self#" should refer to the enclosing function,
4412- # NOT the enclosing struct instance
4413- struct CallableStruct2; end
4414- @eval function (obj:: CallableStruct2 )()
4415- function inner_func ()
4416- $ (Expr (:thisfunction ))
4417- end
4418- inner_func
4419- end
4420-
4421- let cs = CallableStruct2 ()
4422- @test cs ()() === cs ()
4423- @test cs ()() != = cs
4424- end
44254410 end
44264411
44274412 @testset " Do blocks" begin
@@ -4443,7 +4428,7 @@ end
44434428 # @__FUNCTION__ with kwargs
44444429 foo (; n) = n <= 1 ? 1 : n * (@__FUNCTION__ )(; n = n - 1 )
44454430 @test foo (n = 5 ) == 120
4446-
4431+
44474432 # Expr(:thisfunction) with kwargs
44484433 let
44494434 @eval f2 (; n= 1 ) = n <= 1 ? n : n * $ (Expr (:thisfunction ))(; n= n- 1 )
@@ -4462,8 +4447,23 @@ end
44624447 @eval using .$ A: CallableStruct
44634448 c = CallableStruct (5 )
44644449 @test c () === c
4465-
4466- # Expr(:thisfunction) in callable structs
4450+
4451+ # In closures, var"#self#" should refer to the enclosing function,
4452+ # NOT the enclosing struct instance
4453+ struct CallableStruct2; end
4454+ @eval function (obj:: CallableStruct2 )()
4455+ function inner_func ()
4456+ $ (Expr (:thisfunction ))
4457+ end
4458+ inner_func
4459+ end
4460+
4461+ let cs = CallableStruct2 ()
4462+ @test cs ()() === cs ()
4463+ @test cs ()() != = cs
4464+ end
4465+
4466+ # Accessing values via self-reference
44674467 struct CallableStruct3
44684468 value:: Int
44694469 end
@@ -4474,15 +4474,27 @@ end
44744474 @test cs () === cs
44754475 @test cs (10 ) === 52
44764476 end
4477+
4478+ # Callable struct with args and kwargs
4479+ struct CallableStruct4
4480+ end
4481+ @eval function (obj:: CallableStruct4 )(x, args... ; y= 2 , kws... )
4482+ return (; func= (@__FUNCTION__ ), x, args, y, kws)
4483+ end
4484+ c = CallableStruct4 ()
4485+ @test_broken c (1 ). func === c
4486+ @test c (2 , 3 ). args == (3 ,)
4487+ @test c (2 ; y= 4 ). y == 4
4488+ @test c (2 ; y= 4 , a= 5 , b= 6 , c= 7 ). kws[:c ] == 7
44774489 end
44784490
44794491 @testset " Special cases" begin
44804492 # Generated functions
44814493 let @generated foo () = Expr (:thisfunction )
44824494 @test foo () === foo
44834495 end
4484-
4485- # Struct constructor with thisfunction
4496+
4497+ # Struct constructors
44864498 let
44874499 @eval struct Cols{T<: Tuple }
44884500 cols:: T
@@ -4492,9 +4504,15 @@ end
44924504 result = Cols (1 , 2 , 3 )
44934505 @test occursin (" Cols" , result)
44944506 end
4495-
4496- # Error upon misuse
4507+ end
4508+
4509+ @testset " Error upon misuse" begin
44974510 @gensym B
44984511 @test_throws ErrorException @eval (module $ B; @__FUNCTION__ ; end )
4512+
4513+ @test_throws (
4514+ " \" @__FUNCTION__\" not allowed inside comprehension or generator" ,
4515+ @eval ([(@__FUNCTION__ ) for _ in 1 : 10 ])
4516+ )
44994517 end
45004518end
0 commit comments