Skip to content

Awkward wording of char slicing error message #38052

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
steveklabnik opened this issue Nov 28, 2016 · 3 comments
Closed

Awkward wording of char slicing error message #38052

steveklabnik opened this issue Nov 28, 2016 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@steveklabnik
Copy link
Member

fn slice_not_on_char_boundaries() {
    let s = "Здравствуйте";
    &s[0..1];
}

gives

thread 'main' panicked at 'index 0 and/or 3 in `Здравствуйте` do not lie on character boundary'

To me, "index do not lie" feels weird, should be "does not lie".

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 28, 2016
@killercup
Copy link
Member

killercup commented Nov 28, 2016

Lol, I initially read this as saying "do not lie [about] character boundary" to the person who wrote s[0..1]!

I'd suggest:

byte index 0 and/or 3 in Здравствуйте is not on a character boundary

Assuming the actual problem is that you can't form a new string slice with invalid/cut off characters:

byte index 0 and/or 3 in Здравствуйте is middle of a character, slice would be invalid UTF-8

@durka
Copy link
Contributor

durka commented Nov 28, 2016

Well, the problem is "and/or": "0 and 3 are" / "0 or 3 is". Can we just check in the function that prints this message, and make the error message less vague? It's on the panic path anyway.

slice is invalid UTF-8 because byte index 3 is in the middle of char д (bytes 2..4) of Здравствуйте

@bluss
Copy link
Member

bluss commented Nov 29, 2016

I made a PR with your strategy @durka. It feels good to be more friendly yet technically specific here (?)

bors added a commit that referenced this issue Jan 3, 2017
Use more specific panic message for &str slicing errors

Separate out of bounds errors from character boundary errors, and print
more details for character boundary errors.

It reports the first error it finds in:

1. begin out of bounds
2. end out of bounds
3. begin <= end violated
3. begin not char boundary
5. end not char boundary.

Example:

    &"abcαβγ"[..4]

    thread 'str::test_slice_fail_boundary_1' panicked at 'byte index 4 is not
    a char boundary; it is inside 'α' (bytes 3..5) of `abcαβγ`'

Fixes #38052
bors added a commit that referenced this issue Jan 3, 2017
Use more specific panic message for &str slicing errors

Separate out of bounds errors from character boundary errors, and print
more details for character boundary errors.

It reports the first error it finds in:

1. begin out of bounds
2. end out of bounds
3. begin <= end violated
3. begin not char boundary
5. end not char boundary.

Example:

    &"abcαβγ"[..4]

    thread 'str::test_slice_fail_boundary_1' panicked at 'byte index 4 is not
    a char boundary; it is inside 'α' (bytes 3..5) of `abcαβγ`'

Fixes #38052
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants