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

Diagnostic says method is available for a type it is not, when using const generics #124946

Closed
j-browne opened this issue May 9, 2024 · 0 comments · Fixed by #125080
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

@j-browne
Copy link
Contributor

j-browne commented May 9, 2024

Code

https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=eefb7934c44344c05b333afcbe6c4739

Current output

Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `build` found for struct `Builder<false, true>` in the current scope
  --> src/main.rs:44:42
   |
9  | struct Builder<const AINIT: bool, const BINIT: bool> {
   | ---------------------------------------------------- method `build` not found for this struct
...
44 |     let item = Builder::new().with_b(()).build();
   |                --------------            ^^^^^ method not found in `Builder<false, true>`
   |                |
   |                method `build` is available on `Builder<false, false>`
   |
   = note: the method was found for
           - `Builder<true, true>`

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

Desired output

Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `build` found for struct `Builder<false, true>` in the current scope
  --> src/main.rs:44:42
   |
9  | struct Builder<const AINIT: bool, const BINIT: bool> {
   | ---------------------------------------------------- method `build` not found for this struct
...
44 |     let item = Builder::new().with_b(()).build();
   |                --------------            ^^^^^ method not found in `Builder<false, true>`
   |                |
   |                method `build` is available on `Builder<true, true>`
   |
   = note: the method was found for
           - `Builder<true, true>`

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

or

Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `build` found for struct `Builder<false, true>` in the current scope
  --> src/main.rs:44:42
   |
9  | struct Builder<const AINIT: bool, const BINIT: bool> {
   | ---------------------------------------------------- method `build` not found for this struct
...
44 |     let item = Builder::new().with_b(()).build();
   |                                          ^^^^^ method not found in `Builder<false, true>`
   |
   = note: the method was found for
           - `Builder<true, true>`

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

Rationale and extra context

The diagnostic says "method build is available on Builder<false, false>", which is not true; it is available on Builder<true, true>, though.

The note does say that it is found for Builder<true, true>, though. This makes me wonder whether the former is even necessary or if it means something else that I do not understand (e.g., is there a difference between "is available on" and "was found for"?).

Other cases

If we call `Builder::new().build();`, we get a correct diagnostic:


   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `build` found for struct `Builder<false, false>` in the current scope
  --> src/main.rs:44:31
   |
9  | struct Builder<const AINIT: bool, const BINIT: bool> {
   | ---------------------------------------------------- method `build` not found for this struct
...
44 |     let item = Builder::new().build();
   |                               ^^^^^ method not found in `Builder<false, false>`
   |
   = note: the method was found for
           - `Builder<true, true>`

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

Rust Version

cargo 1.78.0 (54d8815d0 2024-03-26)
release: 1.78.0
commit-hash: 54d8815d04fa3816edc207bbc4dd36bf18014dbc
commit-date: 2024-03-26
host: x86_64-unknown-linux-gnu
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Arch Linux [64-bit]

Anything else?

No response

@j-browne j-browne 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 May 9, 2024
@fmease fmease added D-papercut Diagnostics: An error or lint that needs small tweaks. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels May 10, 2024
@bors bors closed this as completed in 8c64acd May 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 14, 2024
Rollup merge of rust-lang#125080 - bvanjoi:fix-124946, r=nnethercote

only find segs chain for missing methods when no available candidates

Fixes rust-lang#124946

This PR includes two changes:
- Extracting the lookup for the missing method in chains into a single function.
- Calling this function only when there are no candidates available.
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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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.

2 participants