二元运算 == 不能应用于类型 syn::Path

Binary operation == cannot be applied to type syn::Path

当我使用my fork of async-trait as a dependency, it fails to compile due to syn::* types equality. All is green in async-trait CI checks时。要重现,请启动一个新的 cargo lib 项目并添加到 Cargo.toml:

[dependencies]
syn = { version = "1.0.39", features = ["full"] }

在lib.rs中:

pub fn cmp(a: syn::Path, b: syn::Path) -> bool {
    a == b
}

在 Rust 1.46.0 上编译导致错误:

error[E0369]: binary operation `==` cannot be applied to type `syn::Path`
 --> src/lib.rs:4:7
  |
4 |     a == b
  |     - ^^ - syn::Path
  |     |
  |     syn::Path

error: aborting due to previous error

syn::Path implements Eq/PartialEq with feature "full" or "derive":

use syn; // 1.0.33

fn cmp(a: syn::Path, b: syn::Path) -> bool {
    a == b
}

我探索了 syn 的 PartialEqEq trait 实现是在“完整”或“派生”功能门之后,但我仍然一无所知。

明确尝试了版本 1.0.33,在 playground 中有效,在我的 PC 上结果相同。

我已经克服了将 async-trait 分开并将其折叠起来的障碍,但这超出了我的技能范围。

cargo tree 在新项目上使用 syn:

tmp v0.1.0 (/home/debian/Documents/Projects/tmp)
└── syn v1.0.39
    ├── proc-macro2 v1.0.19
    │   └── unicode-xid v0.2.1
    ├── quote v1.0.7
    │   └── proc-macro2 v1.0.19 (*)
    └── unicode-xid v0.2.1

虽然 类型 syn::Path 在功能 fullderive 启用时可用,但某些 为该类型实现的特征不是。

特别是 as per syn's documentation of optional features,需要 extra-traits 特征才能得到 PartialEq:

extra-traits — Debug, Eq, PartialEq, Hash impls for all syntax tree types.

因此您只需要将 Cargo.toml 调整为

syn = { version = "1.0.39", features = ["full", "extra-traits"] }