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

"only u8 can be cast as char, not i32" needs further information #91836

Closed
GKFX opened this issue Dec 12, 2021 · 4 comments · Fixed by #91939
Closed

"only u8 can be cast as char, not i32" needs further information #91836

GKFX opened this issue Dec 12, 2021 · 4 comments · Fixed by #91939
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GKFX
Copy link
Contributor

GKFX commented Dec 12, 2021

Given the following code: link

0x61i32 as char

The current output is:

only `u8` can be cast as `char`, not `i32`

This is not very clear. There are two pieces of background information the user needs here:

  • chars are Unicode scalar values, which are any integer value from 0 to 0xD7FF and 0xE000 to 0x10FFFF. Only u8 can be statically guaranteed to meet this requirement so only u8 can be cast to char.
  • char::from_u32 is probably what the user wants, if they are interested in non-ASCII values.

The user is directed to further information available at rustc --explain E0604 and https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions, neither of which explains either of those two points.

@GKFX GKFX added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2021
@GKFX GKFX changed the title "only u8 can be cast as char, not i32" is confusing "only u8 can be cast as char, not i32" needs further information Dec 12, 2021
@jyn514
Copy link
Member

jyn514 commented Dec 12, 2021

I think a good improvement would be to
a) Add more detail about this to https://github.com/rust-lang/rust/blob/master/compiler/rustc_error_codes/src/error_codes/E0604.md
b) Add a help to this error that suggests from_u32

@GKFX are you interested in working on one or both of those? :)

@jyn514 jyn514 added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Dec 12, 2021
@GKFX
Copy link
Contributor Author

GKFX commented Dec 13, 2021

@rustbot claim

@GKFX
Copy link
Contributor Author

GKFX commented Dec 14, 2021

While I was reading the docs I noticed a minor error in char::MAX:

The highest valid code point a char can have.

A [`char`] is a Unicode Scalar Value, which means that it is a Code
Point
, but only ones within a certain range. MAX is the highest valid
code point that's a valid Unicode Scalar Value.

This isn't true; a USV is a Code Point within either of two ranges, not one. It's also a bit cagey about what the ranges are numerically; I don't know if this is intentional. Personally I would re-write it to the below:

The highest valid code point a char can have, 0x10FFFF.

A Code Point is any value between zero and char::MAX. A [`char`] is a
Unicode Scalar Value, which is any Code Point that is not in the range
0xD800..=0xDFFF.

Would I put that in the same PR (as they're both char documentation) or a new issue?

@camelid
Copy link
Member

camelid commented Dec 14, 2021

@GKFX A new PR would be better.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 31, 2022
Clarify documentation on char::MAX

As mentioned in rust-lang#91836 (comment), the documentation on `char::MAX` is not quite correct – USVs are not "only ones within a certain range", they are code points _outside_ a certain range. I have corrected this and given the actual numbers as there is no reason to hide them.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 5, 2022
Clarify error on casting larger integers to char

Closes rust-lang#91836 with changes to E0604.md and a `span_help`.
@bors bors closed this as completed in 58bfe72 Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants