-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix regression in map
and collect
#43120
Conversation
`promote_typejoin_tuple` returned a type which was more precise than the one which `map` and `collect` actually use via `promote_type`, triggering an assertion error in corner cases.
@@ -195,7 +195,7 @@ function typejoin_union_tuple(T::Type) | |||
elseif U isa Union | |||
ci = typejoin(U.a, U.b) | |||
else | |||
ci = U | |||
ci = promote_typejoin_union(U) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vtjnash According to what you noted at #30485 (comment), it's not correct to call promote_typejoin_union
from here given that the function is marked as @pure
. But I don't know how to fix this otherwise, as without @pure
the result isn't inferred, which defeats the point of all this code.
EDIT: in the worst case we could do ci = Any
(at least to get a quick fix for 1.7), as we only need an upper bound for the type. But better be more precise if we can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly as your edit says. Note that promote_typejoin_union(U)
here is already defined to be ci = Any
, so that is the identical change too. And we could perhaps simplify the structure a bit:
diff --git a/base/promotion.jl b/base/promotion.jl
index 21245f0e05..70bb4c3b53 100644
--- a/base/promotion.jl
+++ b/base/promotion.jl
@@ -168,19 +168,17 @@ function promote_typejoin_union(::Type{T}) where T
return Any # TODO: compute more precise bounds
elseif T isa Union
return promote_typejoin(promote_typejoin_union(T.a), promote_typejoin_union(T.b))
- elseif T <: Tuple
- return typejoin_union_tuple(T)
- else
+ elseif T isa DataType
+ T <: Tuple && return typejoin_union_tuple(T)
return T
+ else
+ error("unreachable") # not a type??
end
end
-function typejoin_union_tuple(T::Type)
+function typejoin_union_tuple(T::DataType)
@_pure_meta
u = Base.unwrap_unionall(T)
- u isa Union && return typejoin(
- typejoin_union_tuple(Base.rewrap_unionall(u.a, T)),
- typejoin_union_tuple(Base.rewrap_unionall(u.b, T)))
p = (u::DataType).parameters
lr = length(p)::Int
if lr == 0
@@ -192,6 +190,8 @@ function typejoin_union_tuple(T::Type)
U = Core.Compiler.unwrapva(pi)
if U === Union{}
ci = Union{}
+ elseif U isa UnionAll
+ return Any # TODO: compute more precise bounds
elseif U isa Union
ci = typejoin(U.a, U.b)
else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like the wrong fix, but I think it has put me on the path of the correct one
That was exactly the plan. :-p I've pushed your fix. Let's hope we haven't missed yet another corner case that will be caught only later! |
Thanks! |
`promote_typejoin_tuple` returned a type which was more precise than the one which `map` and `collect` actually use via `promote_type`, triggering an assertion error in corner cases.
`promote_typejoin_tuple` returned a type which was more precise than the one which `map` and `collect` actually use via `promote_type`, triggering an assertion error in corner cases.
promote_typejoin_tuple
returned a type which was more precise than the one whichmap
andcollect
actually use viapromote_type
, triggering an assertion error in corner cases.This is an attempt at fixing #43112.