A bug with diff() for a UnitRange input?

I ran across the following bug in Julia 1.5.0-beta1.0 in diff() when the input is of type UnitRange. The output is also very weird for StepRangeLen inputs. My understanding is that UnitRange is an AbstractArray type. I tested with Julia 1.1.0 and Julia 1.6.0-DEV, both work flawlessly. Is this a bug with diff()? or something else?

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.5.0-beta1.0 (2020-05-28)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> diff(0:4)
ERROR: ArgumentError: step cannot be zero
Stacktrace:
 [1] steprange_last(::Int64, ::Int64, ::Int64) at .\range.jl:215
 [2] StepRange at .\range.jl:205 [inlined]
 [3] _rangestyle at .\range.jl:118 [inlined]
 [4] _range at .\range.jl:116 [inlined]
 [5] #range#43 at .\range.jl:91 [inlined]
 [6] -(::UnitRange{Int64}, ::UnitRange{Int64}) at .\range.jl:1044
 [7] broadcasted at .\broadcast.jl:1081 [inlined]
 [8] broadcasted at .\broadcast.jl:1263 [inlined]
 [9] diff(::UnitRange{Int64}; dims::Int64) at .\multidimensional.jl:849
 [10] diff(::UnitRange{Int64}) at .\multidimensional.jl:809
 [11] top-level scope at REPL[1]:1

julia> diff(0.0:4)
1.0:0.0:1.0

This is the output in Julia 1.6.0-DEV:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.0-DEV.281 (2020-06-20)
 _/ |\__'_|_|_|\__'_|  |  Commit f6d34c3c32 (3 days old master)
|__/                   |

julia> diff(0:4)
4-element Array{Int64,1}:
 1
 1
 1
 1

julia> diff(0.0:4)
4-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0

Yup, this was a bug and is getting fixed on the next 1.5 release candidate.

https://github.com/JuliaLang/julia/pull/36117

1 Like

Yes, nice to know that. Thanks Matt.