Skip to content

Commit

Permalink
Try to fix Undefined Behavior in DataLayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Oct 10, 2024
1 parent 0b6aa34 commit 5818fcd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/DataLayouts/DataLayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,23 @@ end
@generated function _getproperty(
data::AbstractData{S},
::Val{Name},
name::Symbol,
) where {S, Name}
errorstring = "Invalid field name $(Name) for type $(S)"
i = findfirst(isequal(Name), fieldnames(S))
if i === nothing
return :(error($errorstring))
end
static_idx = Val{i}()
return :(Base.@_inline_meta; DataLayouts._property_view(data, $static_idx))
return :(
Base.@_inline_meta; DataLayouts._property_view(data, $static_idx, name)
)
end

@inline function Base.getproperty(data::AbstractData{S}, name::Symbol) where {S}
_getproperty(data, Val{name}())
_getproperty(data, Val{name}(), name)
end
@inline function Base.dotgetproperty(
data::AbstractData{S},
name::Symbol,
) where {S}
_getproperty(data, Val{name}())
_getproperty(data, Val{name}(), name)
end

Base.@propagate_inbounds function Base.getproperty(
Expand All @@ -275,11 +274,18 @@ Base.@propagate_inbounds function Base.getproperty(
union_all(data){SS, Base.tail(type_params(data))...}(dataview)
end

@noinline _property_view(
data::AbstractData{S},
::Val{Nothing},
name,
) where {S} = error("Invalid field name $name for type $(S)")

# In the past, we've sometimes needed a generated function
# for inference and constant propagation:
Base.@propagate_inbounds @generated function _property_view(
data::AD,
::Val{Idx},
name,
) where {S, Idx, AD <: AbstractData{S}}
SS = fieldtype(S, Idx)
T = eltype(parent_array_type(AD))
Expand Down

0 comments on commit 5818fcd

Please sign in to comment.