rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Macro error: expected Expr

Open colinmarc opened this issue 10 months ago • 11 comments

rust-analyzer version: rust-analyzer 1.84.1 (e71f9a9 2025-01-27)

rustc version: rustc 1.84.1 (e71f9a9a9 2025-01-27)

editor or extension: zed or nvim

Image

I'm getting an LSP error on a macro that compiles fine. The issue is reproducible on commit ba3f8bce2749329d2b02922d7637e8546c5eb81b of https://github.com/colinmarc/magic-mirror.

Here is the macro invocation that rust-analyzer doesn't like:

https://github.com/colinmarc/magic-mirror/blob/ba3f8bce2749329d2b02922d7637e8546c5eb81b/mm-server/src/encoder/h264.rs#L22-L28

And here is where the macro lives:

https://github.com/colinmarc/magic-mirror/blob/ba3f8bce2749329d2b02922d7637e8546c5eb81b/mm-server/src/vulkan/chain.rs#L28

Please ignore how insane the macro is 😓 Building the project is also a bit of a hassle, sorry about that. It requires linux and cmake nasm protoc libxkbcommon-dev. Detailed build instructions here: https://colinmarc.github.io/magic-mirror/setup/server/#building-mmserver-from-source

colinmarc avatar Feb 17 '25 10:02 colinmarc

Minimized reproduction:

macro_rules! a {
    ($t:tt) => {};
}

macro_rules! b {
    ($t:ty) => {
        a!($t);
    };
}

b!(&'static str);

None groups strike again...

ChayimFriedman2 avatar Feb 17 '25 13:02 ChayimFriedman2

Thanks so much for boiling it down, I could not have gotten there.

Does that mean the issue is in __replace_expr? Do you have a suggested workaround for the time being?

colinmarc avatar Feb 17 '25 13:02 colinmarc

You can wrap it in parentheses:

                    Self::new(__replace_expr!(($HeadStruct) Default::default()), $(__replace_expr!(($Struct) Default::default()),)*)

ChayimFriedman2 avatar Feb 17 '25 13:02 ChayimFriedman2

Running into this with specta as well. Same issue - adding parentheses fixes it

merlinaudio avatar Jul 14 '25 09:07 merlinaudio

I ran into this issue today.

~I had "rust-analyzer.rustc.source": "discover" configured in vscode (for working with dylint), and had recently upgraded my rust toolchain.~

~The solution for me was to run rustup component add rustc-dev again.~

UPDATE: scratch that, it didn't work after all.

To add more context to my situation, I started seeing errors like this all over the codebase after the toolchain from 1.86 to 1.89.

Image

I tried 1.87 and 1.88 and the error persists, so it seems like this was some kind of regression introduced in 1.87.

dcastro avatar Aug 29 '25 09:08 dcastro

I am also seeing this after a toolchain bump from 1.86. Eager to find a fix, and this macro is in a dependency.

dam5h avatar Sep 02 '25 17:09 dam5h

@dam5h A toolchain bump caused it, not a change in the code? Do you upgrade your r-a with your toolchain? If you have a reproduction please open a new issue, this could be something else.

ChayimFriedman2 avatar Sep 02 '25 17:09 ChayimFriedman2

Yes, I have the same issue as @dcastro above, the only change was a toolchain bump. I also went from 1.86 -> 1.89.

I nuked my lsp target directory as well, just to start clean but it persisted. I'll try to make a minimal repro.

EDIT: Will try the fix above with a fork of the dependency which has the macro.

dam5h avatar Sep 02 '25 17:09 dam5h

I'm also encountering that issue with rust 1.90, but it was resolved by updating the dependencies (serde or jsonschema were probably the relevant ones, as the type has derive macros from there).

But before I still had the situation where the code was building and running just fine, but this error was shown.

SolidTux avatar Oct 01 '25 20:10 SolidTux

Running into this with specta as well. Same issue - adding parentheses fixes it

also running into this error with specta, but with a different syntax so not sure if it's the same bug:

#[derive(Type)]
pub enum Patch<T> {
    Ignore,
    Clear,
    Set(T),
}

Errors expecting T to be an expression:

Image

jesseb34r avatar Nov 11 '25 17:11 jesseb34r

Also reproducible with bare newtype:

#[derive(specta::Type)]
pub struct Bar(String);

mokurin000 avatar Nov 24 '25 14:11 mokurin000