Skip to content

Relax str::get_unchecked precondition to permit empty slicing #69385

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

Merged
merged 1 commit into from
Feb 24, 2020
Merged

Relax str::get_unchecked precondition to permit empty slicing #69385

merged 1 commit into from
Feb 24, 2020

Conversation

ridiculousfish
Copy link
Contributor

Prior to this commit, str documented that get_unchecked had
the precondition that "begin must come before end". This would appear
to prohibit empty slices (i.e. begin == end).

In practice, get_unchecked is called often with empty slices. Let's relax
the precondition so as to allow them.

Prior to this commit, `str` documented that `get_unchecked` had
the precondition that "`begin` must come before `end`". This would appear
to prohibit empty slices (i.e. begin == end).

In practice, get_unchecked is called often with empty slices. Let's relax
the precondition so as to allow them.
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 22, 2020
@Mark-Simulacrum
Copy link
Member

Surely get_unchecked is invalid on an empty slice, with any index? (Possibly modulo ZSTs, but even there I'm not sure).

It was always my understanding that get_unchecked is UB exactly when get would return None; but this sort of implies that's not correct?

Could you provide an example where this would be fine?

@ridiculousfish
Copy link
Contributor Author

ridiculousfish commented Feb 23, 2020

An example is:

unsafe { "abc".get_unchecked(0..0) }

I would expect this to work, despite violating the precondition that "the starting index must come before the ending index"

@Centril
Copy link
Contributor

Centril commented Feb 23, 2020

            let ptr = slice.as_ptr().add(self.start);
            let len = self.end - self.start;
            super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))

==>

            let ptr = "abc".as_ptr();
            let len = 0 - 0;
            super::from_utf8_unchecked(slice::from_raw_parts(ptr, len))

==>

            super::from_utf8_unchecked(&[])

==>

            ""

@ridiculousfish
Copy link
Contributor Author

ridiculousfish commented Feb 23, 2020

Yes, that is the current observed behavior. However it is undefined, because it violates the precondition.

My change adjusts the precondition to make this defined. Note it is purely a documentation change.

@nagisa
Copy link
Member

nagisa commented Feb 23, 2020

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Feb 23, 2020

📌 Commit 9e41c4b has been approved by nagisa

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 23, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2020
… r=nagisa

Relax str::get_unchecked precondition to permit empty slicing

Prior to this commit, `str` documented that `get_unchecked` had
the precondition that "`begin` must come before `end`". This would appear
to prohibit empty slices (i.e. begin == end).

In practice, get_unchecked is called often with empty slices. Let's relax
the precondition so as to allow them.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2020
… r=nagisa

Relax str::get_unchecked precondition to permit empty slicing

Prior to this commit, `str` documented that `get_unchecked` had
the precondition that "`begin` must come before `end`". This would appear
to prohibit empty slices (i.e. begin == end).

In practice, get_unchecked is called often with empty slices. Let's relax
the precondition so as to allow them.
bors added a commit that referenced this pull request Feb 24, 2020
Rollup of 5 pull requests

Successful merges:

 - #69372 (Updates links in various Compiler Error Index entries)
 - #69385 (Relax str::get_unchecked precondition to permit empty slicing)
 - #69386 (Fix minor error in `MaybeUninit::get_mut()` doc example)
 - #69394 (Clean up E0367 explanation)
 - #69405 (docs: Stdin::read_line: mention the appending)

Failed merges:

r? @ghost
@bors bors merged commit d73aa67 into rust-lang:master Feb 24, 2020
@Mark-Simulacrum
Copy link
Member

(In hindsight, I missed that this was referring to a range index vs. a usize index).

@ridiculousfish ridiculousfish deleted the relax_get_unchecked branch April 28, 2020 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants