Skip to content

Commit eb85e3b

Browse files
authored
Change _setindex implementation for tuples to improve constant propagation (#40635)
1 parent 3dcf799 commit eb85e3b

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

base/tuple.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ function setindex(x::Tuple, v, i::Integer)
5555
_setindex(v, i, x...)
5656
end
5757

58-
function _setindex(v, i::Integer, first, tail...)
58+
function _setindex(v, i::Integer, args...)
5959
@_inline_meta
60-
return (ifelse(i == 1, v, first), _setindex(v, i - 1, tail...)...)
60+
return ntuple(j -> ifelse(j == i, v, args[j]), length(args))
6161
end
62-
_setindex(v, i::Integer) = ()
6362

6463

6564
## iterating ##

test/tuple.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ end
533533

534534
@test Base.setindex((1, 2, 4), 4, true) === (4, 2, 4)
535535
@test_throws BoundsError Base.setindex((1, 2), 2, false)
536+
537+
f() = Base.setindex((1:1, 2:2, 3:3), 9, 1)
538+
@test @inferred(f()) == (9, 2:2, 3:3)
536539
end
537540

538541
@testset "inferrable range indexing with constant values" begin

0 commit comments

Comments
 (0)