-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Remove string content from panics #153677
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -81,25 +81,21 @@ const fn slice_error_fail_ct(_: &str, _: usize, _: usize) -> ! { | |||||
|
|
||||||
| #[track_caller] | ||||||
| fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! { | ||||||
| const MAX_DISPLAY_LENGTH: usize = 256; | ||||||
| let trunc_len = s.floor_char_boundary(MAX_DISPLAY_LENGTH); | ||||||
| let s_trunc = &s[..trunc_len]; | ||||||
| let ellipsis = if trunc_len < s.len() { "[...]" } else { "" }; | ||||||
| let len = s.len(); | ||||||
|
|
||||||
| // 1. begin is OOB. | ||||||
| if begin > len { | ||||||
| panic!("start byte index {begin} is out of bounds of `{s_trunc}`{ellipsis}"); | ||||||
| panic!("start byte index {begin} is out of bounds for string of length {len}"); | ||||||
| } | ||||||
|
|
||||||
| // 2. end is OOB. | ||||||
| if end > len { | ||||||
| panic!("end byte index {end} is out of bounds of `{s_trunc}`{ellipsis}"); | ||||||
| panic!("end byte index {end} is out of bounds for string of length {len}"); | ||||||
| } | ||||||
|
|
||||||
| // 3. range is backwards. | ||||||
| if begin > end { | ||||||
| panic!("begin > end ({begin} > {end}) when slicing `{s_trunc}`{ellipsis}") | ||||||
| panic!("byte range starts at {begin} but ends at {end}"); | ||||||
| } | ||||||
|
|
||||||
| // 4. begin is inside a character. | ||||||
|
|
@@ -109,7 +105,7 @@ fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! { | |||||
| let range = floor..ceil; | ||||||
| let ch = s[floor..ceil].chars().next().unwrap(); | ||||||
| panic!( | ||||||
| "start byte index {begin} is not a char boundary; it is inside {ch:?} (bytes {range:?}) of `{s_trunc}`{ellipsis}" | ||||||
| "start byte index {begin} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)" | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -120,15 +116,15 @@ fn slice_error_fail_rt(s: &str, begin: usize, end: usize) -> ! { | |||||
| let range = floor..ceil; | ||||||
| let ch = s[floor..ceil].chars().next().unwrap(); | ||||||
| panic!( | ||||||
| "end byte index {end} is not a char boundary; it is inside {ch:?} (bytes {range:?}) of `{s_trunc}`{ellipsis}" | ||||||
| "end byte index {end} is not a char boundary; it is inside {ch:?} (bytes {range:?} of string)" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1, 2, 6 describe the type so not redundant right? |
||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| // 6. end is OOB and range is inclusive (end == len). | ||||||
| // This test cannot be combined with 2. above because for cases like | ||||||
| // `"abcαβγ"[4..9]` the error is that 4 is inside 'α', not that 9 is OOB. | ||||||
| debug_assert_eq!(end, len); | ||||||
| panic!("end byte index {end} is out of bounds of `{s_trunc}`{ellipsis}"); | ||||||
| panic!("end byte index {end} is out of bounds for string of length {len}"); | ||||||
| } | ||||||
|
|
||||||
| impl str { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,11 +484,22 @@ unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 { | |
| } | ||
| } | ||
|
|
||
| /// Copied from core::str::raw::slice_error_fail | ||
| #[inline(never)] | ||
| fn slice_error_fail(s: &Wtf8, begin: usize, end: usize) -> ! { | ||
This comment was marked as resolved.
Sorry, something went wrong.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| assert!(begin <= end); | ||
| panic!("index {begin} and/or {end} in `{s:?}` do not lie on character boundary"); | ||
| let len = s.len(); | ||
| if begin > len { | ||
| panic!("start byte index {begin} is out of bounds for string of length {len}"); | ||
| } | ||
| if end > len { | ||
| panic!("end byte index {end} is out of bounds for string of length {len}"); | ||
| } | ||
| if begin > end { | ||
| panic!("byte range starts at {begin} but ends at {end}"); | ||
| } | ||
| if !s.is_code_point_boundary(begin) { | ||
| panic!("byte index {begin} is not a code point boundary"); | ||
| } | ||
| panic!("byte index {end} is not a code point boundary"); | ||
|
Comment on lines
-490
to
+502
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 This looks like a huge improvement to usability. |
||
| } | ||
|
Comment on lines
487
to
503
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
|
||
| /// Iterator for the code points of a WTF-8 string. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
|
|
||
| thread 'main' ($TID) panicked at $DIR/const-eval-select-backtrace-std.rs:6:8: | ||
| start byte index 1 is out of bounds of `` | ||
| start byte index 1 is out of bounds for string of length 0 | ||
| note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"of string" sounds odd to me -- is it needed? (Same for similar messages)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like including the word string because the problem is due to mis-use of a str/String API, which may not otherwise be obvious