Skip to content

Commit 8ab1ece

Browse files
committed
Forward Vararg to tfuncs taking a variable number of arguments
1 parent 717a6d0 commit 8ab1ece

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

base/compiler/tfuncs.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,9 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
10931093
else
10941094
return Type
10951095
end
1096+
if !isempty(args) && isvarargtype(args[end])
1097+
return Type
1098+
end
10961099
largs = length(args)
10971100
if headtype === Union
10981101
largs == 0 && return Const(Bottom)
@@ -1417,11 +1420,6 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
14171420
end
14181421
end
14191422
return Any
1420-
elseif f === Core._expr
1421-
if length(argtypes) < 1 && !isva
1422-
return Bottom
1423-
end
1424-
return Expr
14251423
elseif f === invoke
14261424
if length(argtypes) > 1 && sv !== nothing && (isa(argtypes[1], Const) || isa(argtypes[1], Type))
14271425
if isa(argtypes[1], Const)
@@ -1471,15 +1469,17 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
14711469
# definitely too many arguments
14721470
return Bottom
14731471
end
1474-
if tf[1] == tf[2] || length(argtypes) - 1 == tf[2]
1475-
# expand Vararg to required number of arguments
1476-
vatype = unwrapva(argtypes[end])
1472+
if length(argtypes) - 1 == tf[2]
14771473
argtypes = argtypes[1:end-1]
1478-
while length(argtypes) < tf[2]
1479-
push!(argtypes, vatype)
1480-
end
14811474
else
1482-
return Any
1475+
vatype = argtypes[end]
1476+
argtypes = argtypes[1:end-1]
1477+
while length(argtypes) < tf[1]
1478+
push!(argtypes, unwrapva(vatype))
1479+
end
1480+
if length(argtypes) < tf[2]
1481+
push!(argtypes, unconstrain_vararg_length(vatype))
1482+
end
14831483
end
14841484
elseif !(tf[1] <= length(argtypes) <= tf[2])
14851485
# wrong # of args

test/compiler/inference.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,3 +2789,27 @@ end
27892789
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Vararg{Integer}}) == Integer
27902790
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Integer, Vararg}) == Integer
27912791
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Integer, Any, Vararg}) == Union{}
2792+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core._expr), Vararg}) == Expr
2793+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core._expr), Any, Vararg}) == Expr
2794+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core._expr), Any, Any, Vararg}) == Expr
2795+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(applicable), Vararg}) == Bool
2796+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(applicable), Any, Vararg}) == Bool
2797+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(applicable), Any, Any, Vararg}) == Bool
2798+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(applicable), Any, Any, Any, Vararg}) == Bool
2799+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(getfield), Tuple{Int}, Vararg}) == Int
2800+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(getfield), Tuple{Int}, Any, Vararg}) == Int
2801+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(getfield), Tuple{Int}, Any, Any, Vararg}) == Int
2802+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(getfield), Any, Any, Any, Any, Vararg}) == Union{}
2803+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(fieldtype), Vararg}) == Type
2804+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(fieldtype), Any, Vararg}) == Type
2805+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(fieldtype), Any, Any, Vararg}) == Type
2806+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(fieldtype), Any, Any, Any, Vararg}) == Type
2807+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(fieldtype), Any, Any, Any, Any, Vararg}) == Union{}
2808+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core.apply_type), Vararg}) == Type
2809+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core.apply_type), Any, Vararg}) == Type
2810+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(Core.apply_type), Any, Any, Vararg}) == Type
2811+
f_apply_cglobal(args...) = cglobal(args...)
2812+
@test Core.Compiler.return_type(f_apply_cglobal, Tuple{Vararg{Type{Int}}}) == Ptr
2813+
@test Core.Compiler.return_type(f_apply_cglobal, Tuple{Any, Vararg{Type{Int}}}) == Ptr
2814+
@test Core.Compiler.return_type(f_apply_cglobal, Tuple{Any, Type{Int}, Vararg{Type{Int}}}) == Ptr{Int}
2815+
@test Core.Compiler.return_type(f_apply_cglobal, Tuple{Any, Type{Int}, Type{Int}, Vararg{Type{Int}}}) == Union{}

0 commit comments

Comments
 (0)