-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Poor error message when user forgets derive that has attributes #47608
Comments
Mentioning @keeperofdakeys who implemented attribute support for derives in #37614. Would either of you be interested in following up with an improved error message for this case? |
This error message could be a bit misleading. What if they really wanted a proc macro and forgot to import it? What if derives from multiple crates use the same attribute? What if the derive macro hasn't been imported? It is a nice solution though, and I'd be interested in more views about it. Edit: And I'm happy to implement this if required. Irrespective of this, the generic error message should have something like "did you forget to import a proc macro, or define a #[derive] for this item?" added to it. ping @jseyfried @nrc |
What should the diagnostic text be for the following cases?:
I can see how we can provide good diagnostics for cases 1 and 2, maybe for case 3, but I'm intrigued by what the appropriate text for case 4 might be for newcomers, short of a whitelist of attributes from popular crates :-/ |
Your case 1 is the only one I have seen over and over again in IRC. I wouldn't bother with the other cases. For case 1 I would like a diagnostic like this:
along with a suggested fix that shows adding the (I guess alphanumerically the first, if there is more than one in scope) derive as a new |
There's a much more likely reproducer of case 2 on edition 2018, if you simply forget to #[derive(Serialize)]
#[serde(untagged)]
enum CellIndex {
Auto,
Index(u32),
} gives the same error and doesn't mention the missing derive at all
|
Yeah, this bit me hard yesterday. |
It is especially difficult to figure out what happens when |
The error message now says that the derive macro is unresolved when error[E0658]: The attribute `serde` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
--> src/main.rs:2:3
|
2 | #[serde(untagged)]
| ^^^^^
|
= help: add #![feature(custom_attribute)] to the crate attributes to enable
error: cannot find derive macro `Serialize` in this scope
--> src/main.rs:1:10
|
1 | #[derive(Serialize)]
| ^^^^^^^^^ This was fixed somewhere between stable and beta in one of PRs improving error recovery, #56999 probably. |
Had similar issue with Rust 2018 and this code: #[derive(Serialize)] // where I forgot this line
#[serde(remote = "StatusCode")]
struct StatusCodeDef(#[serde(getter = "StatusCode::as_u16")] u16);
impl From<StatusCodeDef> for StatusCode {
fn from(def: StatusCodeDef) -> Self {
StatusCode::from_u16(def.0).unwrap()
}
} and the error was:
|
For now it only works if Edit: The suggestion will now happen as long as at least |
``` error: cannot find attribute `sede` in this scope --> src/main.rs:18:7 | 18 | #[sede(untagged)] | ^^^^ | help: the derive macros `Serialize` and `Deserialize` accept the similarly named `serde` attribute | 18 | #[serde(untagged)] | ~~~~~ error: cannot find attribute `serde` in this scope --> src/main.rs:12:7 | 12 | #[serde(untagged)] | ^^^^^ | = note: `serde` is in scope, but it is a crate, not an attribute help: `serde` is an attribute that can be used by the derive macros `Serialize` and `Deserialize`, you might be missing a `derive` attribute | 10 | #[derive(Serialize, Deserialize)] | ``` Mitigate rust-lang#47608.
``` error: cannot find attribute `sede` in this scope --> src/main.rs:18:7 | 18 | #[sede(untagged)] | ^^^^ | help: the derive macros `Serialize` and `Deserialize` accept the similarly named `serde` attribute | 18 | #[serde(untagged)] | ~~~~~ error: cannot find attribute `serde` in this scope --> src/main.rs:12:7 | 12 | #[serde(untagged)] | ^^^^^ | = note: `serde` is in scope, but it is a crate, not an attribute help: `serde` is an attribute that can be used by the derive macros `Serialize` and `Deserialize`, you might be missing a `derive` attribute | 10 + #[derive(Serialize, Deserialize)] 11 | struct Foo { | ``` Mitigate rust-lang#47608.
The error and suggestion are super misleading and I have seen this a few times in #serde. It should be possible for the compiler to observe that there are derive macros in scope with
serde
declared as an attribute, and suggest using those.A better message would not have the part about the compiler adding meaning to
#[serde]
in the future and would recommend using#[derive(Serialize)]
or#[derive(Deserialize)]
on the struct containing the attribute.The text was updated successfully, but these errors were encountered: