Skip to content

Commit 946da64

Browse files
authored
ensure inbounds propagate (JuliaArrays#8)
1 parent d74fac9 commit 946da64

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/structarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Base.propertynames(s::StructArray) = fieldnames(typeof(columns(s)))
3838

3939
Base.size(s::StructArray) = size(columns(s)[1])
4040

41-
Base.getindex(s::StructArray, I::Int...) = get_ith(s, I...)
41+
Base.@propagate_inbounds Base.getindex(s::StructArray, I::Int...) = get_ith(s, I...)
4242
function Base.getindex(s::StructArray{T, N, C}, I::Union{Int, AbstractArray, Colon}...) where {T, N, C}
4343
StructArray{T}(map(v -> getindex(v, I...), columns(s)))
4444
end
@@ -47,7 +47,7 @@ function Base.view(s::StructArray{T, N, C}, I...) where {T, N, C}
4747
StructArray{T}(map(v -> view(v, I...), columns(s)))
4848
end
4949

50-
Base.setindex!(s::StructArray, val, I::Int...) = set_ith!(s, val, I...)
50+
Base.@propagate_inbounds Base.setindex!(s::StructArray, val, I::Int...) = set_ith!(s, val, I...)
5151

5252
fields(::Type{<:NamedTuple{K}}) where {K} = K
5353
fields(::Type{<:StructArray{T}}) where {T} = fields(T)

src/utils.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ eltypes(::Type{NamedTuple{K, V}}) where {K, V} = eltypes(V)
1111
field = Expr(:., :s, Expr(:quote, key))
1212
push!(args, :($field[I...]))
1313
end
14-
Expr(:call, :createinstance, :T, args...)
14+
return quote
15+
@boundscheck checkbounds(s, I...)
16+
@inbounds $(Expr(:call, :createinstance, :T, args...))
17+
end
1518
end
1619

1720
@generated function set_ith!(s::StructArray{T}, vals, I...) where {T}
@@ -22,7 +25,10 @@ end
2225
push!(args, :($field[I...] = $val))
2326
end
2427
push!(args, :s)
25-
Expr(:block, args...)
28+
return quote
29+
@boundscheck checkbounds(s, I...)
30+
@inbounds $(Expr(:block, args...))
31+
end
2632
end
2733

2834
createinstance(::Type{T}, args...) where {T} = T(args...)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Test
77
t = StructArray((a = a, b = b))
88
@test (@inferred t[2,2]) == (a = 4, b = 7)
99
@test (@inferred t[2,1:2]) == StructArray((a = [3, 4], b = [6, 7]))
10+
@test_throws BoundsError t[3,3]
1011
@test (@inferred view(t, 2, 1:2)) == StructArray((a = view(a, 2, 1:2), b = view(b, 2, 1:2)))
1112
end
1213

0 commit comments

Comments
 (0)