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 handling of allow_bottom_tparams in isambiguous #20982

Merged
merged 1 commit into from
Mar 12, 2017

Conversation

timholy
Copy link
Member

@timholy timholy commented Mar 11, 2017

With master, we have a lot of ambiguities, but a subset of these involve method signatures that are ambiguous only because of the presence of Union{} as a type-parameter in the intersection:

julia> foo(x::Complex{<:Integer}) = 1
foo (generic function with 1 method)

julia> foo(x::Complex{<:Rational}) = 2
foo (generic function with 2 methods)

julia> m1, m2 = collect(methods(foo))
2-element Array{Any,1}:
 foo(x::Complex{#s1} where #s1<:Integer) in Main at REPL[3]:1 
 foo(x::Complex{#s1} where #s1<:Rational) in Main at REPL[4]:1

julia> typeintersect(m1.sig, m2.sig)
Tuple{#foo,Complex{Union{}}}

julia> Base.isambiguous(m1, m2)
true

This isn't the kind of intersection we should usually be worried about, so in e101000 a strategy was developed to exclude these kinds of intersections. However, I found that it doesn't correctly catch all cases, including the one above:

julia> Base.isambiguous(m1, m2, false)
true

With this PR, I believe I've fixed the detection of Union{}. It has a pretty dramatic effect on our ambiguity list.

Master:

julia> length(Base.Test.detect_ambiguities(Base,Core,allow_bottom=true))
Skipping Base.<|
Skipping Base.Parallel
194

julia> length(Base.Test.detect_ambiguities(Base,Core,allow_bottom=false))
Skipping Base.<|
Skipping Base.Parallel
111

This PR:

julia> length(Base.Test.detect_ambiguities(Base,Core,allow_bottom=true))
Skipping Base.<|
Skipping Base.Parallel
194

julia> length(Base.Test.detect_ambiguities(Base,Core,allow_bottom=false))
Skipping Base.<|
Skipping Base.Parallel
11

11 seems rather more manageable (the "missing" digit is not a copy/paste error). Suddenly the steps that @Sacha0 has been taking (#20970, #20971) don't seem so small anymore: those should fix 3 of the 11. Of the rest, it's just 4 functions: cat (4 ambiguous pairs), checkbounds_indices (2 ambiguous pairs), colon (1 ambiguous pair), and a constructor method for Tuple (1 ambiguous pair).

I do find myself wishing we could change the keyword argument to consistently have the same name, and I find accept_bottom_tparams clearer than allow_bottom_tparams (and perhaps flipping the sense, exclude_bottom_tparams, to be the clearest of all). But at this point, I don't think we can do that for 0.6. I did add some docstrings.

Fixes #20246

@timholy timholy added the types and dispatch Types, subtyping and method dispatch label Mar 11, 2017
@timholy timholy added this to the 0.6.0 milestone Mar 11, 2017

For parametric types, `allow_bottom_tparams` controls whether
`Union{}` is considered a valid intersection of type parameters. For example:
```julia
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this could be a doctest with minor changes

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insofar as I am qualified to review (not far), this looks great!

@Sacha0
Copy link
Member

Sacha0 commented Mar 11, 2017

(#20970, #20971, #20986, #20988, #20989, and #20990 should together resolve all relevant ambiguities. Best!)

The former version failed to detect that certain type parameters were Bottom (for example, in the case in the added docstring for `isambiguous`).
@timholy timholy force-pushed the teh/ambiguities_bottom branch from 4582705 to ec4444a Compare March 12, 2017 11:48
@timholy timholy merged commit 0615ea2 into master Mar 12, 2017
@timholy timholy deleted the teh/ambiguities_bottom branch March 12, 2017 13:00
@timholy
Copy link
Member Author

timholy commented Mar 12, 2017

@Sacha0, when your pile of PRs merges, please do us the honor of turning that test back on. Thanks for jump-starting the effort to resolve these ambiguities.

@Sacha0
Copy link
Member

Sacha0 commented Mar 13, 2017

exclude_bottom_tparams, to be the clearest of all

+1

@@ -941,13 +941,41 @@ function method_exists(f::ANY, t::ANY)
typemax(UInt)) != 0
end

"""
isambiguous(m1, m2, [allow_bottom_tparams=true]) -> Bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated aside, but I think we should gradually aim to get away from using square brackets to denote optional arguments and uniformly use the julia syntax convention for them in docstrings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False ambiguity with Union
3 participants