|
| 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 | + |
| 11 | +""" |
| 12 | + test_method_signature(frule|rrule, method) |
| 13 | +
|
| 14 | +Tests that the method signature is sensible. |
| 15 | +Right now this just means checking the rule is not being applied to `DataType`, `Union`, or |
| 16 | +`UnionAll`. |
| 17 | +which is easy to do accidentally when writing rules for constructors. |
| 18 | +It happens if you write e.g. `rrule(::typeof(Foo), x)` rather than `rrule(::Type{<:Foo}, x)`. |
| 19 | +This would then actually define `rrule(::DataType, x)`. (or `UnionAll` if `Foo` |
| 20 | +was parametric, or `Union` if `Foo` was a type alias for a `Union`) |
| 21 | +""" |
| 22 | +function test_method_signature end |
| 23 | + |
| 24 | +function test_method_signature(::typeof(rrule), method::Method) |
| 25 | + @testset "Sensible Constructors" begin |
| 26 | + function_type = if method.sig <: Tuple{Any, RuleConfig, Type, Vararg} |
| 27 | + _parameters(method.sig)[3] |
| 28 | + elseif method.sig <: Tuple{Any, Type, Vararg} |
| 29 | + _parameters(method.sig)[2] |
| 30 | + else |
| 31 | + nothing |
| 32 | + end |
| 33 | + |
| 34 | + @test_msg( |
| 35 | + "Bad constructor rrule. `typeof(T)` used rather than `Type{T}`. $method", |
| 36 | + function_type ∉ (DataType, UnionAll, Union) |
| 37 | + ) |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +function test_method_signature(::typeof(frule), method::Method) |
| 42 | + @testset "Sensible Constructors" begin |
| 43 | + function_type = if method.sig <: Tuple{Any, RuleConfig, Any, Type, Vararg} |
| 44 | + _parameters(method.sig)[4] |
| 45 | + elseif method.sig <: Tuple{Any, Any, Type, Vararg} |
| 46 | + _parameters(method.sig)[3] |
| 47 | + else |
| 48 | + nothing |
| 49 | + end |
| 50 | + |
| 51 | + @test_msg( |
| 52 | + "Bad constructor frule. `typeof(T)` used rather than `Type{T}`. $method", |
| 53 | + function_type ∉ (DataType, UnionAll, Union) |
| 54 | + ) |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +""" |
| 59 | + test_method_tables() |
| 60 | +
|
| 61 | +Checks that the method tables for `rrule` and `frule` are sensible. |
| 62 | +This in future may carry out a number of checks, but presently just checks to make sure that |
| 63 | +no rules have been added to the very general `DataType`, `Union` or `UnionAll` types, |
| 64 | +which is easy to do accidentally when writing rules for constructors. |
| 65 | +It happens if you write e.g. `rrule(::typeof(Foo), x)` rather than `rrule(::Type{<:Foo}, x)`. |
| 66 | +This would then actually define `rrule(::DataType, x)`. (or `UnionAll` if `Foo` |
| 67 | +was parametric, or `Union` if `Foo` was a type alias for a `Union`) |
| 68 | +""" |
| 69 | +function test_method_tables() |
| 70 | + @testset "Sensible Constructors" begin |
| 71 | + # if someone wrote e.g. `rrule(::typeof(Foo), x)` rather than |
| 72 | + # `rrule(::Type{<:Foo}, x)` then that would actually define `rrule(::DataType, x)` |
| 73 | + # which would be bad. This test checks for that and fails if such a method exists. |
| 74 | + for method in methods(rrule) |
| 75 | + test_method_signature(rrule, method) |
| 76 | + end |
| 77 | + # frule |
| 78 | + for method in methods(frule) |
| 79 | + test_method_signature(frule, method) |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments