Skip to content

Commit 09d3b26

Browse files
JeffBezansontkelman
authored andcommitted
fix #21848, bug in widening done by limit_type_depth
This would widen e.g. `Complex{T} where T` to `_ <: Complex`, which is valid in some sense but yields a type that's not a supertype of the input, which is bad. (cherry picked from commit f46ba0a) ref #21907
1 parent 9564404 commit 09d3b26

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

base/inference.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,19 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool, vars::Vector{TypeVar}=TypeV
706706
P = t.parameters
707707
isempty(P) && return t
708708
if d > MAX_TYPE_DEPTH
709-
cov && return t.name.wrapper
710-
var = TypeVar(:_, t.name.wrapper)
709+
if isvarargtype(t)
710+
# never replace Vararg with non-Vararg
711+
return Vararg{limit_type_depth(P[1], d, cov, vars), P[2]}
712+
end
713+
widert = t.name.wrapper
714+
if !(t <: widert)
715+
# This can happen when a typevar has bounds too wide for its context, e.g.
716+
# `Complex{T} where T` is not a subtype of `Complex`. In that case widen even
717+
# faster to something safe to ensure the result is a supertype of the input.
718+
widert = Any
719+
end
720+
cov && return widert
721+
var = TypeVar(:_, widert)
711722
push!(vars, var)
712723
return var
713724
end

test/inference.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,14 @@ function break_21369()
777777
end
778778
end
779779
@test_throws ErrorException break_21369() # not TypeError
780+
781+
# issue #21848
782+
@test Core.Inference.limit_type_depth(Ref{Complex{T} where T}, Core.Inference.MAX_TYPE_DEPTH) == Ref
783+
let T = Tuple{Tuple{Int64, Void},
784+
Tuple{Tuple{Int64, Void},
785+
Tuple{Int64, Tuple{Tuple{Int64, Void},
786+
Tuple{Tuple{Int64, Void}, Tuple{Int64, Tuple{Tuple{Int64, Void}, Tuple{Tuple, Tuple}}}}}}}}
787+
@test Core.Inference.limit_type_depth(T, 0) >: T
788+
@test Core.Inference.limit_type_depth(T, 1) >: T
789+
@test Core.Inference.limit_type_depth(T, 2) >: T
790+
end

0 commit comments

Comments
 (0)