Skip to content

Commit

Permalink
Throw on non-promotion in fallback UnitRange constructor (JuliaLang…
Browse files Browse the repository at this point in the history
…#50292)

* Throw on promotion failure in UnitRange

* rename variables
  • Loading branch information
jishnub authored Jun 27, 2023
1 parent fc8b700 commit 6f26e4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ struct UnitRange{T<:Real} <: AbstractUnitRange{T}
end
UnitRange{T}(start, stop) where {T<:Real} = UnitRange{T}(convert(T, start), convert(T, stop))
UnitRange(start::T, stop::T) where {T<:Real} = UnitRange{T}(start, stop)
UnitRange(start, stop) = UnitRange(promote(start, stop)...)
function UnitRange(start, stop)
startstop_promoted = promote(start, stop)
not_sametype((start, stop), startstop_promoted)
UnitRange(startstop_promoted...)
end

# if stop and start are integral, we know that their difference is a multiple of 1
unitrange_last(start::Integer, stop::Integer) =
Expand Down
3 changes: 3 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ using InteractiveUtils: code_llvm
@test last(10:0.2:3) === 9.8
@test step(10:0.2:3) === 0.2
@test isempty(10:0.2:3)

unitrangeerrstr = "promotion of types Char and Char failed to change any arguments"
@test_throws unitrangeerrstr UnitRange('a', 'b')
end

using Dates, Random
Expand Down

0 comments on commit 6f26e4f

Please sign in to comment.