Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit d4cdde1

Browse files
authoredSep 4, 2018
fix JuliaLang#28915, identity conversion to union of tuple types (JuliaLang#28951)
1 parent 14fb104 commit d4cdde1

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed
 

‎base/essentials.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,22 @@ function typename(a::Union)
264264
end
265265
typename(union::UnionAll) = typename(union.body)
266266

267-
convert(::Type{T}, x::T) where {T<:Tuple{Any, Vararg{Any}}} = x
268-
convert(::Type{Tuple{}}, x::Tuple{Any, Vararg{Any}}) = throw(MethodError(convert, (Tuple{}, x)))
269-
convert(::Type{T}, x::Tuple{Any, Vararg{Any}}) where {T<:Tuple} =
267+
const AtLeast1 = Tuple{Any, Vararg{Any}}
268+
269+
# converting to empty tuple type
270+
convert(::Type{Tuple{}}, ::Tuple{}) = ()
271+
convert(::Type{Tuple{}}, x::AtLeast1) = throw(MethodError(convert, (Tuple{}, x)))
272+
273+
# converting to tuple types with at least one element
274+
convert(::Type{T}, x::T) where {T<:AtLeast1} = x
275+
convert(::Type{T}, x::AtLeast1) where {T<:AtLeast1} =
270276
(convert(tuple_type_head(T), x[1]), convert(tuple_type_tail(T), tail(x))...)
271277

278+
# converting to Vararg tuple types
279+
convert(::Type{Tuple{Vararg{V}}}, x::Tuple{Vararg{V}}) where {V} = x
280+
convert(T::Type{Tuple{Vararg{V}}}, x::Tuple) where {V} =
281+
(convert(tuple_type_head(T), x[1]), convert(T, tail(x))...)
282+
272283
# TODO: the following definitions are equivalent (behaviorally) to the above method
273284
# I think they may be faster / more efficient for inference,
274285
# if we could enable them, but are they?

‎test/ambiguous.jl

+4
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ end
273273
pop!(need_to_handle_undef_sparam, first(methods(Core.Compiler.same_names)))
274274
pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, (Type{Union{Core.Compiler.Some{T}, Nothing}} where T, Core.Compiler.Some)))
275275
pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, (Type{Union{T, Nothing}} where T, Core.Compiler.Some)))
276+
pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{}}))
277+
pop!(need_to_handle_undef_sparam, which(Core.Compiler.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{Int8}}))
276278
@test need_to_handle_undef_sparam == Set()
277279
end
278280
let need_to_handle_undef_sparam =
@@ -295,6 +297,8 @@ end
295297
pop!(need_to_handle_undef_sparam, which(Base.nonmissingtype, Tuple{Type{Union{Missing, T}} where T}))
296298
pop!(need_to_handle_undef_sparam, which(Base.convert, (Type{Union{Some{T}, Nothing}} where T, Some)))
297299
pop!(need_to_handle_undef_sparam, which(Base.convert, (Type{Union{T, Nothing}} where T, Some)))
300+
pop!(need_to_handle_undef_sparam, which(Base.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{}}))
301+
pop!(need_to_handle_undef_sparam, which(Base.convert, Tuple{Type{Tuple{Vararg{Int}}}, Tuple{Int8}}))
298302
@test need_to_handle_undef_sparam == Set()
299303
end
300304
end

‎test/tuple.jl

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ end
8686
end
8787

8888
@test empty((1, 2.0, "c")) === ()
89+
90+
# issue #28915
91+
@test convert(Union{Tuple{}, Tuple{Int}}, (1,)) === (1,)
8992
end
9093

9194
@testset "size" begin

0 commit comments

Comments
 (0)
This repository has been archived.