Skip to content

Commit

Permalink
make Tuple{Union{}} unconstructable (#49111)
Browse files Browse the repository at this point in the history
Type intersection assumed it was equal to Union{}, so this makes it
unconstructable so that holds true. This is similar to what the
NamedTuple constructor does.

Secondarily, this fixes an inference bug where it would create
Vararg{Union{}} and then incorrectly handle that fieldtype.

- Fixes #32392
- Addresses part of the concerns discussed in
  JuliaLang/julia#24614 (comment)
- Addresses part of the issues presented in
  JuliaLang/julia#26175
- May allow improving jl_type_equality_is_identity
  (https://github.com/JuliaLang/julia/pull/49017/files#diff-882927c6e612596e22406ae0d06adcee88a9ec05e8b61ad81b48942e2cb266e9R986)
- May allow improving intersection (finish_unionall can be more
  aggressive at computing varval for any typevars that appears in
  covariant position and has lb=Union{} and ub=leaf type)
  • Loading branch information
vtjnash authored Apr 10, 2023
1 parent 7d3907a commit 4141fd5
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for (t1, t2) in ((:UnitUpperTriangular, :UpperTriangular),
(:UnitLowerTriangular, :LowerTriangular))
@eval begin
function (+)(UL::$t1, J::UniformScaling)
ULnew = copymutable_oftype(UL.data, Base._return_type(+, Tuple{eltype(UL), typeof(J)}))
ULnew = copymutable_oftype(UL.data, Base.promote_op(+, eltype(UL), typeof(J)))
for i in axes(ULnew, 1)
ULnew[i,i] = one(ULnew[i,i]) + J
end
Expand All @@ -193,7 +193,7 @@ end
# However, to preserve type stability, we do not special-case a
# UniformScaling{<:Complex} that happens to be real.
function (+)(A::Hermitian, J::UniformScaling{<:Complex})
TS = Base._return_type(+, Tuple{eltype(A), typeof(J)})
TS = Base.promote_op(+, eltype(A), typeof(J))
B = copytri!(copymutable_oftype(parent(A), TS), A.uplo, true)
for i in diagind(B)
B[i] = A[i] + J
Expand All @@ -202,7 +202,7 @@ function (+)(A::Hermitian, J::UniformScaling{<:Complex})
end

function (-)(J::UniformScaling{<:Complex}, A::Hermitian)
TS = Base._return_type(+, Tuple{eltype(A), typeof(J)})
TS = Base.promote_op(+, eltype(A), typeof(J))
B = copytri!(copymutable_oftype(parent(A), TS), A.uplo, true)
B .= .-B
for i in diagind(B)
Expand All @@ -213,7 +213,7 @@ end

function (+)(A::AbstractMatrix, J::UniformScaling)
checksquare(A)
B = copymutable_oftype(A, Base._return_type(+, Tuple{eltype(A), typeof(J)}))
B = copymutable_oftype(A, Base.promote_op(+, eltype(A), typeof(J)))
for i in intersect(axes(A,1), axes(A,2))
@inbounds B[i,i] += J
end
Expand All @@ -222,7 +222,7 @@ end

function (-)(J::UniformScaling, A::AbstractMatrix)
checksquare(A)
B = convert(AbstractMatrix{Base._return_type(+, Tuple{eltype(A), typeof(J)})}, -A)
B = convert(AbstractMatrix{Base.promote_op(+, eltype(A), typeof(J))}, -A)
for i in intersect(axes(A,1), axes(A,2))
@inbounds B[i,i] += J
end
Expand Down

0 comments on commit 4141fd5

Please sign in to comment.