Skip to content

Compiler suggests invalid syntax for derive attribute #118809

Closed
@alexxbb

Description

@alexxbb

Code

#[derive(Debug, Deserialize)]
pub struct Build {
    #[serde(deserialize_with = "as_u64")]
    build: u32,
    product: Product,
    release: String,
    status: String,
    version: String,
}

Current output

error[E0308]: `?` operator has incompatible types
   --> downloader-api\src\lib.rs:140:17
    |
140 | #[derive(Debug, Deserialize)]
    |                 ^^^^^^^^^^^ expected `u32`, found `u64`
    |
    = note: `?` operator cannot convert from `u64` to `u32`
    = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit
    |
140 | #[derive(Debug, Deserialize.try_into().unwrap())]
    |                            ++++++++++++++++++++

Desired output

Not sure what the correct output should be, but the current one suggests using an invalid syntax: `#[derive(Debug, Deserialize.try_into().unwrap())]`
If no useful "help" message can be provided in this context, the compiler should not suggest an invalid syntax in the "help" section.

Rationale and extra context

No response

Other cases

No response

Anything else?

rustc 1.76.0-nightly (9a66e4471 2023-11-19)

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions