From 1c6ea4a678513551cc00d9c94251e26d51169a80 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 15 May 2020 13:22:02 -0400 Subject: [PATCH] in inference, disallow 3-element Unions if one elt is abstract --- base/compiler/typelimits.jl | 5 ++++- test/compiler/inference.jl | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/base/compiler/typelimits.jl b/base/compiler/typelimits.jl index 2c8d7a5805573..88948421f1c01 100644 --- a/base/compiler/typelimits.jl +++ b/base/compiler/typelimits.jl @@ -268,8 +268,11 @@ function type_more_complex(@nospecialize(t), @nospecialize(c), sources::SimpleVe return true end +union_count_abstract(x::Union) = union_count_abstract(x.a) + union_count_abstract(x.b) +union_count_abstract(@nospecialize(x)) = !isdispatchelem(x) + function issimpleenoughtype(@nospecialize t) - return unionlen(t) <= MAX_TYPEUNION_LENGTH && unioncomplexity(t) <= MAX_TYPEUNION_COMPLEXITY + return unionlen(t)+union_count_abstract(t) <= MAX_TYPEUNION_LENGTH && unioncomplexity(t) <= MAX_TYPEUNION_COMPLEXITY end # pick a wider type that contains both typea and typeb, diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 7092b8fc38e97..f1ddc7349c843 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -91,10 +91,10 @@ tmerge_test(Tuple{ComplexF64, ComplexF64, ComplexF32}, Tuple{Vararg{Union{Comple Tuple{Vararg{Complex}}, false) tmerge_test(Tuple{}, Tuple{Complex, Vararg{Union{ComplexF32, ComplexF64}}}, Tuple{Vararg{Complex}}) -@test Core.Compiler.tmerge(Tuple{}, Union{Int16, Nothing, Tuple{ComplexF32, ComplexF32}}) == - Union{Int16, Nothing, Tuple{Vararg{ComplexF32}}} -@test Core.Compiler.tmerge(Union{Int32, Nothing, Tuple{ComplexF32}}, Union{Int32, Nothing, Tuple{ComplexF32, ComplexF32}}) == - Union{Int32, Nothing, Tuple{Vararg{ComplexF32}}} +@test Core.Compiler.tmerge(Tuple{}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) == + Union{Nothing, Tuple{Vararg{ComplexF32}}} +@test Core.Compiler.tmerge(Union{Nothing, Tuple{ComplexF32}}, Union{Nothing, Tuple{ComplexF32, ComplexF32}}) == + Union{Nothing, Tuple{Vararg{ComplexF32}}} # issue 9770 @noinline x9770() = false