Skip to content

Commit

Permalink
Assume :nothrow in iterating over tuples (#54330)
Browse files Browse the repository at this point in the history
On master,
```julia
julia> Base.infer_effects(iterate, (Tuple{Int,Int,Int}, Int))
(+c,+e,!n,+t,+s,+m,+u)
```
Since the indexing is only carried out for valid indices, it should be
safe to mark this as `nothrow`.
After this PR,
```julia
julia> Base.infer_effects(iterate, (Tuple{Int,Int,Int}, Int))
(+c,+e,+n,+t,+s,+m,+u)
```

---------

Co-authored-by: Shuhei Kadowaki <[email protected]>
  • Loading branch information
jishnub and aviatesk authored May 2, 2024
1 parent b385b4f commit f712512
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ end

function iterate(@nospecialize(t::Tuple), i::Int=1)
@inline
@_nothrow_meta
return (1 <= i <= length(t)) ? (t[i], i + 1) : nothing
end

Expand Down
2 changes: 2 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ end

@test eachindex((2,5,"foo")) === Base.OneTo(3)
@test eachindex((2,5,"foo"), (1,2,5,7)) === Base.OneTo(4)

@test Core.Compiler.is_nothrow(Base.infer_effects(iterate, (Tuple{Int,Int,Int}, Int)))
end


Expand Down

0 comments on commit f712512

Please sign in to comment.