diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index 571313e51bf76..a9543bcac4eba 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -78,7 +78,13 @@ function instanceof_tfunc(@nospecialize(t)) elseif isa(t, UnionAll) t′ = unwrap_unionall(t) t′′, isexact = instanceof_tfunc(t′) - return rewrap_unionall(t′′, t), isexact + tr = rewrap_unionall(t′′, t) + if t′′ isa DataType && !has_free_typevars(tr) + # a real instance must be within the declared bounds of the type, + # so we can intersect with the original wrapper. + tr = typeintersect(tr, t′′.name.wrapper) + end + return tr, isexact elseif isa(t, Union) ta, isexact_a = instanceof_tfunc(t.a) tb, isexact_b = instanceof_tfunc(t.b) diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index d82d4d96b7065..9609043f78652 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -2442,3 +2442,6 @@ f31974(n::Int) = f31974(1:n) # This query hangs if type inference improperly attempts to const prop # call cycles. @test code_typed(f31974, Tuple{Int}) !== nothing + +f_overly_abstract_complex() = Complex(Ref{Number}(1)[]) +@test Base.return_types(f_overly_abstract_complex, Tuple{}) == [Complex]