diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index 5d4d52172a300..ed09d5316473a 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -156,7 +156,15 @@ function subst_trivial_bounds(@nospecialize(atypes)) end v = atypes.var if isconcretetype(v.ub) || v.lb === v.ub - return subst_trivial_bounds(atypes{v.ub}) + subst = try + atypes{v.ub} + catch + # Note in rare cases a var bound might not be valid to substitute. + nothing + end + if subst !== nothing + return subst_trivial_bounds(subst) + end end return UnionAll(v, subst_trivial_bounds(atypes.body)) end diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 1e61cc9e44094..4ce4eca03b91e 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -3403,3 +3403,8 @@ end @test @inferred(f40177(T)) == fieldtype(T, 1) end end + +# issue #41908 +f41908(x::Complex{T}) where {String<:T<:String} = 1 +g41908() = f41908(Any[1][1]) +@test only(Base.return_types(g41908, ())) <: Int