Skip to content

Commit

Permalink
Improve inference of tail-like functions (#29264)
Browse files Browse the repository at this point in the history
Previously these sorts of function would block constant propagation.
Hopfully #28955 will just fix this, but until then, add a surgical
fix and a test.
  • Loading branch information
Keno authored and jrevels committed Dec 31, 2018
1 parent fb28ab4 commit 5976159
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,21 @@ function abstract_call_method_with_const_args(@nospecialize(f), argtypes::Vector
atypes = get_argtypes(inf_result)
if method.isva
vargs = argtypes[(nargs + 1):end]
all_vargs_const = true
for i in 1:length(vargs)
a = maybe_widen_conditional(vargs[i])
all_vargs_const &= a isa Const
if i > length(inf_result.vargs)
push!(inf_result.vargs, a)
elseif a isa Const
inf_result.vargs[i] = a
end
end
# If all vargs are const, the result may be a constant
# tuple. If so, we should make sure to treat it as such
if all_vargs_const
atypes[nargs + 1] = builtin_tfunction(tuple, inf_result.vargs, sv)
end
end
for i in 1:nargs
a = maybe_widen_conditional(argtypes[i])
Expand Down
11 changes: 11 additions & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2089,3 +2089,14 @@ f30394(foo::T1, ::Type{T2}) where {T2, T1 <: T2} = foo
f30394(foo, T2) = f30394(foo.foo_inner, T2)

@test Base.return_types(f30394, (Foo30394_2, Type{Base30394})) == Any[Base30394]

# test that this doesn't cause an internal error
get_order_kwargs()
end

# Test that tail-like functions don't block constant propagation
my_tail_const_prop(i, tail...) = tail
function foo_tail_const_prop()
Val{my_tail_const_prop(1,2,3,4)}()
end
@test (@inferred foo_tail_const_prop()) == Val{(2,3,4)}()

0 comments on commit 5976159

Please sign in to comment.