Skip to content

Commit fb402c2

Browse files
mbaumanKristofferC
authored andcommitted
fix #32442, broadcasting over non-offset arrays with mismatched axis eltypes (#34230)
In cases where we have multiple arrays with `OneTo` axes that do not share the same axis eltype, we should simply default to constructing a new array with `OneTo{Int}` axes. (cherry picked from commit 11e7c33)
1 parent e6b2a8e commit fb402c2

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

base/broadcast.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ _bcsm(a::Number, b::Number) = a == b || b == 1
496496
# (We may not want to define general promotion rules between, say, OneTo and Slice, but if
497497
# we get here we know the axes are at least consistent for the purposes of broadcasting)
498498
axistype(a::T, b::T) where T = a
499+
axistype(a::OneTo, b::OneTo) = OneTo{Int}(a)
499500
axistype(a, b) = UnitRange{Int}(a)
500501

501502
## Check that all arguments are broadcast compatible with shape

test/ranges.jl

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -572,24 +572,40 @@ end
572572
@test sum(0:0.000001:1) == 500000.5
573573
@test sum(0:0.1:10) == 505.
574574
end
575-
@testset "broadcasted operations with scalars" begin
576-
@test broadcast(-, 1:3) === -1:-1:-3
577-
@test broadcast(-, 1:3, 2) === -1:1
578-
@test broadcast(-, 1:3, 0.25) === 1-0.25:3-0.25
579-
@test broadcast(+, 1:3) === 1:3
580-
@test broadcast(+, 1:3, 2) === 3:5
581-
@test broadcast(+, 1:3, 0.25) === 1+0.25:3+0.25
582-
@test broadcast(+, 1:2:6, 1) === 2:2:6
583-
@test broadcast(+, 1:2:6, 0.3) === 1+0.3:2:5+0.3
584-
@test broadcast(-, 1:2:6, 1) === 0:2:4
585-
@test broadcast(-, 1:2:6, 0.3) === 1-0.3:2:5-0.3
586-
@test broadcast(-, 2, 1:3) === 1:-1:-1
587-
end
588-
@testset "operations between ranges and arrays" begin
589-
@test all(([1:5;] + (5:-1:1)) .== 6)
590-
@test all(((5:-1:1) + [1:5;]) .== 6)
591-
@test all(([1:5;] - (1:5)) .== 0)
592-
@test all(((1:5) - [1:5;]) .== 0)
575+
@testset "broadcasted operations with scalars" for T in (Int, UInt, Int128)
576+
@test broadcast(-, T(1):3, 2) === T(1)-2:1
577+
@test broadcast(-, T(1):3, 0.25) === T(1)-0.25:3-0.25
578+
@test broadcast(+, T(1):3) === T(1):3
579+
@test broadcast(+, T(1):3, 2) === T(3):5
580+
@test broadcast(+, T(1):3, 0.25) === T(1)+0.25:3+0.25
581+
@test broadcast(+, T(1):2:6, 1) === T(2):2:6
582+
@test broadcast(+, T(1):2:6, 0.3) === T(1)+0.3:2:5+0.3
583+
@test broadcast(-, T(1):2:6, 1) === T(0):2:4
584+
@test broadcast(-, T(1):2:6, 0.3) === T(1)-0.3:2:5-0.3
585+
if T <: Unsigned
586+
@test_broken broadcast(-, T(1):3) == -T(1):-1:-T(3)
587+
@test_broken broadcast(-, 2, T(1):3) == T(1):-1:-T(1)
588+
else
589+
@test length(broadcast(-, T(1):3, 2)) === length(T(1)-2:T(3)-2)
590+
@test broadcast(-, T(1):3) == -T(1):-1:-T(3)
591+
@test broadcast(-, 2, T(1):3) == T(1):-1:-T(1)
592+
end
593+
end
594+
@testset "operations between ranges and arrays" for T in (Int, UInt, Int128)
595+
@test all(([T(1):5;] + (T(5):-1:1)) .=== T(6))
596+
@test all(((T(5):-1:1) + [T(1):5;]) .=== T(6))
597+
@test all(([T(1):5;] - (T(1):5)) .=== T(0))
598+
@test all(((T(1):5) - [T(1):5;]) .=== T(0))
599+
end
600+
@testset "issue #32442: Broadcasting over views with non-`Int` indices" begin
601+
a=rand(UInt32,20)
602+
c=rand(UInt64,5)
603+
@test reinterpret(UInt64,view(a,UInt64.(11:20))) .- c ==
604+
reinterpret(UInt64,view(a,(11:20))) .- c ==
605+
reinterpret(UInt64,view(a,(UInt64(11):UInt64(20)))) .- c ==
606+
copy(reinterpret(UInt64,view(a,(UInt64(11):UInt64(20))))) .- c
607+
608+
@test view(a,(Int32(11):Int32(20))) .+ [1] == a[11:20] .+ 1
593609
end
594610
@testset "tricky floating-point ranges" begin
595611
for (start, step, stop, len) in ((1, 1, 3, 3), (0, 1, 3, 4),

0 commit comments

Comments
 (0)