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

Vector eltype depends on code executed before - only 1.9rc #48961

Closed
aplavin opened this issue Mar 9, 2023 · 14 comments · Fixed by #49014
Closed

Vector eltype depends on code executed before - only 1.9rc #48961

aplavin opened this issue Mar 9, 2023 · 14 comments · Fixed by #49014
Labels
bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version types and dispatch Types, subtyping and method dispatch

Comments

@aplavin
Copy link
Contributor

aplavin commented Mar 9, 2023

# run in a fresh session
@show typeof([1, missing, 2])

prints typeof([1, missing, 2]) = Vector{Union{Missing, Int64}} as expected.
But add a totally irrelevant line, and eltype changes:

# run in a fresh session
[1, missing, nothing, 2]
@show typeof([1, missing, 2])

prints typeof([1, missing, 2]) = Vector{Any}.

Only happens on 1.9rc, not 1.8 nor 1.9 betas.

@DilumAluthge DilumAluthge added the regression Regression in behavior compared to a previous version label Mar 9, 2023
@giordano giordano added this to the 1.9 milestone Mar 9, 2023
@christiangnrd
Copy link
Contributor

25bad181eb184bce7d3a32a18699fe9f9f9a9325 is the first bad commit
commit 25bad181eb184bce7d3a32a18699fe9f9f9a9325
Author: Jeff Bezanson <jeff.bezanson@gmail.com>
Date:   Tue Jan 24 09:05:18 2023 -0500

    fix #47658, state stack overflow on unions containing typevars (#48221)
    
    (cherry picked from commit 596ce6542624e9b8c3782b19936e2226f307e118)

 src/subtype.c   | 34 ++++++++++++++++++++++++++++++++++
 test/subtype.jl | 24 +++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)

@giordano
Copy link
Contributor

CC @JeffBezanson

@N5N3
Copy link
Member

N5N3 commented Mar 11, 2023

Looks like a dispatch failure

julia> @which promote_rule(Union{Missing, Int}, Int)
promote_rule(T::Type{>:Missing}, S::Type)
     @ Base missing.jl:59

julia> [1, missing, nothing, 2]
4-element Vector{Union{Missing, Nothing, Int64}}:
 1
  missing
  nothing
 2

julia> @which promote_rule(Union{Missing, Int}, Int)
promote_rule(T::Type{>:Union{Missing, Nothing}}, S::Type)
     @ Base missing.jl:49

But the subtyping is correct

julia> S = ans.sig
Tuple{typeof(promote_rule), Type{>:Union{Missing, Nothing}}, Type}
julia> T = Tuple{typeof(promote_rule), Type{Union{Missing, Int}}, Type{Int}}
Tuple{typeof(promote_rule), Type{Union{Missing, Int64}}, Type{Int64}}

julia> T <: S
false

@maleadt
Copy link
Member

maleadt commented Mar 14, 2023

Also reproduces on 596ce65, but not on latest master. Bisected the fix:

96acd4d4e90647caab17af0f689723f5a2594cfd is the first good commit
commit 96acd4d4e90647caab17af0f689723f5a2594cfd
Author: N5N3 <2642243996@qq.com>
Date:   Thu Feb 2 11:41:26 2023 +0800

    Subtype: avoid false alarm caused by eager `forall_exists_subtype`. (#48441)

    * Avoid earsing `Runion` within nested `forall_exists_subtype`

    If `Runion.more != 0` we‘d better not erase the local `Runion` as we need it if the subtyping fails after.

    This commit replaces `forall_exists_subtype` with a local version.
    It first tries `forall_exists_subtype` and estimates the "problem scale".
    If subtyping fails and the scale looks small then it switches to the slow path.

    TODO: At present, the "problem scale" only counts the number of checked `Lunion`s.
    But perhaps we need a more accurate result (e.g. sum of `Runion.depth`)

    * Change the reversed subtyping into a local check.

    Make sure we don't forget the bound in `env`.
    (And we can fuse `local_forall_exists_subtype`)

    * Optimization for non-union invariant parameter.

 src/subtype.c   | 119 ++++++++++++++++++++++++++++++++++++++------------------
 test/subtype.jl |  13 +++++--
 2 files changed, 91 insertions(+), 41 deletions(-)
bisect run success

@KristofferC
Copy link
Member

Should add a test for this (and backport it).

@N5N3
Copy link
Member

N5N3 commented Mar 15, 2023

Ok, get the subtyping bug.

S = Type{Union{Missing, Int}}
T = Type{Union{Missing, Nothing, Int}}
S<:T # 1.9rc: true; 1.8.5/master: false; 

I guess the localized reversed subtyping in forall_exists_equal fix the problem. But I haven't verified that.

@N5N3 N5N3 added bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch labels Mar 15, 2023
@KristofferC
Copy link
Member

@N5N3, can you help with the backport here? From what I understand, #48441 and #48603 have to be backported but when I try to do so I get down a rabbit hole of conflicts with earlier changes by you to subtyping.

@N5N3
Copy link
Member

N5N3 commented Mar 15, 2023

I’ll take a look tomorrow.

@vtjnash
Copy link
Member

vtjnash commented Mar 15, 2023

We should perhaps revert Jeff's commit instead so we can get the release out without adding more uncertainty

@KristofferC
Copy link
Member

Yeah, maybe that is a better idea.

@KristofferC
Copy link
Member

I pushed the revert.

@KristofferC KristofferC removed this from the 1.9 milestone Mar 15, 2023
@N5N3
Copy link
Member

N5N3 commented Mar 15, 2023

OK, the example above was caused by a mess on Lunion.
Before #48441, we let the root forall loop handle the >: check in forall_exists_equal.
Even if eq_union failed for the 1st Lunion, it might pass in the 2nd round as the global Lunion has been changed and thus some checks might be skipped.
(The fastpath should not use the same Lunion as the norm path.)

#48441 fixes such risk as forall_exists_equal no more changes global Lunion.
I think we can add the MWE to test and close this issue.

N5N3 added a commit to N5N3/julia that referenced this issue Mar 15, 2023
N5N3 added a commit that referenced this issue Mar 16, 2023
@JeffBezanson
Copy link
Member

I would rather backport the new fix #48441 as well; some people (Catlab) are depending on this being fixed and they will be sad.

@KristofferC
Copy link
Member

Well, then I need help with the backport(s).

oscardssmith pushed a commit to oscardssmith/julia that referenced this issue Mar 20, 2023
Xnartharax pushed a commit to Xnartharax/julia that referenced this issue Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants