【问题标题】:Binary operation == cannot be applied to type syn::Path二进制操作 == 不能应用于类型 syn::Path
【发布时间】:2020-12-19 23:33:10
【问题描述】:

当我使用my fork of async-trait 作为依赖项时,由于syn::* 类型相等,它无法编译。 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 特征实现在“完整”或“派生”功能门之后,但我仍然不知道。

明确尝试了 1.0.33 版,该版本在 Playground 中运行,在我的 PC 上结果相同。

我已经经历了将 async-trait 拆分并重新组合在一起的障碍,但这超出了我的技能范围。

  • rustc 1.46.0 (04488afe3 2020-08-24)
  • 货物 1.46.0 (149022b1d 2020-07-17)

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

【问题讨论】:

    标签: rust compiler-errors dependencies rust-cargo


    【解决方案1】:

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

    特别是as per syn's documentation of optional features,需要extra-traits功能才能获取PartialEq

    extra-traits — Debug、Eq、PartialEq、Hash impls 适用于所有语法树类型。

    因此你只需要调整你的Cargo.toml

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

    【讨论】:

    • Path 上的 Debug 特征也有这个问题。我完全被难住了,因为查看源代码,似乎 fullderive 足以获得 impl,但它只是没有用。我没有看文档,因为我有点假设full 将包含所有内容,并且它不适用于full,它也不适用于其他组合。感谢您发布此答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2013-11-06
    • 2016-05-15
    相关资源
    最近更新 更多