Skip to content

Commit 15e7369

Browse files
committed
improve inference of methods with Type{leaftype} parameters
1 parent 67b9b3e commit 15e7369

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

base/inference.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ mutable struct InferenceState
220220
if isa(atyp, DataType) && isdefined(atyp, :instance)
221221
# replace singleton types with their equivalent Const object
222222
atyp = Const(atyp.instance)
223+
elseif isconstType(atyp)
224+
atype = Const(atyp.parameters[1])
223225
else
224226
atyp = rewrap_unionall(atyp, linfo.specTypes)
225227
end
@@ -999,7 +1001,7 @@ function apply_type_tfunc(headtypetype::ANY, args::ANY...)
9991001
ai = args[i]
10001002
if isType(ai)
10011003
aip1 = ai.parameters[1]
1002-
canconst &= isleaftype(aip1)
1004+
canconst &= (isleaftype(aip1) || aip1 === Union{})
10031005
push!(tparams, aip1)
10041006
elseif isa(ai, Const) && (isa(ai.val, Type) || isa(ai.val, TypeVar) || valid_tparam(ai.val))
10051007
push!(tparams, ai.val)

test/inference.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,10 @@ end
828828
end
829829
@test length(code_typed(test_20902, (), optimize = false)) == 1
830830
@test length(code_typed(test_20902, (), optimize = false)) == 1
831+
832+
# normalization of arguments with constant Types as parameters
833+
g21771(T) = T
834+
f21771(::Val{U}) where {U} = Tuple{g21771(U)}
835+
@test @inferred(f21771(Val{Int}())) === Tuple{Int}
836+
@test @inferred(f21771(Val{Union{}}())) === Tuple{Union{}}
837+
@test Base.return_types(f21771, typeof((Val{Integer}(),))) == Any[Type{<:Tuple{Integer}}] # apply_type might be overly conservative here

0 commit comments

Comments
 (0)