Skip to content

Commit e53c607

Browse files
committed
reorder operations so some of them come before the recursive part
1 parent c57067f commit e53c607

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

base/promotion.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,22 @@ function _promote_type_binary(T::Type, S::Type, recursion_depth_limit::Tuple{Var
346346
end
347347
detect_loop_onesided(T::Type, S::Type, A::Type, B::Type) = _types_are_equal(T, A) && _types_are_equal(S, B)
348348
detect_loop(T::Type, S::Type, A::Type, B::Type) = detect_loop_onesided(T, S, A, B) || detect_loop_onesided(T, S, B, A)
349-
if _type_is_bottom(T)
350-
return S
351-
end
352-
if _type_is_bottom(S) || _types_are_equal(S, T)
353-
return T
354-
end
355349
# Try promote_rule in both orders.
356350
ts = promote_rule(T, S)
357351
st = promote_rule(S, T)
358352
# If no promote_rule is defined, both directions give Bottom. In that
359353
# case use typejoin on the original types instead.
360-
if _type_is_bottom(st) && _type_is_bottom(ts)
354+
st_is_bottom = _type_is_bottom(st)
355+
ts_is_bottom = _type_is_bottom(ts)
356+
if st_is_bottom && ts_is_bottom
361357
return typejoin(T, S)
362358
end
359+
if ts_is_bottom
360+
return st
361+
end
362+
if st_is_bottom || _types_are_equal(st, ts)
363+
return ts
364+
end
363365
if detect_loop(T, S, ts, st)
364366
# This is not strictly necessary, as we already limit the recursion depth, but
365367
# makes for nicer UX.
@@ -385,6 +387,12 @@ const _promote_type_binary_recursion_depth_limit = let
385387
end
386388

387389
function promote_type(::Type{T}, ::Type{S}) where {T,S}
390+
if _type_is_bottom(T)
391+
return S
392+
end
393+
if _type_is_bottom(S) || _types_are_equal(S, T)
394+
return T
395+
end
388396
_promote_type_binary(T, S, _promote_type_binary_recursion_depth_limit)
389397
end
390398

0 commit comments

Comments
 (0)