Skip to content

Commit 717a6d0

Browse files
committed
Expand Varargs for tfuncs taking a fixed number of arguments
1 parent 8c07013 commit 717a6d0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

base/compiler/tfuncs.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,9 +1443,6 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
14431443
end
14441444
return Any
14451445
end
1446-
if isva
1447-
return Any
1448-
end
14491446
if isa(f, IntrinsicFunction)
14501447
if is_pure_intrinsic_infer(f) && _all(@nospecialize(a) -> isa(a, Const), argtypes)
14511448
argvals = anymap(a::Const -> a.val, argtypes)
@@ -1469,7 +1466,22 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
14691466
tf = T_FFUNC_VAL[fidx]
14701467
end
14711468
tf = tf::Tuple{Int, Int, Any}
1472-
if !(tf[1] <= length(argtypes) <= tf[2])
1469+
if isva
1470+
if length(argtypes) - 1 > tf[2]
1471+
# definitely too many arguments
1472+
return Bottom
1473+
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])
1477+
argtypes = argtypes[1:end-1]
1478+
while length(argtypes) < tf[2]
1479+
push!(argtypes, vatype)
1480+
end
1481+
else
1482+
return Any
1483+
end
1484+
elseif !(tf[1] <= length(argtypes) <= tf[2])
14731485
# wrong # of args
14741486
return Bottom
14751487
end

test/compiler/inference.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,3 +2776,16 @@ for badf in [getfield_const_typename_bad1, getfield_const_typename_bad2]
27762776
@test code[end] === Core.ReturnNode()
27772777
@test_throws TypeError badf()
27782778
end
2779+
2780+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(sizeof), Vararg{DataType}}) == Int
2781+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(sizeof), DataType, Vararg}) == Int
2782+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(sizeof), DataType, Any, Vararg}) == Union{}
2783+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(===), Vararg}) == Bool
2784+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(===), Any, Vararg}) == Bool
2785+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(===), Any, Any, Vararg}) == Bool
2786+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(===), Any, Any, Any, Vararg}) == Union{}
2787+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Vararg{Symbol}}) == Symbol
2788+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Vararg{Symbol}}) == Symbol
2789+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Vararg{Integer}}) == Integer
2790+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Integer, Vararg}) == Integer
2791+
@test Core.Compiler.return_type(apply26826, Tuple{typeof(setfield!), Any, Symbol, Integer, Any, Vararg}) == Union{}

0 commit comments

Comments
 (0)