Skip to content

Commit

Permalink
Fix find_first_eq (#38)
Browse files Browse the repository at this point in the history
This fixes a problem where `find_first_eq` was failing to find values when there were a mix
of dynamic and static values.
  • Loading branch information
Tokazama authored Feb 4, 2022
1 parent 5d3ad39 commit 4b54d39
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Static"
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
version = "0.5.1"
version = "0.5.2"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
12 changes: 6 additions & 6 deletions src/tuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ eachop_tuple(op, itr, arg, args...) = _eachop_tuple(op, itr, arg, args)
Expr(:block, Expr(:meta, :inline), t)
end


#=
find_first_eq(x, collection::Tuple)
Expand All @@ -74,12 +73,13 @@ If `x` and `collection` are static (`is_static`) and `x` is in `collection` then
value is a `StaticInt`.
=#
@generated function find_first_eq(x::X, itr::I) where {X,N,I<:Tuple{Vararg{Any,N}}}
if (is_static(X) & is_static(I)) === True()
return Expr(:block, Expr(:meta, :inline),
:(Base.Cartesian.@nif $(N + 1) d->(x === getfield(itr, d)) d->(static(d)) d->(nothing)))
# we avoid incidental code gen when evaluated a tuple of known values by iterating
# through `I.parameters` instead of `known(I)`.
index = ifelse(known(X) === missing, nothing, findfirst(==(X), I.parameters))
if index === nothing
:(Base.Cartesian.@nif $(N + 1) d->(x == getfield(itr, d)) d->(d) d->(nothing))
else
return Expr(:block, Expr(:meta, :inline),
:(Base.Cartesian.@nif $(N + 1) d->(x === getfield(itr, d)) d->(d) d->(nothing)))
:($(static(index)))
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,14 @@ using Test
@test @inferred(Static.permute(x, y)) === y
@test @inferred(Static.eachop(getindex, x)) === x


@test Static.field_type(typeof((x = 1, y = 2)), :x) <: Int
@test Static.field_type(typeof((x = 1, y = 2)), static(:x)) <: Int
get_tuple_add(::Type{T}, ::Type{X}, dim::StaticInt) where {T,X} = Tuple{Static.field_type(T, dim),X}
@test @inferred(Static.eachop_tuple(Static.field_type, y, T)) === Tuple{String,Float64,Int}
@test @inferred(Static.eachop_tuple(get_tuple_add, y, T, String)) === Tuple{Tuple{String,String},Tuple{Float64,String},Tuple{Int,String}}
@test @inferred(Static.find_first_eq(static(1), y)) === static(3)
@test @inferred(Static.find_first_eq(static(2), (1, static(2)))) === static(2)
@test @inferred(Static.find_first_eq(static(2), (1, static(2), 3))) === static(2)
# inferred is Union{Int,Nothing}
@test Static.find_first_eq(1, map(Int, y)) === 3

Expand Down

2 comments on commit 4b54d39

@Tokazama
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/53843

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.2 -m "<description of version>" 4b54d39ce755e48f415cc6778ba804f07622e761
git push origin v0.5.2

Please sign in to comment.