@@ -270,10 +270,22 @@ Base.convert(::Type{T}, t::T) where {T<:TrackedReal} = t
270270Base. convert (:: Type{T} , t:: T ) where {T<: TrackedArray } = t
271271
272272for R in REAL_TYPES
273- @eval Base. promote_rule (:: Type{$R} , :: Type{TrackedReal{V,D,O}} ) where {V,D,O} = TrackedReal{promote_type ($ R,V),D,O}
273+ R === :Dual && continue # ForwardDiff.Dual is handled below
274+ @eval begin
275+ if isconcretetype ($ R) # issue ForwardDiff#322
276+ Base. promote_rule (:: Type{TrackedReal{V,D,O}} , :: Type{$R} ) where {V,D,O} = TrackedReal{promote_type (V,$ R),D,O}
277+ Base. promote_rule (:: Type{$R} , :: Type{TrackedReal{V,D,O}} ) where {V,D,O} = TrackedReal{promote_type ($ R,V),D,O}
278+ else
279+ Base. promote_rule (:: Type{TrackedReal{V,D,O}} , :: Type{R} ) where {V,D,O,R<: $R } = TrackedReal{promote_type (V,R),D,O}
280+ Base. promote_rule (:: Type{R} , :: Type{TrackedReal{V,D,O}} ) where {R<: $R ,V,D,O,} = TrackedReal{promote_type (R,V),D,O}
281+ end
282+ end
274283end
275284
276- Base. promote_rule (:: Type{R} , :: Type{TrackedReal{V,D,O}} ) where {R<: Real ,V,D,O} = TrackedReal{promote_type (R,V),D,O}
285+ # Avoid method ambiguities for ForwardDiff.Dual
286+ Base. promote_rule (:: Type{TrackedReal{V1,D,O}} , :: Type{Dual{T,V2,N}} ) where {V1,D,O,T,V2,N} = TrackedReal{promote_type (V1,Dual{T,V2,N}),D,O}
287+ Base. promote_rule (:: Type{Dual{T,V1,N}} , :: Type{TrackedReal{V2,D,O}} ) where {T,V1,N,V2,D,O} = TrackedReal{promote_type (Dual{T,V1,N},V2),D,O}
288+
277289Base. promote_rule (:: Type{TrackedReal{V1,D1,O1}} , :: Type{TrackedReal{V2,D2,O2}} ) where {V1,V2,D1,D2,O1,O2} = TrackedReal{promote_type (V1,V2),promote_type (D1,D2),Nothing}
278290
279291# ##########################
0 commit comments