From 8a751e64a3bfab5ae4b309d2ed8e2f75e96fee59 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Sun, 6 Jun 2021 16:33:47 +0900 Subject: [PATCH] fix #40814, improve type stability of `_totuple` (#41074) Replaces #40987, as suggested by #40814. --- base/tuple.jl | 6 +++++- test/tuple.jl | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/tuple.jl b/base/tuple.jl index 5ebf36b750cf01..dc777e7497598f 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -327,7 +327,11 @@ function _totuple(T, itr, s...) @_inline_meta y = iterate(itr, s...) y === nothing && _totuple_err(T) - return (convert(fieldtype(T, 1), y[1]), _totuple(tuple_type_tail(T), itr, y[2])...) + t1 = convert(fieldtype(T, 1), y[1]) + # inference may give up in recursive calls, so annotate here to force accurate return type to be propagated + rT = tuple_type_tail(T) + ts = _totuple(rT, itr, y[2])::rT + return (t1, ts...) end # use iterative algorithm for long tuples diff --git a/test/tuple.jl b/test/tuple.jl index e8751e73b7ad45..672a7c6c350ea5 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -631,3 +631,6 @@ f38837(xs) = map((F,x)->F(x), (Float32, Float64), xs) @test_throws BoundsError (1, 2)[0:2] @test_throws ArgumentError (1, 2)[OffsetArrays.IdOffsetRange(1:2, -1)] end + +# https://github.com/JuliaLang/julia/issues/40814 +@test Base.return_types(NTuple{3,Int}, (Vector{Int},)) == Any[NTuple{3,Int}]