Skip to content
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

Merged
merged 3 commits into from
Nov 18, 2021
Merged

Fix regression in map and collect #43120

merged 3 commits into from
Nov 18, 2021

Conversation

nalimilan
Copy link
Member

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.

This is an attempt at fixing #43112.

`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)
Copy link
Member Author

@nalimilan nalimilan Nov 17, 2021

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.

Copy link
Member

@vtjnash vtjnash Nov 17, 2021

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

@nalimilan nalimilan linked an issue Nov 17, 2021 that may be closed by this pull request
Copy link
Member

@vtjnash vtjnash left a 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

@nalimilan
Copy link
Member Author

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!

@nalimilan nalimilan added backport 1.7 bugfix This change fixes an existing bug labels Nov 18, 2021
@nalimilan nalimilan merged commit edea1de into master Nov 18, 2021
@nalimilan nalimilan deleted the nl/map branch November 18, 2021 15:04
@nalimilan
Copy link
Member Author

Thanks!

KristofferC pushed a commit that referenced this pull request Nov 19, 2021
`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.

(cherry picked from commit edea1de)
KristofferC pushed a commit that referenced this pull request Nov 26, 2021
`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.

(cherry picked from commit edea1de)
KristofferC pushed a commit that referenced this pull request Nov 26, 2021
`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.

(cherry picked from commit edea1de)
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
`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.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

map throws type assertion error
3 participants