Skip to content

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 breaking This change will break code 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 deprecation This change introduces or involves a deprecation and removed breaking This change will break code needs pkgeval Tests for all registered packages should be run with this change labels Jan 11, 2022
@jakobnissen
Copy link
Member

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
Member

Uhoh

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

We should probably just fix haskey for arrays and tuples.

@jakobnissen
Copy link
Member

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
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 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?

@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
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.

@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 <ekrefredrik@gmail.com>
Co-authored-by: Simeon Schaub <schaub@mit.edu>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Co-authored-by: Simeon Schaub <schaub@mit.edu>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
Co-authored-by: Simeon Schaub <schaub@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This change fixes an existing bug deprecation This change introduces or involves a deprecation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants