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 on Julia 1.7 #33

Merged
merged 8 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ExprTools"
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
authors = ["Invenia Technical Computing"]
version = "0.1.7"
version = "0.1.8"

[compat]
julia = "1"
Expand Down
16 changes: 14 additions & 2 deletions src/method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ function name_of_type(x::Core.TypeName)
end

name_of_type(x::Symbol) = QuoteNode(x) # Literal type-param e.g. `Val{:foo}`
function name_of_type(x::T) where T # Literal type-param e.g. `Val{1}`
# If this error is thrown, there is an issue with out implementation
function name_of_type(x::T) where {T} # Literal type-param e.g. `Val{1}`
# If this error is thrown, there is an issue with our implementation
isbits(x) || throw(DomainError((x, T), "not a valid type-param"))
return x
end
Expand Down Expand Up @@ -178,6 +178,18 @@ function name_of_type(x::Union)
return :(Union{$(parameter_names...)})
end

# Vararg was changed not to be a type anymore in Julia 1.7
if isdefined(Core, :TypeofVararg)
function name_of_type(x::Core.TypeofVararg)
of_T = name_of_type(x.T)
if isdefined(x, :N)
:(Vararg{$of_T,$(name_of_type(x.N))})
else
:(Vararg{$of_T})
end
end
end

function arguments(m::Method, sig=m.sig)
arg_names = argument_names(m)
arg_types = argument_types(sig)
Expand Down
37 changes: 24 additions & 13 deletions test/method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ struct TestCallableStruct end
end

@testset "varadic Tuple" begin
@test_signature vt1(::Tuple{Vararg{Int64, N}}) where N = 2
@test_signature vt1(::Tuple{Vararg{Int64,N}}) where {N} = 2
VERSION >= v"1.7" && @test_signature vt2(::Tuple{Vararg{Int64}}) = 2
end

@testset "Scope Qualification" begin
Expand Down Expand Up @@ -131,21 +132,31 @@ struct TestCallableStruct end
end

@testset "vararg" begin
@test_signature f17(xs::Vararg{Any, N} where N) = 2
if VERSION >= v"1.7"
@test_signature f17(xs::Vararg{Any}) = 2
# `f17_alt(xs...) = 2` lowers to the same method as `f17`
# but has a different AST according to `splitdef` so we can't us @test_signature
f17_alt(xs...) = 2
test_matches(
signature(only_method(f17_alt)),
Dict(:name => :f17_alt, :args => [:(xs::Vararg{Any})]),
)

# `f17_alt(xs...) = 2` lowers to the same method as `f18`
# but has a different AST according to `splitdef` so we can't us @test_signature
f17_alt(xs...) = 2
test_matches(
signature(only_method(f17_alt)),
Dict(
:name => :f17_alt,
:args => [:(xs::(Vararg{Any, N} where N))]
@test_signature f18(xs::Vararg{Int64}) = 2
@test_signature f19(x, xs::Vararg{Any}) = 2x
else
@test_signature f17b(xs::Vararg{Any,N} where {N}) = 2
# `f17b_alt(xs...) = 2` lowers to the same method as `f17b`
# but has a different AST according to `splitdef` so we can't us @test_signature
f17b_alt(xs...) = 2
test_matches(
signature(only_method(f17b_alt)),
Dict(:name => :f17b_alt, :args => [:(xs::(Vararg{Any,N} where {N}))]),
)
)

@test_signature f18(xs::Vararg{Int64, N} where N) = 2
@test_signature f19(x, xs::Vararg{Any, N} where N) = 2x
@test_signature f18b(xs::Vararg{Int64,N} where {N}) = 2
@test_signature f19b(x, xs::Vararg{Any,N} where {N}) = 2x
end
end

@testset "kwargs" begin
Expand Down
20 changes: 10 additions & 10 deletions test/type_utils.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
@testset "type_utils.jl" begin
@testset "parameters" begin
# basic case
@test collect(parameters(AbstractArray{Float32, 3})) == [Float32, 3]
@test collect(parameters(AbstractArray{Float32,3})) == [Float32, 3]
# Type-alias
@test collect(parameters(Vector{Float64})) == [Float64, 1]

# Tuple
@test collect(parameters(Tuple{Int8, Bool})) == [Int8, Bool]
@test collect(parameters(Tuple{Int8,Bool})) == [Int8, Bool]

oxinabox marked this conversation as resolved.
Show resolved Hide resolved
# Tuple with fixed count Vararg
@test collect(parameters(Tuple{Int8, Vararg{Bool, 3}})) == [Int8, Bool, Bool, Bool]
@test collect(parameters(Tuple{Int8,Vararg{Bool,3}})) == [Int8, Bool, Bool, Bool]

# Tuple with varadic Vararg
a, b = collect(parameters(Tuple{Int8, Vararg{Bool}}))
a, b = collect(parameters(Tuple{Int8,Vararg{Bool}}))
@test a == Int8
@test b <: Vararg{Bool}
@test b == Vararg{Bool}

# TypeVar
tvar1 = parameters(Tuple{T} where T<:Number)[1]
tvar1 = parameters(Tuple{T} where {T<:Number})[1]
@test tvar1 isa TypeVar
@test tvar1.name == :T
@test tvar1.lb == Union{}
Expand All @@ -40,14 +40,14 @@
@test tvar4.ub == Real

# Union
@test Set(parameters(Union{Int8, Bool})) == Set([Int8, Bool])
@test Set(parameters(Union{Int8, Bool, Set})) == Set([Int8, Bool, Set])
@test Set(parameters(Union{Int8,Bool})) == Set([Int8, Bool])
@test Set(parameters(Union{Int8,Bool,Set})) == Set([Int8, Bool, Set])

oxinabox marked this conversation as resolved.
Show resolved Hide resolved
# Partially collapsing Union
@test Set(parameters(Union{Int8, Real, Set})) == Set([Real, Set])
@test Set(parameters(Union{Int8,Real,Set})) == Set([Real, Set])

# Unions with type-vars
umem1, umem2 = parameters(Union{Tuple{Z}, Set{Z}} where Z)
umem1, umem2 = parameters(Union{Tuple{Z},Set{Z}} where Z)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
utvar1 = parameters(umem1)[1]
utvar2 = parameters(umem2)[1]
@test utvar1 == utvar2
Expand Down