diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index 3b84395c676d2..45c66a773815f 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -155,7 +155,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 481fd84b87932..2e3dd0b45f875 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -3411,3 +3411,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