Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Int rather than Int64/Integer is intentional
function frule((_, ẋ), ::typeof(getfield), x::Tuple, i::Int)
return x.i, ẋ.i
function ChainRulesCore.frule((_, Δ, _), ::typeof(getfield), strct, sym::Union{Int,Symbol})
return (getfield(strct, sym), isa(Δ, NoTangent) ? NoTangent() : getproperty(Δ, sym))
end

function ChainRulesCore.frule((_, Δ, _, _), ::typeof(getfield), strct, sym::Union{Int,Symbol}, inbounds)
return (getfield(strct, sym, inbounds), isa(Δ, NoTangent) ? NoTangent() : getproperty(Δ, sym))
end

"for a given tuple type, returns a Val{N} where N is the length of the tuple"
Expand Down
16 changes: 16 additions & 0 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
struct FooTwoField
x::Float64
y::Float64
end


@testset "getfield" begin
test_frule(getfield, FooTwoField(1.5, 2.5), :x, check_inferred=false)

test_frule(getfield, (; a=1.5, b=2.5), :a, check_inferred=false)
test_frule(getfield, (; a=1.5, b=2.5), 2)

test_frule(getfield, (1.5, 2.5), 2)
test_frule(getfield, (1.5, 2.5), 2, true)
end

@testset "getindex" begin
@testset "getindex(::Tuple, ...)" begin
x = (1.2, 3.4, 5.6)
Expand Down