Skip to content

Give helpful suggestions for code containing {integer} and other non-concrete types #97677

Closed
@sodiboo

Description

@sodiboo

Given the following initial code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=37a16d044452d822d2e20198fe11f2bc

fn add_ten<N>(n: N) -> N {
    n + 10
}

The output of this code is acceptable and helpful.

error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): cannot add `{integer}` to `N`
 --> src/lib.rs:2:7
  |
2 |     n + 10
  |     - ^ -- {integer}
  |     |
  |     N
  |
help: consider restricting type parameter `N`
  |
1 | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
  |             +++++++++++++++++++++++++++++++++++

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

Ideally the compiler would suggest Output = N instead of Output = {integer}, and infer that from the function body, i guess? I don't know, really, and it's not the main point of this issue

The problem is with the code that is suggested, which if copied verbatim, looks like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=304fea0cb18adf21f14c2d39a327b652

fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
    n + 10
}

And for this code, the current output is:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `integer` in this scope
 --> src/lib.rs:1:39
  |
1 | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
  |                                       ^^^^^^^ not found in this scope

error[[E0658]](https://doc.rust-lang.org/stable/error-index.html#E0658): associated const equality is incomplete
 --> src/lib.rs:1:29
  |
1 | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
  |                             ^^^^^^^^^^^^^^^^^^
  |
  = note: [see issue #92827 <https://github.com/rust-lang/rust/issues/92827>](https://github.com/rust-lang/rust/issues/92827) for more information

error: mismatch in bind of associated type, got const
   --> src/lib.rs:1:29
    |
1   | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
    |                             ^^^^^^^^^^^^^^^^^^
    |
note: associated type defined here does not match const

Some errors have detailed explanations: E0425, E0658.
For more information about an error, try `rustc --explain E0425`.

Ideally the output should look like:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `integer` in this scope
 --> src/lib.rs:1:39
  |
1 | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
  |                                       ^^^^^^^ not found in this scope

error[[E0658]](https://doc.rust-lang.org/stable/error-index.html#E0658): associated const equality is incomplete
 --> src/lib.rs:1:29
  |
1 | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
  |                             ^^^^^^^^^^^^^^^^^^
  |
  = note: [see issue #92827 <https://github.com/rust-lang/rust/issues/92827>](https://github.com/rust-lang/rust/issues/92827) for more information

error: mismatch in bind of associated type, got const
   --> src/lib.rs:1:29
    |
1   | fn add_ten<N: std::ops::Add<Output = {integer}>>(n: N) -> N {
    |                             ^^^^^^^^^^^^^^^^^^
    |
note: associated type defined here does not match const

+ help: consider replacing `{integer}` with a concrete integer type, such as `i32`
+    --> src/lib.rs:1:29
+     |
+ 1   | fn add_ten<N: std::ops::Add<Output = i32>>(n: N) -> N {
+     |                                      ^^^
+     |

Some errors have detailed explanations: E0425, E0658.
For more information about an error, try `rustc --explain E0425`.

Ideally, this help message should also appear if a constant {integer} does exist but causes an error.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions