|
| 1 | +""" |
| 2 | + parameters(type) |
| 3 | +Extracts the type-parameters of the `type`. |
| 4 | +e.g. `parameters(Foo{A, B, C}) == [A, B, C]` |
| 5 | +""" |
| 6 | +parameters(sig::UnionAll) = parameters(sig.body) |
| 7 | +parameters(sig::DataType) = sig.parameters |
| 8 | +parameters(sig::Union) = Base.uniontypes(sig) |
| 9 | + |
| 10 | +@testset "Make sure methods haven't been added to DataType/UnionAll/Union" begin |
| 11 | + # if someone wrote e.g. `rrule(::typeof(Foo), x)` rather than `rrule(::Type{<:Foo}, x)` |
| 12 | + # then that would actually define `rrule(::DataType, x)` which would be bad |
| 13 | + # This test checks for that and fails if such a method exists. |
| 14 | + for method in methods(rrule) |
| 15 | + function_type = if method.sig <: Tuple{typeof(rrule), RuleConfig, Type, Vararg} |
| 16 | + parameters(method.sig)[3] |
| 17 | + elseif method.sig <: Tuple{typeof(rrule), Type, Vararg} |
| 18 | + @show parameters(method.sig)[2] |
| 19 | + else |
| 20 | + nothing |
| 21 | + end |
| 22 | + |
| 23 | + if function_type == DataType || function_type == UnionAll || function_type == Union |
| 24 | + @error "Bad constructor rrule. typeof(T)` not `Type{T}`" method |
| 25 | + @test false |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + # frule |
| 30 | + for method in methods(frule) |
| 31 | + function_type = if method.sig <: Tuple{typeof(frule), RuleConfig, Any, Type, Vararg} |
| 32 | + parameters(method.sig)[3] |
| 33 | + elseif method.sig <: Tuple{typeof(frule), Any, Type, Vararg} |
| 34 | + @show parameters(method.sig)[2] |
| 35 | + else |
| 36 | + nothing |
| 37 | + end |
| 38 | + |
| 39 | + if function_type == DataType || function_type == UnionAll || function_type == Union |
| 40 | + @error "Bad constructor frule. typeof(T)` not `Type{T}`" method |
| 41 | + @test false |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments