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

slice[slice.len()..] doesn't result in an index out of bounds error #32057

Closed
petrochenkov opened this issue Mar 5, 2016 · 5 comments
Closed

Comments

@petrochenkov
Copy link
Contributor

.. ranges are [inclusive, exclusive) and index access is normally bounds checked, e.g. slice[slice.len()+100..] or slice[slice.len()] panics, however slice[slice.len()..]/slice[slice.len()..slice.len()] is an exception, it just returns an empty slice despite the first inclusive index being out of bounds.

I agree this is convenient and I guess this was implemented intentionally, and we can't silently break this now, but it at least needs to be 1) documented as intentional behavior, 2) tested as intentional behavior.
So far I haven't found neither documentation, nor tests, nor design rationale in the range indexing RFCs and their implementations.

@bluss
Copy link
Member

bluss commented Mar 5, 2016

It's definitely intentional that len is a valid slicing index. Our problem is that we use no documentation at all (in rustdoc) for trait implementations. Index for slice and str should have this information.

@petrochenkov
Copy link
Contributor Author

Our problem is that we use no documentation at all (in rustdoc) for trait implementations.

Doc-comments in the source code would be enough for a start, even if they are not displayed by rustdoc.

@bluss
Copy link
Member

bluss commented Mar 5, 2016

start <= end && end <= .len() is pretty ingrained, is drain really the only place that spells it out completely? http://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html#method.drain

We need more people to tell about doc blind spots like this!

steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 8, 2016
…chton

Clarify documentation for string slicing (Index impls)

Clarify documentation for string slicing (Index impls)

- Mention the sugared syntax for the implementations, since it's not
  apparent from the docs that `Index<Range<usize>>` corresponds to
  `&self[a..b]`.
- Be specific in that start <= end and end <= len

This is just one fix in response to rust-lang#32057
steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 8, 2016
…chton

Clarify documentation for string slicing (Index impls)

Clarify documentation for string slicing (Index impls)

- Mention the sugared syntax for the implementations, since it's not
  apparent from the docs that `Index<Range<usize>>` corresponds to
  `&self[a..b]`.
- Be specific in that start <= end and end <= len

This is just one fix in response to rust-lang#32057
@steveklabnik
Copy link
Member

Fixed by #32099

@bluss
Copy link
Member

bluss commented Mar 9, 2016

I need to follow it up with an improvement for slice slicing as well. (Pr was for strings).

bluss added a commit to bluss/rust that referenced this issue Mar 10, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 11, 2016
Clarify doc for slice slicing (Index impls)

Clarify doc for slice slicing (Index impls)

This is a follow up for PR rust-lang#32099 and rust-lang#32057
Manishearth added a commit to Manishearth/rust that referenced this issue Mar 12, 2016
Clarify doc for slice slicing (Index impls)

Clarify doc for slice slicing (Index impls)

This is a follow up for PR rust-lang#32099 and rust-lang#32057
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

No branches or pull requests

3 participants