Skip to content

Macro Error on Serde Serialize/Deserialize: Err value: ParseFloatError { kind: Invalid } rust-analyzer macro-error #8928

Closed

Description

@gbutler69

With the latest Rust Analyzer release in VS Code (latest) I'm receiving the following error when deriving Serialize/Deserialize (Serde) on a simple struct:

proc macro returned error: proc-macro panicked: called Result::unwrap() on an Err value: ParseFloatError { kind: Invalid } rust-analyzer macro-error

I am getting the error with the following simple code:

use std::{fs::File, io, path::Path};

use serde::{Deserialize, Serialize};

fn main() -> Result<(), io::Error> {
    let move_a = Move {
        direction: Direction::North,
        count: 3,
    };

    let file = serialize(&move_a, &Path::new("./tmp.txt"))?;
    let move_b = deserialize(file)?;

    println!("Move A = {:?}", move_a);
    println!("Move B = {:?}", move_b);

    assert_eq!(move_a, move_b);

    Ok(())
}

fn serialize(the_move: &Move, path: &Path) -> Result<File, io::Error> {
    serde_json::to_writer(io::BufWriter::new(File::create(path)?), the_move)?;
    File::open(path)
}

fn deserialize(file: File) -> Result<Move, io::Error> {
    let u = serde_json::from_reader(io::BufReader::new(file))?;
    Ok(u)
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
enum Direction {
    North,
    NorthEast,
    East,
    SouthEast,
    South,
    SouthWest,
    West,
    NorthWest,
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
struct Move {
    pub direction: Direction,
    pub count: u8,
}

This code compile as runs fine from the command line using cargo run.

Also, somewhat counter-intuitively, the "Move" structure is recognized at the usage site as correctly implementing Serialize/Deserialize.

Is this a bug in rust-analyzer (it seems like it) or am I doing something wrong.

Here is my Cargo.toml:

[package]
name = "ex-bb2-serde-json"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Activity

  1. matklad commented on May 22, 2021

    @matklad
    Contributor

    Hm, I cant' reproduce this using the latest version of rust-analyzer:

    image

    Cound you check the version of rust-analyzer you are using? https://rust-analyzer.github.io/manual.html#troubleshooting

  2. gbutler69 commented on May 22, 2021

    @gbutler69
    Author

    The RA version is: v0.2.599. The VSCode version is: 1.56.2.

  3. lnicola commented on May 22, 2021

    @lnicola
    Member

    And which rustc version?

  4. gbutler69 commented on May 22, 2021

    @gbutler69
    Author

    Latest nightly.

  5. lnicola commented on May 22, 2021

    @lnicola
    Member

    Dupe of #8925, I suppose.

  6. gbutler69 commented on May 22, 2021

    @gbutler69
    Author

    NOTE: The error doesn't prevent the proper derivation of Serialize/Deserialize. It just shows the error (in the output tab if you don't have it configured to display errors inline). The error appears on the structure & enum. If I remove the Serialize/Deserialize from both, the error goes away, but then (obviously), Serialize/Deserialize is not implemented.

  7. gbutler69 commented on May 22, 2021

    @gbutler69
    Author

    This does appear to be a nightly related issue. The problem goes away when I switch to stable.

  8. lnicola commented on May 22, 2021

    @lnicola
    Member

    Unfortunately, we currently don't support multiple proc macro ABIs, so we can't fix this until 1.54 comes out.

  9. cynecx commented on May 25, 2021

    @cynecx
    Contributor

    @gbutler69 You can use this patch which applies the upstream abi changes on top of rust-analyzer. Note, that this doesn't implement the functionality of rust-lang/rust#84717. But serde_derive should work without that.

  10. quantumsheep commented on Jun 1, 2021

    @quantumsheep

    Same issue :(

  11. gbutler69 commented on Jun 1, 2021

    @gbutler69
    Author

    Same issue :(

    @quantumsheep Switch to stable for you project (if possible) using: rustup default stable.
    If you need to use nightly, then you'll need to apply the patch and use a patched version of rust-analyzer as mentioned by @cynecx

  12. branpk commented on Jun 1, 2021

    @branpk

    If you need to use nightly but not necessarily the latest nightly, you can also use rustup default nightly-2021-05-20.

  13. sploders101 commented on Aug 6, 2021

    @sploders101

    I just got this issue while using Rust stable on mac (rustc 1.48.0). Read through this issue and thought "I'm not using nightly, but I'll try it anyway". Rust updated to 1.54.0 and the issue is gone, so it's also worthwhile to make sure stable Rust is up to date as well. Hope this helps someone else!

  14. tizoc commented on Sep 7, 2021

    @tizoc

    I am experiencing this issue with nightly-2021-08-04-x86_64-unknown-linux-gnu in VSCode. I fixed it by overriding the rust-analyzer binary that is downloaded by VSCode with the one from the rustup toolchain install which gives me rust-analyzer ea105f9 2021-08-03 dev.

  15. flodiebold commented on Sep 7, 2021

    @flodiebold
    Member

    This was also fixed in #9550, I think.

    @tizoc both of those versions are quite old. I'd instead recommend updating to the current stable version of rust-analyzer.

  16. tizoc commented on Sep 7, 2021

    @tizoc

    @flodiebold the version from VSCode before I overwrote it was rust-analyzer 3dae94bf2 2021-09-07 nightly (after I switched to the "nightly" channel, didn't check before that but since the channel was the "stable" one, I suppose the downloaded binary was the latest "stable" too). But neither worked, I had to overwrite the binary.

    I tried uninstalling and reinstalling the VSCode (from "stable" channel) first, then again but switching to the "nightly" channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions