intellij-rust: enum_dispatch cannot be properly indexed

Environment

  • IntelliJ Rust plugin version: 0.4.162.4321-212
  • Rust toolchain version: nightly-2022-04-03-aarch64-apple-darwin
  • IDE name and version: Clion 21.2.3
  • Operating system: Darwin m1max.local 21.3.0 Darwin Kernel Version 21.3.0 arm64

Problem description

Steps to reproduce

Use enum_dispatch can’t be properly indexed.

image

If I use simple version enum, it can be indexed.

#[enum_dispatch(DataType)]
enum DataTypeImpl {
    Foo,
    Bar,
}
image

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@vlad20012 I’m the author of enum_dispatch, please let me know if there’s anything I can do to make it more IDE-friendly! In particular, if you have a list of the "imaginable rule"s, I’d be happy to look into following them. I wasn’t aware such a list existed 😅

Couldn’t we also use a function-like macro? Then we wouldn’t need to do the extra complicated stuff with a module.

@vlad20012 I’m the author of enum_dispatch, please let me know if there’s anything I can do to make it more IDE-friendly! In particular, if you have a list of the "imaginable rule"s, I’d be happy to look into following them. I wasn’t aware such a list existed 😅

You could remove the need for the 2-step operation by requiring the trait and enum both be part of a module, and attach the macro to the module. It would be slightly more awkward, though you could do it as an inner attribute at the top of the file and have the enum and trait names as the arguments.

@sundy-li It works in rust-analyzer by chance. And it doesn’t work if you start modifying a enum or a trait under enum_dispath macro. In short, rust-analyzer uses single long-living process that expands macros for all crates. IntelliJ Rust uses up to 4 processes that expands macors in parallel. Hence, enum_dispatch macro for a enum and enum_dispatch macro for a trait may fall into different processes and be expanded without knowing each other.

P.S. Don’t try fixing it from the IDE side. Such stateful macros just should not exist. In some day they will stop working even in rustc.

@antonok-edm, could you please write me in Telegram? You can find the contact in my Github profile.

In short, to become IDE-friendly a macro should become a pure function that depends only on a token stream passed as an argument to the macro.

There are many macros that break this rule (like sqlx), but enum_dispatch is really exceptional because it has a state shared between several macro invocations

enum_dispatch is a rare example of absolutely IDE-unfriendly macros. It breaks every imaginable rule. With current design, enum_dispatch will never be supported.