diff --git a/base/essentials.jl b/base/essentials.jl index 35a52e592434e..575d3aac0e9d0 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -264,11 +264,22 @@ function typename(a::Union) end typename(union::UnionAll) = typename(union.body) -convert(::Type{T}, x::T) where {T<:Tuple{Any, Vararg{Any}}} = x -convert(::Type{Tuple{}}, x::Tuple{Any, Vararg{Any}}) = throw(MethodError(convert, (Tuple{}, x))) -convert(::Type{T}, x::Tuple{Any, Vararg{Any}}) where {T<:Tuple} = +const AtLeast1 = Tuple{Any, Vararg{Any}} + +# converting to empty tuple type +convert(::Type{Tuple{}}, ::Tuple{}) = () +convert(::Type{Tuple{}}, x::AtLeast1) = throw(MethodError(convert, (Tuple{}, x))) + +# converting to tuple types with at least one element +convert(::Type{T}, x::T) where {T<:AtLeast1} = x +convert(::Type{T}, x::AtLeast1) where {T<:AtLeast1} = (convert(tuple_type_head(T), x[1]), convert(tuple_type_tail(T), tail(x))...) +# converting to Vararg tuple types +convert(::Type{Tuple{Vararg{V}}}, x::Tuple{Vararg{V}}) where {V} = x +convert(T::Type{Tuple{Vararg{V}}}, x::Tuple) where {V} = + (convert(tuple_type_head(T), x[1]), convert(T, tail(x))...) + # TODO: the following definitions are equivalent (behaviorally) to the above method # I think they may be faster / more efficient for inference, # if we could enable them, but are they? diff --git a/test/ambiguous.jl b/test/ambiguous.jl index 6835a2c1977f7..02d076d5eb8ba 100644 --- a/test/ambiguous.jl +++ b/test/ambiguous.jl @@ -273,6 +273,8 @@ end pop!(need_to_handle_undef_sparam, first(methods(Core.Compiler.same_names))) pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, (Type{Union{Core.Compiler.Some{T}, Nothing}} where T, Core.Compiler.Some))) pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, (Type{Union{T, Nothing}} where T, Core.Compiler.Some))) + pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{}})) + pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{Int8}})) @test need_to_handle_undef_sparam == Set() end let need_to_handle_undef_sparam = @@ -295,6 +297,8 @@ end pop!(need_to_handle_undef_sparam, which(Base.nonmissingtype, Tuple{Type{Union{Missing, T}} where T})) pop!(need_to_handle_undef_sparam, which(Base.convert, (Type{Union{Some{T}, Nothing}} where T, Some))) pop!(need_to_handle_undef_sparam, which(Base.convert, (Type{Union{T, Nothing}} where T, Some))) + pop!(need_to_handle_undef_sparam, which(Base.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{}})) + pop!(need_to_handle_undef_sparam, which(Base.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{Int8}})) @test need_to_handle_undef_sparam == Set() end end diff --git a/test/tuple.jl b/test/tuple.jl index 11dc498086226..3a22f3b089557 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -86,6 +86,9 @@ end end @test empty((1, 2.0, "c")) === () + + # issue #28915 + @test convert(Union{Tuple{}, Tuple{Int}}, (1,)) === (1,) end @testset "size" begin