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

Suggestion for add-assigning chars to u32 could be improved #91063

Open
VoleurVolant opened this issue Nov 20, 2021 · 11 comments
Open

Suggestion for add-assigning chars to u32 could be improved #91063

VoleurVolant opened this issue Nov 20, 2021 · 11 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

@VoleurVolant
Copy link

VoleurVolant commented Nov 20, 2021

Given the following code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=005f59d63a84412a73cfe88146c11f39

fn main() {
 let greeting = "Hello, World!";
 let mut output = 0;
 for c in greeting.chars() {
  output += c;
 }
 println!("The values of the characters in \"{greeting}\" sum to {output}");
}

The current output is:

error[E0277]: cannot add-assign `char` to `{integer}`
 --> src/main.rs:5:10
  |
5 |   output += c;
  |          ^^ no implementation for `{integer} += char`
  |
  = help: the trait `AddAssign<char>` is not implemented for `{integer}`

For more information about this error, try `rustc --explain E0277`.

Ideally the output should look like:

error[E0277]: cannot add-assign `char` to `{integer}`
 --> src/main.rs:5:10
  |
5 |   output += c;
  |          ^^ no implementation for `{integer} += char`
  |
  = help: the trait `AddAssign<char>` is not implemented for `{integer}`. You may have meant to use `as u32` or `to_digit` on the `char`.

For more information about this error, try `rustc --explain E0277`.
@VoleurVolant VoleurVolant 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 Nov 20, 2021
@SamuelBonilla
Copy link

I think that it's not an issue

https://users.rust-lang.org/c/community/8

@inquisitivecrystal inquisitivecrystal added C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Nov 20, 2021
@inquisitivecrystal
Copy link
Contributor

I think that it's not an issue

Could you elaborate on the reason you think so? In general, rust tries to provide helpful diagnostics for errors that someone might reasonably make. It's not that there aren't other solutions, such as asking for help, but simply that it saves time and makes the language more approachable for newcomers.

@workingjubilee
Copy link
Contributor

I agree that the suggestion can be improved.

error[E0277]: cannot add `char` to `{integer}`
 --> src/main.rs:2:15
  |
2 |     let x = 5 + '0';
  |               ^ no implementation for `{integer} + char`
  |
  = help: the trait `Add<char>` is not implemented for `{integer}`
  
error[E0369]: cannot add `u32` to `char`
 --> src/main.rs:2:17
  |
2 |     let x = '5' + 5u32 + '0';
  |             --- ^ ---- u32
  |             |
  |             char

We also don't offer useful advice for the cases of simple LHS or RHS Add either (a type error is good, but we can do better). The diagnostics we provide for those cases, however, will have to be different and more general: it is not clear from context that adding an integer and a char is intended to be an integer or a String, unless it is AddAssigned, at which point we can simply look at the type of the LHS and suggest applicable conversions to that type.

It is particularly worth offering special diagnostics for the cases of u32 with char: Our char type offers an easy conversion to/from u32, and our char is explicitly a USV, backed by a u32 (what other languages call a "rune", which is sometimes a type alias for u32 or i32), so it is easy to assume they wanted that.

People used to some languages may be used to working with an 8-bit char, and might make an incorrect assumption about our char type, but I become less and less confident in that direction as it requires making more and more assumptions.

@estebank
Copy link
Contributor

We already provide suggestions for casting between numeric literals, it could be extended to also account for char:

pub fn check_for_cast(

@cameron1024
Copy link
Contributor

Hi, I'm looking at implementing @estebank 's solution, my current plan is to check for the case of "expected u32 or i32, got char", should I submit a PR for this?

@workingjubilee
Copy link
Contributor

Go for it!

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
…st, r=nagisa

suggest casting between i/u32 and char

As discussed in rust-lang#91063 , this adds a suggestion for converting between i32/u32 <-> char with `as`, and a short explanation for why this is safe
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
…st, r=nagisa

suggest casting between i/u32 and char

As discussed in rust-lang#91063 , this adds a suggestion for converting between i32/u32 <-> char with `as`, and a short explanation for why this is safe
@BGR360
Copy link
Contributor

BGR360 commented Dec 26, 2021

PR merged, issue can be closed?

@workingjubilee
Copy link
Contributor

Hm, should it be? Diagnostic seems unchanged for the example given, even with a type annotation:

error[E0277]: cannot add-assign `char` to `u32`
 --> src/main.rs:5:10
  |
5 |   output += c;
  |          ^^ no implementation for `u32 += char`
  |
  = help: the trait `AddAssign<char>` is not implemented for `u32`

@inquisitivecrystal
Copy link
Contributor

Hm, should it be? Diagnostic seems unchanged for the example given, even with a type annotation:

error[E0277]: cannot add-assign `char` to `u32`
 --> src/main.rs:5:10
  |
5 |   output += c;
  |          ^^ no implementation for `u32 += char`
  |
  = help: the trait `AddAssign<char>` is not implemented for `u32`

I apologize for the early close. It looked like the PR should have taken care of this.

@BGR360
Copy link
Contributor

BGR360 commented Dec 29, 2021

I should have done some amount of due diligence before suggesting we close it, my b

@estebank
Copy link
Contributor

Current output:

error[E0277]: cannot add-assign `char` to `{integer}`
 --> src/main.rs:5:10
  |
5 |   output += c;
  |          ^^ no implementation for `{integer} += char`
  |
  = help: the trait `AddAssign<char>` is not implemented for `{integer}`
  = help: the following other types implement trait `AddAssign<Rhs>`:
            <isize as AddAssign>
            <isize as AddAssign<&isize>>
            <i8 as AddAssign>
            <i8 as AddAssign<&i8>>
            <i16 as AddAssign>
            <i16 as AddAssign<&i16>>
            <i32 as AddAssign>
            <i32 as AddAssign<&i32>>
          and 20 others

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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

No branches or pull requests

7 participants