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

Fix type instability when setindex!! #549

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,34 +538,11 @@ function remove_parent_lens(vn_parent::VarName{sym}, vn_child::VarName{sym}) whe
end

# HACK: All of these are related to https://github.com/JuliaFolds/BangBang.jl/issues/233
# and https://github.com/JuliaFolds/BangBang.jl/pull/238.
# and https://github.com/JuliaFolds/BangBang.jl/pull/238, https://github.com/JuliaFolds2/BangBang.jl/pull/16.
# HACK(torfjelde): Avoids type-instability in `dot_assume` for `SimpleVarInfo`.
function BangBang.possible(
::typeof(BangBang._setindex!), ::C, ::T, ::Colon, ::Integer
) where {C<:AbstractMatrix,T<:AbstractVector}
return BangBang.implements(setindex!, C) &&
promote_type(eltype(C), eltype(T)) <: eltype(C)
end
function BangBang.possible(
::typeof(BangBang._setindex!), ::C, ::T, ::AbstractPPL.ConcretizedSlice, ::Integer
) where {C<:AbstractMatrix,T<:AbstractVector}
return BangBang.implements(setindex!, C) &&
promote_type(eltype(C), eltype(T)) <: eltype(C)
end
# HACK: Makes it possible to use ranges, etc. for setting a vector.
# For example, without this hack, BangBang.jl will consider
#
# x[1:2] = [1, 2]
#
# as NOT supported. This results is calling the immutable
# `BangBang.setindex` instead, which also ends up expanding the
# type of the containing array (`x` in the above scenario) to
# have element type `Any`.
# The below code just, correctly, marks this as possible and
# thus we hit the mutable `setindex!` instead.
function BangBang.possible(
::typeof(BangBang._setindex!), ::C, ::T, ::AbstractVector{<:Integer}
) where {C<:AbstractVector,T<:AbstractVector}
::typeof(BangBang._setindex!), ::C, ::T, ::Vararg
) where {C<:AbstractArray,T<:AbstractArray}
return BangBang.implements(setindex!, C) &&
promote_type(eltype(C), eltype(T)) <: eltype(C)
end
Expand Down
27 changes: 27 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,31 @@
x = rand(dist)
@test vectorize(dist, x) == vec(x.UL)
end

@testset "BangBang.possible" begin
a = zeros(3, 3, 3, 3) # also allow varname concretization
svi = SimpleVarInfo(Dict(@varname(a) => a))
DynamicPPL.setindex!!(svi, ones(3, 2), @varname(a[1, 1:3, 1, 1:2]))
@test eltype(svi[@varname(a)]) != Any

DynamicPPL.setindex!!(svi, ones(3), @varname(a[1, 1, :, 1]))
@test eltype(svi[@varname(a)]) != Any

DynamicPPL.setindex!!(svi, [1, 2], @varname(a[[5, 8]]))
@test eltype(svi[@varname(a)]) != Any

DynamicPPL.setindex!!(
svi,
[1, 2],
@varname(a[[CartesianIndex(1, 1, 3, 1), CartesianIndex(1, 1, 3, 2)]])
)
@test eltype(svi[@varname(a)]) != Any

svi = SimpleVarInfo(Dict(@varname(b) => [zeros(2), zeros(3)]))
DynamicPPL.setindex!!(svi, ones(2), @varname(b[1]))
@test eltype(svi[@varname(b)][1]) != Any

DynamicPPL.setindex!!(svi, ones(2), @varname(b[2][1:2]))
@test eltype(svi[@varname(b)][2]) != Any
end
end
Loading