Skip to content

Commit 1324ceb

Browse files
willow-ahrensKeno
authored andcommitted
Propagate constant calls to new! (#28284)
* this is the thing that infers new. * more stuff! * Incorporating Jeff's suggestions. * Added tests and used Jeff's implementation.
1 parent 2c15430 commit 1324ceb

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,28 @@ function abstract_eval(@nospecialize(e), vtypes::VarTable, sv::InferenceState)
904904
t = abstract_eval_call(e.args, argtypes, vtypes, sv)
905905
elseif e.head === :new
906906
t = instanceof_tfunc(abstract_eval(e.args[1], vtypes, sv))[1]
907-
for i = 2:length(e.args)
908-
if abstract_eval(e.args[i], vtypes, sv) === Bottom
909-
rt = Bottom
907+
if isbitstype(t)
908+
args = Vector{Any}(undef, length(e.args)-1)
909+
isconst = true
910+
for i = 2:length(e.args)
911+
at = abstract_eval(e.args[i], vtypes, sv)
912+
if at === Bottom
913+
t = Bottom
914+
isconst = false
915+
break
916+
elseif at isa Const
917+
if !(at.val isa fieldtype(t, i - 1))
918+
t = Bottom
919+
isconst = false
920+
break
921+
end
922+
args[i-1] = at.val
923+
else
924+
isconst = false
925+
end
926+
end
927+
if isconst
928+
t = Const(ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), t, args, length(args)))
910929
end
911930
end
912931
elseif e.head === :&

test/compiler/compiler.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,13 @@ f21771(::Val{U}) where {U} = Tuple{g21771(U)}
932932
@test @inferred(f21771(Val{Union{}}())) === Tuple{Union{}}
933933
@test @inferred(f21771(Val{Integer}())) === Tuple{Integer}
934934

935+
# PR #28284, check that constants propagate through calls to new
936+
struct t28284
937+
x::Int
938+
end
939+
f28284() = Val(t28284(1))
940+
@inferred f28284()
941+
935942
# missing method should be inferred as Union{}, ref https://github.com/JuliaLang/julia/issues/20033#issuecomment-282228948
936943
@test Base.return_types(f -> f(1), (typeof((x::String) -> x),)) == Any[Union{}]
937944

0 commit comments

Comments
 (0)