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

Compiler suggests invalid syntax for derive attribute #118809

Closed
alexxbb opened this issue Dec 10, 2023 · 2 comments · Fixed by #120272
Closed

Compiler suggests invalid syntax for derive attribute #118809

alexxbb opened this issue Dec 10, 2023 · 2 comments · Fixed by #120272
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexxbb
Copy link

alexxbb commented Dec 10, 2023

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)

@alexxbb alexxbb 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 Dec 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 10, 2023
@saethlin saethlin added C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 11, 2023
@long-long-float
Copy link
Contributor

@rustbot claim

@long-long-float
Copy link
Contributor

Short code to reproduce the issue.

use serde::{Deserialize, Deserializer};

#[derive(Debug, Deserialize)]
pub struct Build {
    #[serde(deserialize_with = "as_u64")]
    build: u32,
}

fn as_u64<'de, D>(d: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    u64::deserialize(d)
}

fn main() {}

bors added a commit to rust-lang-ci/rust that referenced this issue Feb 10, 2024
…-in-derive-macro, r=<try>

Suppress suggestions in derive macro

close rust-lang#118809

I suppress warnings inside derive macros.

For example, the compiler emits following error by a program described in rust-lang#118809 (comment) with a suggestion that indicates invalid syntax.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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
  |
3 | #[derive(Debug, Deserialize.try_into().unwrap())]
  |                            ++++++++++++++++++++

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

In this PR, suggestions to cast are suppressed.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `serde_test` (bin "serde_test") due to 2 previous errors
```
@bors bors closed this as completed in e525bc9 Feb 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 11, 2024
Rollup merge of rust-lang#120272 - long-long-float:suppress-suggestions-in-derive-macro, r=oli-obk

Suppress suggestions in derive macro

close rust-lang#118809

I suppress warnings inside derive macros.

For example, the compiler emits following error by a program described in rust-lang#118809 (comment) with a suggestion that indicates invalid syntax.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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
  |
3 | #[derive(Debug, Deserialize.try_into().unwrap())]
  |                            ++++++++++++++++++++

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

In this PR, suggestions to cast are suppressed.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `serde_test` (bin "serde_test") due to 2 previous errors
```
RalfJung pushed a commit to RalfJung/miri that referenced this issue Feb 12, 2024
…ive-macro, r=oli-obk

Suppress suggestions in derive macro

close #118809

I suppress warnings inside derive macros.

For example, the compiler emits following error by a program described in rust-lang/rust#118809 (comment) with a suggestion that indicates invalid syntax.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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
  |
3 | #[derive(Debug, Deserialize.try_into().unwrap())]
  |                            ++++++++++++++++++++

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

In this PR, suggestions to cast are suppressed.

```
error[E0308]: `?` operator has incompatible types
 --> src/main.rs:3:17
  |
3 | #[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)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `serde_test` (bin "serde_test") due to 2 previous errors
```
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 C-bug Category: This is a bug. 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.

4 participants