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

Confusing diagnostics on #[derive(X)] when X is imported only as trait #88206

Closed
m-ou-se opened this issue Aug 21, 2021 · 3 comments · Fixed by #88232
Closed

Confusing diagnostics on #[derive(X)] when X is imported only as trait #88206

m-ou-se opened this issue Aug 21, 2021 · 3 comments · Fixed by #88232
Assignees
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@m-ou-se
Copy link
Member

m-ou-se commented Aug 21, 2021

Reported by @Hoverbear here: https://twitter.com/a_hoverbear/status/1428763621572501507

use serde::{Deserialize, Serialize};

#[derive(Serialize)]
struct A;

Output:

error: cannot find derive macro `Serialize` in this scope
 --> src/main.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^

warning: unused import: `serde::Serialize`
 --> src/main.rs:1:5
  |
1 | use serde::Serialize;
  |     ^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

The diagnostics shows that Serialize is not found, and then complains that Serialize was imported and unused. Confusing.

(Only the trait Serialize is imported, not the Serialize derive macro, as the derive feature wasn't enabled on serde.)

@m-ou-se m-ou-se added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Aug 21, 2021
@m-ou-se m-ou-se self-assigned this Aug 21, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 21, 2021

I have a patch that improves things a bit by adding a note to the error:

error: cannot find derive macro `Serialize` in this scope
 --> src/main.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^
  |
note: `Serialize` is imported here, but it is not a derive macro
 --> src/main.rs:1:5
  |
1 | use serde::Serialize;
  |     ^^^^^^^^^^^^^^^^

Will clean it up, add some tests, and send the PR later.

@m-ou-se
Copy link
Member Author

m-ou-se commented Aug 22, 2021

This adds notes to all 'macro not found' diagnostics where the name is in scope in some other namespace, or as another type of macro: #88232

@bors bors closed this as completed in c31e02a Aug 23, 2021
@patientplatypus6
Copy link

I might suggest that you include something along the lines of

"are you sure that you imported the derive package of Derive in Cargo.toml?"

https://serde.rs/derive.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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
2 participants