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
16 changes: 16 additions & 0 deletions src/fwds_rvs_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ function fdata(t::T) where {T}
return F(fdata(t.fields))
end

function fdata(::Type{T}) where {T}
throw(
error(
"$T is a type. Perhaps you meant fdata_type($T) or fdata(instance_of_tangent)?"
),
)
end

function fdata(t::T) where {T<:PossiblyUninitTangent}
F = fdata_type(T)
return is_init(t) ? F(fdata(val(t))) : F()
Expand Down Expand Up @@ -530,6 +538,14 @@ function rdata(t::T) where {T}
return R(rdata(t.fields))
end

function rdata(::Type{T}) where {T}
throw(
error(
"$T is a type. Perhaps you meant rdata_type($T) or rdata(instance_of_tangent)?"
),
)
end

function rdata(t::T) where {T<:PossiblyUninitTangent}
R = rdata_type(T)
return is_init(t) ? R(rdata(val(t))) : R()
Expand Down
37 changes: 37 additions & 0 deletions test/fwds_rvs_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,41 @@ end
@test verify_rdata_value(Ptr{Float64}(), NoRData()) === nothing
end
end

@testset "Error messages for types passed to accessor functions" begin
# Test that passing types to fdata throws helpful error message
@test_throws "Float64 is a type. Perhaps you meant fdata_type(Float64) or fdata(instance_of_tangent)?" fdata(
Float64
)
@test_throws "Int64 is a type. Perhaps you meant fdata_type(Int64) or fdata(instance_of_tangent)?" fdata(
Int64
)

# Test that passing types to rdata throws helpful error message
@test_throws "Float64 is a type. Perhaps you meant rdata_type(Float64) or rdata(instance_of_tangent)?" rdata(
Float64
)
@test_throws "Int64 is a type. Perhaps you meant rdata_type(Int64) or rdata(instance_of_tangent)?" rdata(
Int64
)

# Test with more complex types - using exact string matching since the message is deterministic
@test_throws "Vector{Float64} is a type. Perhaps you meant fdata_type(Vector{Float64}) or fdata(instance_of_tangent)?" fdata(
Vector{Float64}
)
@test_throws "Vector{Float64} is a type. Perhaps you meant rdata_type(Vector{Float64}) or rdata(instance_of_tangent)?" rdata(
Vector{Float64}
)

# Verify that existing functionality with instances still works
x = 5.0
t = zero_tangent(x)
@test fdata(t) isa NoFData
@test rdata(t) isa Float64

arr = [1.0, 2.0]
t_arr = zero_tangent(arr)
@test fdata(t_arr) isa Vector{Float64}
@test rdata(t_arr) isa NoRData
end
end