Skip to content

Commit

Permalink
Check issimpleenoughtype before stripping off type parameters in tmer…
Browse files Browse the repository at this point in the history
…ge (JuliaLang#39980)

* Check issimpleenoughtype before stripping off type parameters

* Avoid finalizer to be compiled during test_jl_dump_compiles_toplevel_thunks
  • Loading branch information
tkf authored and antoine-levitt committed May 9, 2021
1 parent 15b265e commit 5fa21da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
(uw isa DataType && ti <: uw.name.wrapper) || return Any
typenames[i] = uw.name
end
u = Union{types...}
if issimpleenoughtype(u)
return u
end
# see if any of the union elements have the same TypeName
# in which case, simplify this tmerge by replacing it with
# the widest possible version of itself (the wrapper)
Expand Down
8 changes: 4 additions & 4 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -955,18 +955,18 @@ p0 = copy(p)
@test isequal([1, 2] .+ [3.0, missing], [4.0, missing])
@test Core.Compiler.return_type(broadcast, Tuple{typeof(+), Vector{Int},
Vector{Union{Float64, Missing}}}) ==
Vector{<:Union{Float64, Missing}}
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
@test isequal([1, 2] + [3.0, missing], [4.0, missing])
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
Vector{Union{Float64, Missing}}}) ==
Vector{<:Union{Float64, Missing}}
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
@test Core.Compiler.return_type(+, Tuple{Vector{Int},
Vector{Union{Float64, Missing}}}) ==
Vector{<:Union{Float64, Missing}}
Union{Vector{Missing}, Vector{Union{Missing, Float64}}, Vector{Float64}}
@test isequal(tuple.([1, 2], [3.0, missing]), [(1, 3.0), (2, missing)])
@test Core.Compiler.return_type(broadcast, Tuple{typeof(tuple), Vector{Int},
Vector{Union{Float64, Missing}}}) ==
Vector{<:Tuple{Int, Any}}
Union{Vector{Tuple{Int, Missing}}, Vector{Tuple{Int, Any}}, Vector{Tuple{Int, Float64}}}
# Check that corner cases do not throw an error
@test isequal(broadcast(x -> x === 1 ? nothing : x, [1, 2, missing]),
[nothing, 2, missing])
Expand Down
2 changes: 2 additions & 0 deletions test/compiler/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ function test_jl_dump_compiles_toplevel_thunks()
# Make sure to cause compilation of the eval function
# before calling it below.
Core.eval(Main, Any[:(nothing)][1])
GC.enable(false) # avoid finalizers to be compiled
topthunk = Meta.lower(Main, :(for i in 1:10; end))
ccall(:jl_dump_compiles, Cvoid, (Ptr{Cvoid},), io.handle)
Core.eval(Main, topthunk)
ccall(:jl_dump_compiles, Cvoid, (Ptr{Cvoid},), C_NULL)
close(io)
GC.enable(true)
tstats = stat(tfile)
tempty = tstats.size == 0
rm(tfile)
Expand Down
18 changes: 17 additions & 1 deletion test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ tmerge_test(Tuple{ComplexF64, ComplexF64, ComplexF32}, Tuple{Vararg{Union{Comple
tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}},
Tuple{Vararg{Complex}})
@test Core.Compiler.tmerge(Tuple{}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Nothing, Tuple{}, Tuple{ComplexF32, ComplexF32}}
@test Core.Compiler.tmerge(Tuple{}, Union{Nothing, Tuple{ComplexF32}, Tuple{ComplexF32, ComplexF32}}) ==
Union{Nothing, Tuple{Vararg{ComplexF32}}}
@test Core.Compiler.tmerge(Union{Nothing, Tuple{ComplexF32}}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Nothing, Tuple{ComplexF32}, Tuple{ComplexF32, ComplexF32}}
@test Core.Compiler.tmerge(Union{Nothing, Tuple{}, Tuple{ComplexF32}}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) ==
Union{Nothing, Tuple{Vararg{ComplexF32}}}
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Vector{Bool})) == Vector
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Vector{Bool})) ==
Union{Vector{Bool}, Vector{Int}, Vector{String}}
@test Core.Compiler.tmerge(Vector{Int}, Core.Compiler.tmerge(Vector{String}, Union{Vector{Bool}, Vector{Symbol}})) == Vector
@test Core.Compiler.tmerge(Base.BitIntegerType, Union{}) === Base.BitIntegerType
@test Core.Compiler.tmerge(Union{}, Base.BitIntegerType) === Base.BitIntegerType
@test Core.Compiler.tmerge(Core.Compiler.InterConditional(1, Int, Union{}), Core.Compiler.InterConditional(2, String, Union{})) === Core.Compiler.Const(true)
Expand Down Expand Up @@ -3227,3 +3233,13 @@ end
return y
end == Any[Union{Nothing, Tuple{String, Union{Nothing, String}}}]
end

@test Base.return_types((Int,)) do x
if x === 0
Some(0.0)
elseif x == 1
Some(1)
else
Some(0x2)
end
end == [Union{Some{Float64}, Some{Int}, Some{UInt8}}]
4 changes: 2 additions & 2 deletions test/worlds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ B265(x::Any, dummy::Nothing) = B265{UInt8}(x, dummy)
@test (B265_(2)::B265{Float64}).field1 === 2.0e0
@test (B265_(3)::B265{UInt8}).field1 === 0x03

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


# test oldworld call / inference
Expand Down

0 comments on commit 5fa21da

Please sign in to comment.