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

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"

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 16 (6 by maintainers)

Most upvoted comments

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

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

@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 https://github.com/rust-lang/rust/pull/84717. But serde_derive should work without that.