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 1 commit
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
6 changes: 6 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ function BangBang.possible(
return BangBang.implements(setindex!, C) &&
promote_type(eltype(C), eltype(T)) <: eltype(C)
end
function BangBang.possible(
::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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is type piracy, isn't it? It shouldn't be defined in DynamicPPL but in BangBang itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @torfjelde already open a PR at JuliaFolds/BangBang.jl#238.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repo is inactive. JuliaFolds was forked to JuliaFolds2, so the PR has to be opened in https://github.com/JuliaFolds2/BangBang.jl.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to get a timely review and release there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also verified locally that the BangBang PR should fix the issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, I was not aware of this! Okay, that's quite good to know 👍

And yeah, we should do that 👍

One thing though: we probably don't need the other overloads with this Vararg version, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing the other should be safe 👍.

Maybe if the JuliaFold2 folks are responsive, this PR can be just removing all the BangBang.possible


# HACK(torfjelde): This makes it so it works on iterators, etc. by default.
# TODO(torfjelde): Do better.
Expand Down
20 changes: 20 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@
x = rand(dist)
@test vectorize(dist, x) == vec(x.UL)
end

@testset "BangBang.possible" begin
a = zeros(3, 3, 3, 3)
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
end
end
Loading