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

Use primitive indexing in slice's Index/IndexMut #36454

Merged
merged 1 commit into from
Sep 15, 2016

Conversation

bluss
Copy link
Member

@bluss bluss commented Sep 13, 2016

[T]'s Index implementation is normally not used for indexing, instead
the compiler supplied indexing is used.

Use the compiler supplied version in Index/IndexMut.

This removes an inconsistency:

Compiler supplied bound check failures look like this:

thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'

If you convince Rust to use the Index impl for slices, bounds check
failure looks like this instead:

thread 'main' panicked at 'assertion failed: index < self.len()'

The latter is used if you for example use Index generically:

use std::ops::Index;
fn foo<T: ?Sized>(x: &T) where T: Index<usize> { &x[4]; }

foo(&[1, 2, 3][..])

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Contributor

bors commented Sep 13, 2016

📌 Commit 20b9f1c has been approved by alexcrichton

@sfackler
Copy link
Member

@bors
Copy link
Contributor

bors commented Sep 14, 2016

⌛ Testing commit 20b9f1c with merge 5675d4f...

@bors
Copy link
Contributor

bors commented Sep 14, 2016

💔 Test failed - auto-win-msvc-64-opt-rustbuild

@alexcrichton
Copy link
Member

@bors: retry

On Tue, Sep 13, 2016 at 7:48 PM, bors notifications@github.com wrote:

💔 Test failed - auto-win-msvc-64-opt-rustbuild
https://buildbot.rust-lang.org/builders/auto-win-msvc-64-opt-rustbuild/builds/2492


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#36454 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAD95DJGzlNKtv5nzesvkMdZscfglUIAks5qp2BugaJpZM4J8B2j
.

@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

Thanks @sfackler

@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

So huh, it has using the Index trait for arrays, that's not how I understood it.

@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

Fixed the test by amending the commit.

@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

To explain the mystery, it looks like array indexing uses the Index trait for slice in constexprs, and compiler-supplied indexing otherwise (do you know about this @oli-obk ?)

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Sep 14, 2016

📌 Commit 7e425d9 has been approved by alexcrichton

@oli-obk
Copy link
Contributor

oli-obk commented Sep 14, 2016

The error pattern is still wrong. It should not include "assertion failed".

The constexpr behaviour sounds very odd. Might be related to @eddyb's new const trans?

@alexcrichton
Copy link
Member

@bors: r-

Indeed looks like travis is still failing

@eddyb
Copy link
Member

eddyb commented Sep 14, 2016

Not sure if relevant, but if integer type inference is being relied on, #33903 could be involved.

[T]'s Index implementation is normally not used for indexing, instead
the compiler supplied indexing is used.

Use the compiler supplied version in Index/IndexMut.

This removes an inconsistency:

Compiler supplied bound check failures look like this:

thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'

If you convince Rust to use the Index impl for slices, bounds check
failure looks like this instead:

thread 'main' panicked at 'assertion failed: index < self.len()'

The latter is used if you for example use Index generically::

   use std::ops::Index;
   fn foo<T: ?Sized>(x: &T) where T: Index<usize> { &x[4]; }

   foo(&[1, 2, 3][..])
@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

Oh! Sorry, that was dumb. I'm fixing this up.

@bluss
Copy link
Member Author

bluss commented Sep 14, 2016

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Sep 14, 2016

📌 Commit a4ee9c6 has been approved by alexcrichton

Manishearth added a commit to Manishearth/rust that referenced this pull request Sep 15, 2016
…crichton

Use primitive indexing in slice's Index/IndexMut

[T]'s Index implementation is normally not used for indexing, instead
the compiler supplied indexing is used.

Use the compiler supplied version in Index/IndexMut.

This removes an inconsistency:

Compiler supplied bound check failures look like this:

thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'

If you convince Rust to use the Index impl for slices, bounds check
failure looks like this instead:

thread 'main' panicked at 'assertion failed: index < self.len()'

The latter is used if you for example use Index generically:

```rust
use std::ops::Index;
fn foo<T: ?Sized>(x: &T) where T: Index<usize> { &x[4]; }

foo(&[1, 2, 3][..])
```
bors added a commit that referenced this pull request Sep 15, 2016
Rollup of 9 pull requests

- Successful merges: #36384, #36405, #36425, #36429, #36438, #36454, #36459, #36461, #36463
- Failed merges: #36444
@bors bors merged commit a4ee9c6 into rust-lang:master Sep 15, 2016
@bluss bluss deleted the slice-primitive-index branch September 16, 2016 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants