Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw descriptive BoundsError in copyto! #46192

Merged
merged 1 commit into from
Jul 27, 2022
Merged

Conversation

LilithHafner
Copy link
Member

Before, we got a BoundsError, now we should get a BoundsError that describes the Array and indices. The errors thrown are the same as for dest[doffs:doffs+n-1] .= src[soffs:soffs+n-1].

julia> x = rand(Int, 4); y = rand(4);

julia> copyto!(x, 1, y, 2, 4) # before
ERROR: BoundsError
Stacktrace:
 [1] _copyto_impl!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Base ./array.jl:326
 [2] copyto!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Base ./array.jl:319
 [3] top-level scope
   @ REPL[97]:1

julia> copyto!(x, 1, y, 2, 4) # after
ERROR: BoundsError: attempt to access 4-element Vector{Float64} at index [2:5]
Stacktrace:
 [1] throw_boundserror(A::Vector{Float64}, I::Tuple{UnitRange{Int64}})
   @ Base ./abstractarray.jl:715
 [2] checkbounds
   @ ./abstractarray.jl:680 [inlined]
 [3] _copyto_impl!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Main ./REPL[99]:5
 [4] copyto!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Base ./array.jl:314
 [5] top-level scope
   @ REPL[103]:1

julia> copyto!(x, 2, y, 2, 4)
ERROR: BoundsError: attempt to access 4-element Vector{Int64} at index [2:5]
Stacktrace:
 [1] throw_boundserror(A::Vector{Int64}, I::Tuple{UnitRange{Int64}})
   @ Base ./abstractarray.jl:715
 [2] checkbounds
   @ ./abstractarray.jl:680 [inlined]
 [3] _copyto_impl!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Main ./REPL[99]:4
 [4] copyto!(dest::Vector{Int64}, doffs::Int64, src::Vector{Float64}, soffs::Int64, n::Int64)
   @ Base ./array.jl:314
 [5] top-level scope
   @ REPL[104]:1

julia> x[1:4] .= y[2:5]
ERROR: BoundsError: attempt to access 4-element Vector{Float64} at index [2:5]
Stacktrace:
 [1] throw_boundserror(A::Vector{Float64}, I::Tuple{UnitRange{Int64}})
   @ Base ./abstractarray.jl:715
 [2] checkbounds
   @ ./abstractarray.jl:680 [inlined]
 [3] getindex(A::Vector{Float64}, I::UnitRange{Int64})
   @ Base ./array.jl:922
 [4] top-level scope
   @ REPL[105]:1

julia> x[2:5] .= y[2:5]
ERROR: BoundsError: attempt to access 4-element Vector{Int64} at index [2:5]
Stacktrace:
 [1] throw_boundserror(A::Vector{Int64}, I::Tuple{UnitRange{Int64}})
   @ Base ./abstractarray.jl:715
 [2] checkbounds
   @ ./abstractarray.jl:680 [inlined]
 [3] view
   @ ./subarray.jl:177 [inlined]
 [4] maybeview
   @ ./views.jl:148 [inlined]
 [5] dotview(::Vector{Int64}, ::UnitRange{Int64})
   @ Base.Broadcast ./broadcast.jl:1214
 [6] top-level scope
   @ REPL[106]:1

@LilithHafner LilithHafner added domain:arrays [a, r, r, a, y, s] domain:error messages Better, more actionable error messages labels Jul 27, 2022
Copy link
Member

@fredrikekre fredrikekre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I looked at this exact code yesterday, but didn't bother making a PR 🙂

@LilithHafner
Copy link
Member Author

Thanks! I was debugging a sorting algorithm and saw room for improvement in the error messages.

@fredrikekre fredrikekre merged commit b2bf56e into master Jul 27, 2022
@fredrikekre fredrikekre deleted the LilithHafner-patch-1 branch July 27, 2022 15:43
ffucci pushed a commit to ffucci/julia that referenced this pull request Aug 11, 2022
pcjentsch pushed a commit to pcjentsch/julia that referenced this pull request Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:error messages Better, more actionable error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants