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

call signature with free type variable can corrupt the dispatch cache #49027

Open
KristofferC opened this issue Mar 16, 2023 · 4 comments · Fixed by #49097
Open

call signature with free type variable can corrupt the dispatch cache #49027

KristofferC opened this issue Mar 16, 2023 · 4 comments · Fixed by #49097
Assignees
Labels
domain:types and dispatch Types, subtyping and method dispatch

Comments

@KristofferC
Copy link
Sponsor Member

KristofferC commented Mar 16, 2023

MWE:

using SciMLBase

function check_isdefined(::Type{<:SciMLBase.IntervalNonlinearFunction{iip}}) where {iip}
    @show @isdefined iip
    if @isdefined iip
        @show iip
    end
end

dt2 = IntervalNonlinearFunction{TypeVar(:iip)}

check_isdefined(dt2)

On bad Julia versions this gives

#= /home/kristofferc/julia/bisect.jl:9 =# @isdefined(iip) = true
ERROR: LoadError: UndefVarError: `iip` not defined

On good ones

#= /home/kristofferc/julia/bisect.jl:9 =# @isdefined(iip) = false

Bisected to #46791 (which was backported). cc @aviatesk

Xref: #35661

@KristofferC KristofferC added the kind:regression Regression in behavior compared to a previous version label Mar 16, 2023
@KristofferC KristofferC added this to the 1.9 milestone Mar 16, 2023
@aviatesk aviatesk self-assigned this Mar 21, 2023
aviatesk added a commit that referenced this issue Mar 22, 2023
We use the specialized signature of a method for accurate analysis of
whether a static parameter is constrained or not. However, it turns out
that we can only use it when it doesn't contain any free type variables
(which it sometimes does, e.g., when the inference entry is given a
signature with a free type variable). In such cases, we should use the
method signature, which, by design, never contains any free type
variables.

fix #49027
aviatesk added a commit that referenced this issue Mar 22, 2023
We use the specialized signature of a method for accurate analysis of
whether a static parameter is constrained or not. However, it turns out
that we can only use it when it doesn't contain any free type variables
(which it sometimes does, e.g., when the inference entry is given a
signature with a free type variable). In such cases, we should use the
method signature, which, by design, never contains any free type
variables.

fix #49027
@vtjnash
Copy link
Sponsor Member

vtjnash commented Mar 22, 2023

Note that this construction will partially corrupt the dispatch cache currently (even after #49097), potentially leading to undefined behavior, so users should avoid defining methods that may get called like that for now. We have a few open issues about eventually fixing that, but it has not been a priority yet.

@KristofferC
Copy link
Sponsor Member Author

I am not sure this exact construction is what is used in practice. It was more something like:

Base.show(io::IO, t::Type{A{X, Y}} where {X, Y}
    if @isdefined X
        print(io, "Type{X, ...}")
    else
        @invoke show(io::IO, t::Type)
    end
end

where X was not defined (but the @isdefined branch was taken).

@vtjnash
Copy link
Sponsor Member

vtjnash commented Mar 22, 2023

Yes, that is what is not permitted currently. Since show normally dispatches on kinds, overloading show for a type will introduce undefined behavior into your application.

@aviatesk aviatesk reopened this Mar 23, 2023
@aviatesk aviatesk added domain:types and dispatch Types, subtyping and method dispatch and removed kind:regression Regression in behavior compared to a previous version labels Mar 23, 2023
@aviatesk aviatesk removed this from the 1.9 milestone Mar 23, 2023
@aviatesk
Copy link
Sponsor Member

Removed the 1.9 milestone flag because the workaround is now backported to 1.9.

@aviatesk aviatesk changed the title @isdefined sometimes returns true even though the variable is undefined call signature with free type variable can corrupt the dispatch cache Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants