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

remove tuple indexing by non-integer #43760

Merged
merged 16 commits into from
Feb 11, 2022

Conversation

oscardssmith
Copy link
Member

Currently we don't throw an error for (1,2)[1.0] even though we really should. I'd be open to making this a deprectation warning rather than an error, if people think this might break actual people's code.

@oscardssmith oscardssmith added kind:breaking This change will break code kind:bugfix This change fixes an existing bug needs pkgeval Tests for all registered packages should be run with this change labels Jan 11, 2022
@fredrikekre
Copy link
Member

No reason not to have a deprecation.

@fredrikekre fredrikekre added kind:deprecation This change introduces or involves a deprecation and removed kind:breaking This change will break code needs pkgeval Tests for all registered packages should be run with this change labels Jan 11, 2022
base/tuple.jl Outdated Show resolved Hide resolved
@jakobnissen
Copy link
Contributor

jakobnissen commented Jan 12, 2022

Isn't this behaviour confusing though?

julia> key = 1.0;

julia> haskey((1,), key)
true

julia> (1,)[key]
ERROR

Relevant #42679

@JeffBezanson
Copy link
Sponsor Member

Uhoh

@JeffBezanson JeffBezanson added the status:triage This should be discussed on a triage call label Jan 12, 2022
@JeffBezanson
Copy link
Sponsor Member

We should probably just fix haskey for arrays and tuples.

@jakobnissen
Copy link
Contributor

I don't really have a dog in this fight, but I believe the only real way to fix this is by reverting #42679. The logic behind #42679 is simply flawed - keys(x) does not give you an exhaustive collection of all possible objects that is a valid key of x.

The problem in this PR is not limited to arrays and tuples. As long as #42679 is not reverted, it will appear whenever one of these two situations happen

  • a == b, but only either a or b is a valid key (like this PR), in which case the generic fallback of haskey will be true, but indexing will error
  • a != b, but both a and b are valid indices that refer to the same element. For example:
julia> v = [1]; i = CartesianIndex(1);

julia> haskey(v, i)
false

julia> v[i]
1

And these edge cases are endless - it's very easy to make a new type where one of the two situations above happen.

@JeffBezanson
Copy link
Sponsor Member

From triage: let's run pkgeval and plan to put in a deprecation for this. We should also revert #42679 (it's not in 1.7, so no big deal).

@JeffBezanson JeffBezanson removed the status:triage This should be discussed on a triage call label Jan 20, 2022
@oscardssmith
Copy link
Member Author

@vtjnash anything else to do here (other than run nanosoldier?

base/deprecated.jl Outdated Show resolved Hide resolved
@oscardssmith
Copy link
Member Author

I just remembered that pkgeval isn't needed because I ended up deprecating rather than removing the "feature"

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 3, 2022

new tests are failing:

Test Failed at /buildworker/worker/tester_linux64/build/share/julia/test/tuple.jl:155
  Expression: getindex((), 1.0)
    Expected: BoundsError
      Thrown: ErrorException

once fixed, this LGTM

@oscardssmith
Copy link
Member Author

@JeffBezanson can you look at the build error here? I don't understand why it's not working.

@simeonschaub
Copy link
Member

The @deprecate macro needs at least 2 arguments, the old and the new expression. You might have to manually implement this using depwarn here.

base/deprecated.jl Outdated Show resolved Hide resolved
@fredrikekre
Copy link
Member

You might have to manually implement this using depwarn here.

This will give worse source information though? Why doesn't the macro work?

@oscardssmith
Copy link
Member Author

I haven't managed to figure out a way to combine @deprecate and @eval that works.

@fredrikekre
Copy link
Member

Why do you need to use @eval? This works:

@deprecate getindex(t::Tuple, i::Real) t[convert(Int, i)]

and gives

julia> (1,)[1.]
┌ Warning: `getindex(t::Tuple, i::Real)` is deprecated, use `t[convert(Int, i)]` instead.
│   caller = top-level scope at REPL[2]:1

@oscardssmith
Copy link
Member Author

The problem is that changes the type of error of (1,)[0.]. We want it to be a BoundsError, but with that version, it is an ErrorException

@fredrikekre
Copy link
Member

No?

julia> (1,)[0.]
┌ Warning: `getindex(t::Tuple, i::Real)` is deprecated, use `t[convert(Int, i)]` instead.
│   caller = top-level scope at REPL[2]:1
└ @ Core REPL[2]:1
ERROR: BoundsError: attempt to access Tuple{Int64} at index [0]

@oscardssmith
Copy link
Member Author

huh, not sure what was happening then. Let's retry CI

@fredrikekre
Copy link
Member

Probably the tests run with --depwarn=error (so the deprecation errors with an ErrorException).

@oscardssmith
Copy link
Member Author

oh. That's it. I'll change the test.

@oscardssmith
Copy link
Member Author

Can we merge this now? (Windows CI failure is unrelated, will be fixed by #44097)

test/tuple.jl Outdated
Comment on lines 153 to 157
@test_deprecated getindex((1,), 1.0) === 1
@test_deprecated getindex((1,2), 2.0) === 2
@test_throws Exception getindex((), 1.0)
@test_throws Exception getindex((1,2), 0.0)
@test_throws Exception getindex((1,2), -1.0)
Copy link
Member

Choose a reason for hiding this comment

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

Probably these should be moved to https://github.com/JuliaLang/julia/blob/master/test/deprecation_exec.jl (which doesn't run with --depwarn=error).

@oscardssmith oscardssmith force-pushed the oscardssmith-stricter-tuple=indexing branch from 49057c3 to caeec48 Compare February 10, 2022 18:50
@oscardssmith oscardssmith force-pushed the oscardssmith-stricter-tuple=indexing branch from caeec48 to dfb5393 Compare February 11, 2022 18:29
@JeffBezanson JeffBezanson merged commit 4409a1b into master Feb 11, 2022
@JeffBezanson JeffBezanson deleted the oscardssmith-stricter-tuple=indexing branch February 11, 2022 21:00
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request Feb 17, 2022
Co-authored-by: Fredrik Ekre <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
Co-authored-by: Fredrik Ekre <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
Co-authored-by: Fredrik Ekre <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bugfix This change fixes an existing bug kind:deprecation This change introduces or involves a deprecation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants