Macro error: expected Expr
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
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
Minimized reproduction:
macro_rules! a {
($t:tt) => {};
}
macro_rules! b {
($t:ty) => {
a!($t);
};
}
b!(&'static str);
None groups strike again...
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?
You can wrap it in parentheses:
Self::new(__replace_expr!(($HeadStruct) Default::default()), $(__replace_expr!(($Struct) Default::default()),)*)
Running into this with specta as well. Same issue - adding parentheses fixes it
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.
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.
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 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.
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.
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.
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:
Also reproducible with bare newtype:
#[derive(specta::Type)]
pub struct Bar(String);