Skip to content

Don't claim arrays with a Union{} element type for TrackedStyle - #325

Merged
devmotion merged 1 commit into
masterfrom
dmw/broadcaststyle-union
Oct 25, 2026
Merged

devmotion merged 1 commit into
masterfrom
dmw/broadcaststyle-union

Conversation

@devmotion

Copy link
Copy Markdown
Member

Fixes #324.

Union{} <: TrackedReal, so the covariant rule BroadcastStyle(::Type{<:AbstractArray{<:TrackedReal{<:Any, D}, N}}) from #301 also matched e.g. Vector{Union{}}. That is type piracy, and once ReverseDiff was loaded, String.(Union{}[]) errored in getouttype because D was unbound.

Changes

  • BroadcastStyle lists the element types explicitly, with invariant parameters: TrackedReal{V, D, O}, with O free, with V <: Real free, and with both free. Union{} cannot match an invariant parameter, so no Union{} method is needed. A free V has to carry its bound V <: Real to equal the element types that e.g. typejoin builds.
  • getouttype stays covariant and gets a guard, getouttype(::AbstractArray{Union{}}) = Union{}. A Union{} array can still reach a tracked broadcast next to a tracked argument, e.g. x .+ Union{}[].

Behaviour

  • Unchanged: TrackedArray, its Adjoint/SubArray/reshape, arrays of TrackedReal that fix D, and static arrays, whose own style still wins.
  • No longer claimed: element types that are a Union of different TrackedReals, or that carry odd bounds like TrackedReal{<:Float64, …}. As in 1.18.5, they broadcast with the scalar rules, so the derivatives are the same but the result is an Array{<:TrackedReal}.

🤖 Generated with Claude Code

`Union{} <: TrackedReal`, so the covariant `BroadcastStyle` rule from #301 also claimed
e.g. `Vector{Union{}}`, and broadcasting over it errored in `getouttype` once ReverseDiff
was loaded. List the element types explicitly with invariant parameters instead, which
`Union{}` cannot match, and guard `getouttype` against a `Union{}` array that reaches a
tracked broadcast next to a tracked argument.

Fixes #324

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.29%. Comparing base (9d49db8) to head (a315c14).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #325      +/-   ##
==========================================
+ Coverage   94.27%   94.29%   +0.01%     
==========================================
  Files          19       19              
  Lines        1695     1699       +4     
==========================================
+ Hits         1598     1602       +4     
  Misses         97       97              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devmotion
devmotion merged commit 51cda83 into master Oct 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1.18.6: broadcasting over an empty Vector{Union{}} throws UndefVarError: D once ReverseDiff is loaded

1 participant