Skip to content

Commit

Permalink
Propagate constant calls to new! (#28284)
Browse files Browse the repository at this point in the history
* this is the thing that infers new.

* more stuff!

* Incorporating Jeff's suggestions.

* Added tests and used Jeff's implementation.
  • Loading branch information
willow-ahrens authored and Keno committed Sep 17, 2018
1 parent 2c15430 commit 1324ceb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 22 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,28 @@ function abstract_eval(@nospecialize(e), vtypes::VarTable, sv::InferenceState)
t = abstract_eval_call(e.args, argtypes, vtypes, sv)
elseif e.head === :new
t = instanceof_tfunc(abstract_eval(e.args[1], vtypes, sv))[1]
for i = 2:length(e.args)
if abstract_eval(e.args[i], vtypes, sv) === Bottom
rt = Bottom
if isbitstype(t)
args = Vector{Any}(undef, length(e.args)-1)
isconst = true
for i = 2:length(e.args)
at = abstract_eval(e.args[i], vtypes, sv)
if at === Bottom
t = Bottom
isconst = false
break
elseif at isa Const
if !(at.val isa fieldtype(t, i - 1))
t = Bottom
isconst = false
break
end
args[i-1] = at.val
else
isconst = false
end
end
if isconst
t = Const(ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), t, args, length(args)))
end
end
elseif e.head === :&
Expand Down
7 changes: 7 additions & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@ f21771(::Val{U}) where {U} = Tuple{g21771(U)}
@test @inferred(f21771(Val{Union{}}())) === Tuple{Union{}}
@test @inferred(f21771(Val{Integer}())) === Tuple{Integer}

# PR #28284, check that constants propagate through calls to new
struct t28284
x::Int
end
f28284() = Val(t28284(1))
@inferred f28284()

# missing method should be inferred as Union{}, ref https://github.com/JuliaLang/julia/issues/20033#issuecomment-282228948
@test Base.return_types(f -> f(1), (typeof((x::String) -> x),)) == Any[Union{}]

Expand Down

0 comments on commit 1324ceb

Please sign in to comment.