Skip to content

Commit 3fdc246

Browse files
authored
Merge pull request #33 from invenia/ox/17
Fix on Julia 1.7
2 parents 090c74b + 8983be6 commit 3fdc246

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ExprTools"
22
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
33
authors = ["Invenia Technical Computing"]
4-
version = "0.1.7"
4+
version = "0.1.8"
55

66
[compat]
77
julia = "1"

src/method.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ function name_of_type(x::Core.TypeName)
141141
end
142142

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

181+
# Vararg was changed not to be a type anymore in Julia 1.7
182+
if isdefined(Core, :TypeofVararg)
183+
function name_of_type(x::Core.TypeofVararg)
184+
of_T = isdefined(x, :T) ? name_of_type(x.T) : :Any
185+
if isdefined(x, :N)
186+
:(Vararg{$of_T,$(name_of_type(x.N))})
187+
else
188+
:(Vararg{$of_T})
189+
end
190+
end
191+
end
192+
181193
function arguments(m::Method, sig=m.sig)
182194
arg_names = argument_names(m)
183195
arg_types = argument_types(sig)

test/method.jl

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ struct TestCallableStruct end
5959
end
6060

6161
@testset "varadic Tuple" begin
62-
@test_signature vt1(::Tuple{Vararg{Int64, N}}) where N = 2
62+
@test_signature vt1(::Tuple{Vararg{Int64,N}}) where {N} = 2
63+
VERSION >= v"1.7" && @test_signature vt2(::Tuple{Vararg{Int64}}) = 2
6364
end
6465

6566
@testset "Scope Qualification" begin
@@ -131,21 +132,31 @@ struct TestCallableStruct end
131132
end
132133

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

136-
# `f17_alt(xs...) = 2` lowers to the same method as `f18`
137-
# but has a different AST according to `splitdef` so we can't us @test_signature
138-
f17_alt(xs...) = 2
139-
test_matches(
140-
signature(only_method(f17_alt)),
141-
Dict(
142-
:name => :f17_alt,
143-
:args => [:(xs::(Vararg{Any, N} where N))]
145+
@test_signature f18(xs::Vararg{Int64}) = 2
146+
@test_signature f19(x, xs::Vararg{Any}) = 2x
147+
else
148+
@test_signature f17b(xs::Vararg{Any,N} where {N}) = 2
149+
# `f17b_alt(xs...) = 2` lowers to the same method as `f17b`
150+
# but has a different AST according to `splitdef` so we can't us @test_signature
151+
f17b_alt(xs...) = 2
152+
test_matches(
153+
signature(only_method(f17b_alt)),
154+
Dict(:name => :f17b_alt, :args => [:(xs::(Vararg{Any,N} where {N}))]),
144155
)
145-
)
146156

147-
@test_signature f18(xs::Vararg{Int64, N} where N) = 2
148-
@test_signature f19(x, xs::Vararg{Any, N} where N) = 2x
157+
@test_signature f18b(xs::Vararg{Int64,N} where {N}) = 2
158+
@test_signature f19b(x, xs::Vararg{Any,N} where {N}) = 2x
159+
end
149160
end
150161

151162
@testset "kwargs" begin
@@ -312,7 +323,17 @@ struct TestCallableStruct end
312323
@test length(no_hygiene[:whereparams]) == 1
313324
@test no_hygiene[:whereparams] != hygiene[:whereparams] # different Symbols
314325
# very coarse test to make sure the renamed arg is in the expression it should be
315-
@test occursin(string(no_hygiene[:whereparams][1]), string(no_hygiene[:args][1]))
326+
@test occursin(
327+
string(no_hygiene[:whereparams][1]), string(no_hygiene[:args][1])
328+
)
329+
end
330+
end
331+
332+
@testset "internals" begin
333+
@testset "name_of_type" begin
334+
# This isn't part of the public API, and isn't currently hit by anything that is
335+
# but it really seems like it should work.
336+
VERSION >= v"1.7" && @test ExprTools.name_of_type(Vararg) == :(Vararg{Any})
316337
end
317338
end
318339
end

test/type_utils.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
@testset "type_utils.jl" begin
22
@testset "parameters" begin
33
# basic case
4-
@test collect(parameters(AbstractArray{Float32, 3})) == [Float32, 3]
4+
@test collect(parameters(AbstractArray{Float32,3})) == [Float32, 3]
55
# Type-alias
66
@test collect(parameters(Vector{Float64})) == [Float64, 1]
77

88
# Tuple
9-
@test collect(parameters(Tuple{Int8, Bool})) == [Int8, Bool]
10-
9+
@test collect(parameters(Tuple{Int8,Bool})) == [Int8, Bool]
1110
# Tuple with fixed count Vararg
12-
@test collect(parameters(Tuple{Int8, Vararg{Bool, 3}})) == [Int8, Bool, Bool, Bool]
11+
@test collect(parameters(Tuple{Int8,Vararg{Bool,3}})) == [Int8, Bool, Bool, Bool]
1312

1413
# Tuple with varadic Vararg
15-
a, b = collect(parameters(Tuple{Int8, Vararg{Bool}}))
14+
a, b = collect(parameters(Tuple{Int8,Vararg{Bool}}))
1615
@test a == Int8
17-
@test b <: Vararg{Bool}
16+
@test b == Vararg{Bool}
1817

1918
# TypeVar
20-
tvar1 = parameters(Tuple{T} where T<:Number)[1]
19+
tvar1 = parameters(Tuple{T} where {T<:Number})[1]
2120
@test tvar1 isa TypeVar
2221
@test tvar1.name == :T
2322
@test tvar1.lb == Union{}
@@ -40,14 +39,13 @@
4039
@test tvar4.ub == Real
4140

4241
# Union
43-
@test Set(parameters(Union{Int8, Bool})) == Set([Int8, Bool])
44-
@test Set(parameters(Union{Int8, Bool, Set})) == Set([Int8, Bool, Set])
45-
42+
@test Set(parameters(Union{Int8,Bool})) == Set([Int8, Bool])
43+
@test Set(parameters(Union{Int8,Bool,Set})) == Set([Int8, Bool, Set])
4644
# Partially collapsing Union
47-
@test Set(parameters(Union{Int8, Real, Set})) == Set([Real, Set])
45+
@test Set(parameters(Union{Int8,Real,Set})) == Set([Real, Set])
4846

4947
# Unions with type-vars
50-
umem1, umem2 = parameters(Union{Tuple{Z}, Set{Z}} where Z)
48+
umem1, umem2 = parameters(Union{Tuple{Z},Set{Z}} where {Z})
5149
utvar1 = parameters(umem1)[1]
5250
utvar2 = parameters(umem2)[1]
5351
@test utvar1 == utvar2

0 commit comments

Comments
 (0)