@@ -1011,46 +1011,58 @@ function test_const_return(@nospecialize(f), @nospecialize(t), @nospecialize(val
10111011 end
10121012end
10131013
1014- test_const_return (()-> 1 , Tuple{}, 1 )
1015- test_const_return (()-> sizeof (Int), Tuple{}, sizeof (Int))
1016- test_const_return (()-> sizeof (1 ), Tuple{}, sizeof (Int))
1017- test_const_return (()-> sizeof (DataType), Tuple{}, sizeof (DataType))
1018- test_const_return (()-> sizeof (1 < 2 ), Tuple{}, 1 )
1019- @eval test_const_return (()-> Core. sizeof ($ (Array {Int} ())), Tuple{}, sizeof (Int))
1020- @eval test_const_return (()-> Core. sizeof ($ (Matrix {Float32} (2 , 2 ))), Tuple{}, 4 * 2 * 2 )
1021-
1022- function find_core_sizeof_call (code)
1014+ function find_call (code, func, narg)
10231015 for ex in code
10241016 isa (ex, Expr) || continue
10251017 ex = ex:: Expr
1026- if ex. head === :call && length (ex. args) == 2
1027- if ex. args[1 ] === Core. sizeof || ex. args[1 ] == GlobalRef (Core, :sizeof )
1018+ if ex. head === :call && length (ex. args) == narg
1019+ farg = ex. args[1 ]
1020+ if isa (farg, GlobalRef)
1021+ farg = farg:: GlobalRef
1022+ if isdefined (farg. mod, farg. name) && isconst (farg. mod, farg. name)
1023+ farg = getfield (farg. mod, farg. name)
1024+ end
1025+ end
1026+ if farg === func
10281027 return true
10291028 end
10301029 elseif Core. Inference. is_meta_expr (ex)
10311030 continue
10321031 end
1033- find_core_sizeof_call (ex. args) && return true
1032+ find_call (ex. args, func, narg ) && return true
10341033 end
10351034 return false
10361035end
10371036
1037+ test_const_return (()-> 1 , Tuple{}, 1 )
1038+ test_const_return (()-> sizeof (Int), Tuple{}, sizeof (Int))
1039+ test_const_return (()-> sizeof (1 ), Tuple{}, sizeof (Int))
1040+ test_const_return (()-> sizeof (DataType), Tuple{}, sizeof (DataType))
1041+ test_const_return (()-> sizeof (1 < 2 ), Tuple{}, 1 )
1042+ @eval test_const_return (()-> Core. sizeof ($ (Array {Int} ())), Tuple{}, sizeof (Int))
1043+ @eval test_const_return (()-> Core. sizeof ($ (Matrix {Float32} (2 , 2 ))), Tuple{}, 4 * 2 * 2 )
1044+
10381045# Make sure Core.sizeof with a ::DataType as inferred input type is inferred but not constant.
10391046function sizeof_typeref (typeref)
10401047 Core. sizeof (typeref[])
10411048end
10421049@test @inferred (sizeof_typeref (Ref {DataType} (Int))) == sizeof (Int)
1043- @test find_core_sizeof_call (first (@code_typed sizeof_typeref (Ref {DataType} ())). code)
1050+ @test find_call (first (@code_typed sizeof_typeref (Ref {DataType} ())). code, Core . sizeof, 2 )
10441051# Constant `Vector` can be resized and shouldn't be optimized to a constant.
10451052const constvec = [1 , 2 , 3 ]
10461053@eval function sizeof_constvec ()
10471054 Core. sizeof ($ constvec)
10481055end
10491056@test @inferred (sizeof_constvec ()) == sizeof (Int) * 3
1050- @test find_core_sizeof_call (first (@code_typed sizeof_constvec ()). code)
1057+ @test find_call (first (@code_typed sizeof_constvec ()). code, Core . sizeof, 2 )
10511058push! (constvec, 10 )
10521059@test @inferred (sizeof_constvec ()) == sizeof (Int) * 4
10531060
1061+ test_const_return ((x)-> isdefined (x, :re ), Tuple{Complex128}, true )
1062+ isdefined_f3 (x) = isdefined (x, 3 )
1063+ @test @inferred (isdefined_f3 (())) == false
1064+ @test find_call (first (code_typed (isdefined_f3, Tuple{Tuple{Vararg{Int}}})[1 ]). code, isdefined, 3 )
1065+
10541066let isa_tfunc = Core. Inference. t_ffunc_val[
10551067 findfirst (Core. Inference. t_ffunc_key, isa)][3 ]
10561068 @test isa_tfunc (Array, Const (AbstractArray)) === Const (true )
0 commit comments