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

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

Closed
gbutler69 opened this issue May 22, 2021 · 16 comments

Comments

@gbutler69
Copy link

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"
@matklad
Copy link
Member

matklad commented May 22, 2021

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

@gbutler69
Copy link
Author

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

@lnicola
Copy link
Member

lnicola commented May 22, 2021

And which rustc version?

@gbutler69
Copy link
Author

Latest nightly.

@lnicola
Copy link
Member

lnicola commented May 22, 2021

Dupe of #8925, I suppose.

@gbutler69
Copy link
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.

@gbutler69
Copy link
Author

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

@lnicola
Copy link
Member

lnicola commented May 22, 2021

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

@cynecx
Copy link
Contributor

cynecx commented May 25, 2021

@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.

@quantumsheep
Copy link

Same issue :(

@gbutler69
Copy link
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

@branpk
Copy link

branpk commented Jun 1, 2021

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

@sploders101
Copy link

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!

@tizoc
Copy link

tizoc commented Sep 7, 2021

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.

@flodiebold
Copy link
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.

@tizoc
Copy link

tizoc commented Sep 7, 2021

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants