Skip to content

Commit

Permalink
Unwrap y::UnionALL eagerly when x<:y if x isa ∀-var. (#49023)
Browse files Browse the repository at this point in the history
Close #45874
  • Loading branch information
N5N3 authored Mar 16, 2023
1 parent 3919a89 commit c1d1bde
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,16 @@ static int subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param)
// to other left-side variables, so using || here is safe.
return subtype(xub, y, e, param) || subtype(x, ylb, e, param);
}
if (jl_is_unionall(y)) {
jl_varbinding_t *xb = lookup(e, (jl_tvar_t*)x);
if (xb == NULL ? !e->ignore_free : !xb->right) {
// We'd better unwrap `y::UnionAll` eagerly if `x` isa ∀-var.
// This makes sure the following cases work correct:
// 1) `∀T <: Union{∃S, SomeType{P}} where {P}`: `S == Any` ==> `S >: T`
// 2) `∀T <: Union{∀T, SomeType{P}} where {P}`:
return subtype_unionall(x, (jl_unionall_t*)y, e, 1, param);
}
}
return var_lt((jl_tvar_t*)x, y, e, param);
}
if (jl_is_typevar(y))
Expand Down
12 changes: 5 additions & 7 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@ struct Z38497{T>:Int} <: Y38497{T} end
@test only(intersection_env(Union{S, Matrix{Int}} where S<:Matrix, Matrix)[2]) isa TypeVar
T46784{B<:Val, M<:AbstractMatrix} = Tuple{<:Union{B, <:Val{<:B}}, M, Union{AbstractMatrix{B}, AbstractMatrix{<:Vector{<:B}}}}
@testintersect(T46784{T,S} where {T,S}, T46784, !Union{})
@test_broken T46784 <: T46784{T,S} where {T,S}
@test T46784 <: T46784{T,S} where {T,S}

#issue 36185
let S = Tuple{Type{T},Array{Union{T,Missing},N}} where {T,N},
Expand Down Expand Up @@ -2376,12 +2376,10 @@ abstract type P47654{A} end

@testset "known subtype/intersect issue" begin
#issue 45874
# Causes a hang due to jl_critical_error calling back into malloc...
# let S = Pair{Val{P}, AbstractVector{<:Union{P,<:AbstractMatrix{P}}}} where P,
# T = Pair{Val{R}, AbstractVector{<:Union{P,<:AbstractMatrix{P}}}} where {P,R}
# @test_broken S <: T
# @test_broken typeintersect(S,T) === S
# end
let S = Pair{Val{P}, AbstractVector{<:Union{P,<:AbstractMatrix{P}}}} where P,
T = Pair{Val{R}, AbstractVector{<:Union{P,<:AbstractMatrix{P}}}} where {P,R}
@test S <: T
end

#issue 41561
@test_broken typeintersect(Tuple{Vector{VT}, Vector{VT}} where {N1, VT<:AbstractVector{N1}},
Expand Down

0 comments on commit c1d1bde

Please sign in to comment.