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

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

Closed
sodiboo opened this issue Jun 3, 2022 · 1 comment · Fixed by #97778
Closed
Assignees
Labels
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.

Comments

@sodiboo
Copy link

sodiboo commented Jun 3, 2022

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.

@sodiboo sodiboo 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 Jun 3, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Jun 4, 2022

We could probably suppress this suggestion easily, but that's somewhat sad -- instead, I've been meaning to rework Ty::is_suggestable into something that is more precise and generic, and I would also probably like to rework it to opportunistically replace, e.g. {integer} with i32. Let me see what I can do here.

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 11, 2022
…dy, r=cjgillot

Tidy up miscellaneous bounds suggestions

Just some small fixes to suggestions

- Generalizes `Ty::is_suggestable` into a `TypeVisitor`, so that it can be called on things other than `Ty`
- Makes `impl Trait` in arg position no longer suggestible (generalizing the fix in rust-lang#97640)
- Fixes `impl Trait` not being replaced with fresh type param when it's deeply nested in function signature (fixes rust-lang#97760)
- Fixes some poor handling of `where` clauses with no predicates (also rust-lang#97760)
- Uses `InferCtxt::resolve_numeric_literals_with_default` so we suggest `i32` instead of `{integer}` (fixes rust-lang#97677)

Sorry there aren't many tests the fixes. Most of them would just be duplicates of other tests with empty `where` clauses or `impl Trait` in arg position instead of generic params. Let me know if you'd want more test coverage.
@bors bors closed this as completed in 37a4225 Jun 12, 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 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.

2 participants