Skip to content

Commit d80f6e8

Browse files
committed
teach inference that T::Type when ::Type{T} is an argument
1 parent 828e76d commit d80f6e8

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

base/compiler/inferencestate.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,34 @@ function sptypes_from_meth_instance(linfo::MethodInstance)
143143
if has_free_typevars(ub)
144144
ub = Any
145145
end
146-
if Any <: ub
146+
lb = v.lb
147+
while lb isa TypeVar
148+
lb = lb.lb
149+
end
150+
if has_free_typevars(lb)
151+
lb = Bottom
152+
end
153+
if Any <: ub && lb <: Bottom
147154
ty = Any
148-
else
149-
lb = v.lb
150-
while lb isa TypeVar
151-
lb = lb.lb
155+
# if this parameter came from arg::Type{T}, we know that T::Type
156+
sig = linfo.def.sig
157+
temp = sig
158+
for j = 1:i-1
159+
temp = temp.body
152160
end
153-
if has_free_typevars(lb)
154-
lb = Bottom
161+
Pi = temp.var
162+
while temp isa UnionAll
163+
temp = temp.body
155164
end
165+
sigtypes = temp.parameters
166+
for j = 1:length(sigtypes)
167+
tj = sigtypes[j]
168+
if isType(tj) && tj.parameters[1] === Pi
169+
ty = Type
170+
break
171+
end
172+
end
173+
else
156174
tv = TypeVar(v.name, lb, ub)
157175
ty = UnionAll(tv, Type{tv})
158176
end

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,3 +2204,6 @@ _rttf_test(::Int128) = 0
22042204
_call_rttf_test() = Core.Compiler.return_type(_rttf_test, Tuple{Any})
22052205
@test Core.Compiler.return_type(_rttf_test, Tuple{Any}) === Int
22062206
@test _call_rttf_test() === Int
2207+
2208+
f_with_Type_arg(::Type{T}) where {T} = T
2209+
@test Base.return_types(f_with_Type_arg, (Any,)) == Any[Type]

0 commit comments

Comments
 (0)