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

Restrict REPL method completion to ignore strictly less specific ones #43572

Closed
wants to merge 2 commits into from

Conversation

Liozou
Copy link
Member

@Liozou Liozou commented Dec 27, 2021

Spurred by (and solves) "the inevitable edge-case" of #43536.
To copy-paste the issue I described there, we currently have:

julia> foo(x::Int, y) = x+y
foo (generic function with 1 method)

julia> foo(x::Integer, z) = x-z
foo (generic function with 2 methods)

julia> foo(3, #TAB
foo(x::Int64, y) in Main at REPL[4]:1
foo(x::Integer, z) in Main at REPL[5]:1

Both methods are suggested, while only the first one is any relevant since the type of x is known to be Int, which means that in this context, foo(3, a) will always call the first method, independently of the type of a.

This PR fixes that by removing suggestions of methods that are strictly less specific than other ones:

julia> foo(3, #TAB
foo(x::Int64, y) in Main at REPL[50]:1

It also happens when using the ?(x,y)TAB syntax of #38791 since I believe that's desired as well there.

A note on method ambiguities: I chose to keep ambiguous method calls, even though they can only result in error, since I think it can help debug those ambiguity errors. To continue with the same example, in this PR:

julia> foo(x, t::Integer) = t
foo (generic function with 3 methods)

julia> foo(3, 4, #TAB
foo(x, t::Integer) in Main at REPL[52]:1
foo(x::Int64, y) in Main at REPL[50]:1
foo(x::Integer, z) in Main at REPL[51]:1
julia> foo(3, 4)
ERROR: MethodError: foo(::Int64, ::Int64) is ambiguous. Candidates:
  foo(x, t::Integer) in Main at REPL[52]:1
  foo(x::Int64, y) in Main at REPL[50]:1
  foo(x::Integer, z) in Main at REPL[51]:1
Possible fix, define
  foo(::Int64, ::Integer)

@Liozou
Copy link
Member Author

Liozou commented Dec 28, 2021

Actually there is a better reason to TAB-complete potentially ambiguous method calls: the REPL may simply not have access to the tightest argument types. For example:

julia> foo(a::Integer, b) = x;

julia> foo(u, v::Integer) = v;

julia> foo(x::Int, y::Int) = x + y;

julia> foo(Ref{Integer}(4)[], Ref{Integer}(3)[], #TAB
foo(x::Int64, y::Int64) in Main at REPL[3]:1
foo(a::Integer, b) in Main at REPL[1]:1
foo(u, v::Integer) in Main at REPL[2]:1
julia> foo(Ref{Integer}(4)[], Ref{Integer}(3)[])
7

julia> foo(Ref{Integer}(4)[], Ref{Integer}(0x0000ab)[], #TAB
foo(x::Int64, y::Int64) in Main at REPL[3]:1
foo(a::Integer, b) in Main at REPL[1]:1
foo(u, v::Integer) in Main at REPL[2]:1
julia> foo(Ref{Integer}(4)[], Ref{Integer}(0x0000ab)[])
ERROR: MethodError: foo(::Int64, ::UInt32) is ambiguous. Candidates:
  foo(a::Integer, b) in Main at REPL[1]:1
  foo(u, v::Integer) in Main at REPL[2]:1
Possible fix, define
  foo(::Integer, ::Integer)
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

I amended the comment and added tests for this in the latest commit.

@Liozou Liozou added the stdlib:REPL Julia's REPL (Read Eval Print Loop) label Dec 28, 2021
@Liozou
Copy link
Member Author

Liozou commented Jan 4, 2022

Now rebased on top of #43577. CI failure on linux32 is unrelated

vtjnash added a commit that referenced this pull request Jan 7, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 19, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 19, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 19, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 19, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 20, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
vtjnash added a commit that referenced this pull request Jan 24, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by #43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
@Liozou
Copy link
Member Author

Liozou commented Jan 31, 2022

For reference, the issue that appeared in #43536 was:

julia> boo(a::Int; kw1) = a - kw1;

julia> boo(a::Integer; kw2) = a + kw2;

julia> boo(3; kw#TAB
kw1=       kw2=       kwsorter3  kwtest3    kwtest4

The kw2= suggestion should not appear, since only the first method can ever be called.

@Liozou
Copy link
Member Author

Liozou commented Jan 31, 2022

Closing now since #43865 is a strict superset of this PR.

@Liozou Liozou closed this Jan 31, 2022
@Liozou Liozou deleted the replcompletemethods branch January 31, 2022 22:39
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by JuliaLang#43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
- Restrict method completion to ignore strictly less specific ones
- Fix various lookup bugs
- Improve slurping of final expression

Inspired by JuliaLang#43572
Co-authored-by: Lionel Zoubritzky <lionel.zoubritzky@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib:REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant