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

Don't treat tuple struct types as functions in diagnostics #114247

Open
laundmo opened this issue Jul 30, 2023 · 4 comments
Open

Don't treat tuple struct types as functions in diagnostics #114247

laundmo opened this issue Jul 30, 2023 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@laundmo
Copy link

laundmo commented Jul 30, 2023

Code

#[derive(Debug)]
struct A();

fn main(){
    dbg!(A);
}

Current output

error[E0277]: `fn() -> A {A}` doesn't implement `Debug`
 --> src/main.rs:5:5
  |
2 | struct A();
  | -------- consider calling the constructor for `A`
...
5 |     dbg!(A);
  |     ^^^^^^^ `fn() -> A {A}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
  |
  = help: the trait `Debug` is not implemented for fn item `fn() -> A {A}`
  = help: use parentheses to construct this tuple struct: `A()`
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to previous error

Desired output

error[E0423]: expected value, found struct `A`

Rationale and extra context

Since i never defined a function, it seems odd to get errors mentioning the function signature. While the example code is pretty clean, in more complex cases this can be a very confusing message

Other cases

consider the following error a user of the Bevy game engine received and was confused by, where there is no hint of the Person struct needing to be instantiated.

#[derive(Component)]
struct Name(String);

#[derive(Component)]
struct Person();

fn add_people(mut commands: Commands) {
    commands.spawn((Name("Test".to_string()), Person));
}
error[E0277]: the trait bound `(fn() -> Person {Person}, Name): Bundle` is not satisfied
   --> src\main.rs:10:20
    |
10  |     commands.spawn((Person, Name("Test".to_string())));
    |              ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bundle` is not implemented for `(fn() -> Person {Person}, Name)`
    |              |
    |              required by a bound introduced by this call
    |
    = help: the following other types implement trait `Bundle`:
              ()
              (B0, B1)
              (B0, B1, B2)
              (B0, B1, B2, B3)
              (B0, B1, B2, B3, B4)
              (B0, B1, B2, B3, B4, B5)
              (B0, B1, B2, B3, B4, B5, B6)
              (B0, B1, B2, B3, B4, B5, B6, B7)
            and 8 others
note: required by a bound in `bevy::prelude::Commands::<'w, 's>::spawn`

Anything else?

No response

@laundmo laundmo 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 Jul 30, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 30, 2023
@saethlin saethlin added D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 30, 2023
@saethlin
Copy link
Member

saethlin commented Jul 30, 2023

Even with a fair bit of Rust experience I still find ctors surprising. Your desired output is technically wrong, but we should definitely be able to identify the difference between a fn function and a ctor and suggest a call.

@compiler-errors
Copy link
Member

compiler-errors commented Jul 30, 2023

@saethlin the help above does suggest a call, though? edit: unless you're talking about example 2, in which case, that's gonna be very hard, since that requires really rich information about the trait obligation that's failing 😅

@laundmo
Copy link
Author

laundmo commented Jul 30, 2023

while it does provide a help, seeing the function signature is still confusing in both examples

@Centri3
Copy link
Member

Centri3 commented Jul 30, 2023

I believe def_kind with DefKind::Ctor can be used to detect this and show "struct constructor" instead, this is already done in other places I think:

DefKind::Ctor(CtorOf::Struct, _) => "struct constructor".into(),
DefKind::Ctor(CtorOf::Variant, _) => "enum constructor".into(),
_ => "fn item".into(),

Even then not perfect, but I agree that showing the signature should be avoided

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-confusing Diagnostics: Confusing error or lint that should be reworked. 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

5 participants