diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index e047c7d83fac3..3ed40537ecb9a 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -154,7 +154,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 9e461b9c39824..9238ce65475f5 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -3085,3 +3085,8 @@ end end return x end) === Union{Int, Float64, Char} + +# issue #41908 +f41908(x::Complex{T}) where {String<:T<:String} = 1 +g41908() = f41908(Any[1][1]) +@test only(Base.return_types(g41908, ())) <: Int