Skip to content

Fix panic message when RangeFrom index is out of bounds - #74510

Merged
bors merged 2 commits into
rust-lang:masterfrom
LukasKalbertodt:fix-range-from-index-panic
Jul 25, 2020
Merged

Fix panic message when RangeFrom index is out of bounds#74510
bors merged 2 commits into
rust-lang:masterfrom
LukasKalbertodt:fix-range-from-index-panic

Conversation

@LukasKalbertodt

@LukasKalbertodtLukasKalbertodt commented Jul 19, 2020

Copy link
Copy Markdown
Contributor

Before, the Range method was called with end = slice.len(). Unfortunately, because Range::index first checks the order of the indices (start has to be smaller than end), an out of bounds index leads to core::slice::slice_index_order_fail being called. This prints the message 'slice index starts at 27 but ends at 10', which is worse than 'index 27 out of range for slice of length 10'. This is not only useful to normal users reading panic messages, but also for people inspecting assembly and being confused by slice_index_order_fail calls.

You can see the produced assembly here and try on Playground here. (By the way. this is only about which panic function is called; I'm pretty sure it does not improve anything about performance).

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @hanna-kruppe

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

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 19, 2020
@hanna-kruppe

Copy link
Copy Markdown
Contributor

So, this is definitely an improvement and I'd be happy to merge it. But I wonder if a different (entirely new) panic message would be better than reusing the one for a single index being OOB? Something like:

slice index starts at 27 but len is 10

@LukasKalbertodt

Copy link
Copy Markdown
ContributorAuthor

That's a good point. I guess I could then also improve the error message for RangeTo. I just saw CI failed, too, so I will adjust this and push shortly.

@LukasKalbertodt

Copy link
Copy Markdown
ContributorAuthor

I thought a bit about good panic messages, but this isn't trivial :/
I think the best would be to have different panic messages for each range type. Something like:

end index of 3..27 is out of range for slice of length 10

That way, users easily see that the panic was caused by a range index. But... (a) that adds a couple of new panic function, and (b) passing two indices (in case of Range) to the panic function bloats the actual function (as it usually requires at least one additional instruction to prepare the argument).

Alternatively, one could add two new panic messages (for start index of range >= len, and for end index of range >= len). But it's not as clear to the user, I think.

Opinions?

@hanna-kruppe

Copy link
Copy Markdown
Contributor

Code size increases aren't great, and the separate "range start > len" and "range end > len" messages would be a considerable improvement over the status quo already, so I think I prefer that.

Before, the `Range` method was called with `end = slice.len()`.
Unfortunately, because `Range::index` first checks the order of the
indices (start has to be smaller than end), an out of bounds index
leads to `core::slice::slice_index_order_fail` being called. This
prints the message 'slice index starts at 27 but ends at 10', which is
worse than 'index 27 out of range for slice of length 10'. This is not
only useful to normal users reading panic messages, but also for people
inspecting assembly and being confused by `slice_index_order_fail`
calls.
@LukasKalbertodt
LukasKalbertodtforce-pushed the fix-range-from-index-panic branch from e6c79c0 to 87aee9fCompareJuly 19, 2020 14:22
@LukasKalbertodt

Copy link
Copy Markdown
ContributorAuthor

Sounds good to me. I just pushed an update. Now we should have:

range start index 27 out of range for slice of length 10

(for RangeFrom)

range end index 27 out of range for slice of length 10

(for Range and RangeTo)

I didn't use your suggestion as these ones are more similar to the existing index message. But if you prefer something else, I can still change of course.

@LukasKalbertodt
LukasKalbertodtforce-pushed the fix-range-from-index-panic branch from 87aee9f to ea74e7dCompareJuly 19, 2020 14:30
@hanna-kruppe

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Jul 19, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit ea74e7d has been approved by hanna-kruppe

@borsbors 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 Jul 19, 2020
Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 19, 2020
…x-panic, r=hanna-kruppe
Fix panic message when `RangeFrom` index is out of bounds
Before, the `Range` method was called with `end = slice.len()`. Unfortunately, because `Range::index` first checks the order of the indices (start has to be smaller than end), an out of bounds index leads to `core::slice::slice_index_order_fail` being called. This prints the message 'slice index starts at 27 but ends at 10', which is worse than 'index 27 out of range for slice of length 10'. This is not only useful to normal users reading panic messages, but also for people inspecting assembly and being confused by `slice_index_order_fail` calls.
You can see the produced assembly [here](https://rust.godbolt.org/z/GzMGWf) and try on Playground [here](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=aada5996b2f3848075a6d02cf4055743). (By the way. this is only about which panic function is called; I'm pretty sure it does not improve anything about performance).
@bors

bors commented Jul 19, 2020

Copy link
Copy Markdown
Collaborator

⌛ Testing commit ea74e7d with merge 4006dc7d272433d55e4fa4c2d8da2b709bd7e2fd...

@bors

bors commented Jul 19, 2020

Copy link
Copy Markdown
Collaborator

💔 Test failed - checks-actions

@borsbors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 19, 2020
@LukasKalbertodt
LukasKalbertodtforce-pushed the fix-range-from-index-panic branch from ea74e7d to 0d64b01CompareJuly 19, 2020 22:40
@LukasKalbertodt

Copy link
Copy Markdown
ContributorAuthor

Fixed and pushed again. And I ran the full x.py test locally now, so I guess this should be fine :P

@paolobarbolinipaolobarbolini mentioned this pull request Jul 23, 2020
@hanna-kruppe

Copy link
Copy Markdown
Contributor

Whoops, missed this! @bors r+

@bors

bors commented Jul 25, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit 0d64b01 has been approved by hanna-kruppe

@borsbors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 25, 2020
@borsbors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jul 25, 2020
@bors

bors commented Jul 25, 2020

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 0d64b01 with merge fe08fb7...

Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 25, 2020
…x-panic, r=hanna-kruppe
Fix panic message when `RangeFrom` index is out of bounds
Before, the `Range` method was called with `end = slice.len()`. Unfortunately, because `Range::index` first checks the order of the indices (start has to be smaller than end), an out of bounds index leads to `core::slice::slice_index_order_fail` being called. This prints the message 'slice index starts at 27 but ends at 10', which is worse than 'index 27 out of range for slice of length 10'. This is not only useful to normal users reading panic messages, but also for people inspecting assembly and being confused by `slice_index_order_fail` calls.
You can see the produced assembly [here](https://rust.godbolt.org/z/GzMGWf) and try on Playground [here](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=aada5996b2f3848075a6d02cf4055743). (By the way. this is only about which panic function is called; I'm pretty sure it does not improve anything about performance).
@bors

bors commented Jul 25, 2020

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions, checks-azure
Approved by: hanna-kruppe
Pushing fe08fb7 to master...

@borsbors added the merged-by-bors This PR was explicitly merged by bors. label Jul 25, 2020
@bors
bors merged commit fe08fb7 into rust-lang:masterJul 25, 2020
@LukasKalbertodt

Copy link
Copy Markdown
ContributorAuthor

@hanna-kruppe No problem, that's still notably faster than my average review delay :P Thanks!

@LukasKalbertodt
LukasKalbertodt deleted the fix-range-from-index-panic branch July 25, 2020 23:19
@cuvipercuviper added this to the 1.47.0 milestone May 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.S-waiting-on-borsStatus: 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.

5 participants

@LukasKalbertodt@rust-highfive@hanna-kruppe@bors@cuviper