Skip to content

Commit b6c224b

Browse files
tkfKristofferC
authored andcommitted
Check issimpleenoughtype before stripping off type parameters in tmerge (#39980)
* Check issimpleenoughtype before stripping off type parameters * Avoid finalizer to be compiled during test_jl_dump_compiles_toplevel_thunks (cherry picked from commit 3635f04)
1 parent 31d59cb commit b6c224b

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

base/compiler/typelimits.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
395395
(uw isa DataType && ti <: uw.name.wrapper) || return Any
396396
typenames[i] = uw.name
397397
end
398+
u = Union{types...}
399+
if issimpleenoughtype(u)
400+
return u
401+
end
398402
# see if any of the union elements have the same TypeName
399403
# in which case, simplify this tmerge by replacing it with
400404
# the widest possible version of itself (the wrapper)

test/broadcast.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -955,18 +955,18 @@ p0 = copy(p)
955955
@test isequal([1, 2] .+ [3.0, missing], [4.0, missing])
956956
@test Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int},
957957
Vector{Union{Float64, Missing}}}) ==
958-
Vector{<:Union{Float64, Missing}}
958+
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
959959
@test isequal([1, 2] + [3.0, missing], [4.0, missing])
960960
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
961961
Vector{Union{Float64, Missing}}}) ==
962-
Vector{<:Union{Float64, Missing}}
962+
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
963963
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
964964
Vector{Union{Float64, Missing}}}) ==
965-
Vector{<:Union{Float64, Missing}}
965+
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
966966
@test isequal(tuple.([1, 2], [3.0, missing]), [(1, 3.0), (2, missing)])
967967
@test Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int},
968968
Vector{Union{Float64, Missing}}}) ==
969-
Vector{<:Tuple{Int, Any}}
969+
Union{Vector{Tuple{Int, Missing}}, Vector{Tuple{Int, Any}}, Vector{Tuple{Int, Float64}}}
970970
# Check that corner cases do not throw an error
971971
@test isequal(broadcast(x -> x === 1 ? nothing : x, [1, 2, missing]),
972972
[nothing, 2, missing])

test/compiler/codegen.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ function test_jl_dump_compiles_toplevel_thunks()
7373
# Make sure to cause compilation of the eval function
7474
# before calling it below.
7575
Core.eval(Main, Any[:(nothing)][1])
76+
GC.enable(false) # avoid finalizers to be compiled
7677
topthunk = Meta.lower(Main, :(for i in 1:10; end))
7778
ccall(:jl_dump_compiles, Cvoid, (Ptr{Cvoid},), io.handle)
7879
Core.eval(Main, topthunk)
7980
ccall(:jl_dump_compiles, Cvoid, (Ptr{Cvoid},), C_NULL)
8081
close(io)
82+
GC.enable(true)
8183
tstats = stat(tfile)
8284
tempty = tstats.size == 0
8385
rm(tfile)

test/compiler/inference.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,16 @@ tmerge_test(Tuple{ComplexF64, ComplexF64, ComplexF32}, Tuple{Vararg{Union{Comple
9595
tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}},
9696
Tuple{Vararg{Complex}})
9797
@test Core.Compiler.tmerge(Tuple{}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
98+
Union{Nothing, Tuple{}, Tuple{ComplexF32, ComplexF32}}
99+
@test Core.Compiler.tmerge(Tuple{}, Union{Nothing, Tuple{ComplexF32}, Tuple{ComplexF32, ComplexF32}}) ==
98100
Union{Nothing, Tuple{Vararg{ComplexF32}}}
99101
@test Core.Compiler.tmerge(Union{Nothing, Tuple{ComplexF32}}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
102+
Union{Nothing, Tuple{ComplexF32}, Tuple{ComplexF32, ComplexF32}}
103+
@test Core.Compiler.tmerge(Union{Nothing, Tuple{}, Tuple{ComplexF32}}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
100104
Union{Nothing, Tuple{Vararg{ComplexF32}}}
101-
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Vector{Bool})) == Vector
105+
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Vector{Bool})) ==
106+
Union{Vector{Bool}, Vector{Int}, Vector{String}}
107+
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Union{Vector{Bool}, Vector{Symbol}})) == Vector
102108
@test Core.Compiler.tmerge(Base.BitIntegerType, Union{}) === Base.BitIntegerType
103109
@test Core.Compiler.tmerge(Union{}, Base.BitIntegerType) === Base.BitIntegerType
104110

@@ -3002,3 +3008,13 @@ Base.return_types((Union{Int,Nothing},)) do x
30023008
end
30033009
x
30043010
end == [Int]
3011+
3012+
@test Base.return_types((Int,)) do x
3013+
if x === 0
3014+
Some(0.0)
3015+
elseif x == 1
3016+
Some(1)
3017+
else
3018+
Some(0x2)
3019+
end
3020+
end == [Union{Some{Float64}, Some{Int}, Some{UInt8}}]

test/worlds.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ B265(x::Any, dummy::Nothing) = B265{UInt8}(x, dummy)
8989
@test (B265_(2)::B265{Float64}).field1 === 2.0e0
9090
@test (B265_(3)::B265{UInt8}).field1 === 0x03
9191

92-
@test Base.return_types(B265_, (Int,)) == Any[B265]
93-
@test Core.Compiler.return_type(B265_, (Int,)) == B265
92+
@test B265{UInt8} <: only(Base.return_types(B265_, (Int,))) <: B265
93+
@test B265{UInt8} <: Core.Compiler.return_type(B265_, (Int,)) <: B265
9494

9595

9696
# test oldworld call / inference

0 commit comments

Comments
 (0)