为什么我的带有依赖类型的 Rust 代码不能编译?

问题描述 投票:0回答:0

为什么我的 Rust 代码不能编译?

pub struct BinaryFormatWithoutFieldUid<'a, T>(pub &'a T);

pub struct BinaryFormatWithoutLength<'a, T>(pub &'a T);

pub trait Serialize {}

impl<'a, T> Serialize for BinaryFormatWithoutFieldUid<'a, T>
    where BinaryFormatWithoutLength<'a, T>: Serialize
{}

impl<'a, T> Serialize for BinaryFormatWithoutFieldUid<'a, Option<T>>
    where BinaryFormatWithoutFieldUid<'a, T>: Serialize
{}

impl<'a, T> Serialize for BinaryFormatWithoutLength<'a, Option<T>>
    where BinaryFormatWithoutLength<'a, T>: Serialize
{}
error[E0275]: overflow evaluating the requirement `BinaryFormatWithoutLength<'_, std::option::Option<_>>: Serialize`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`xrpl_async`)
note: required for `BinaryFormatWithoutLength<'_, std::option::Option<std::option::Option<_>>>` to implement `Serialize`
  --> src/lib.rs:15:13
   |
15 | impl<'a, T> Serialize for BinaryFormatWithoutLength<'a, Option<T>>
   |             ^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: 127 redundant requirements hidden
   = note: required for `BinaryFormatWithoutLength<'_, std::option::Option<std::option::Option<std::option::Option<...>>>>` to implement `Serialize`
   = note: the full type name has been written to '/home/porton/Projects/bugs/rust-overflow/target/debug/deps/xrpl_async-97a5b377c31d9cdb.long-type-8508140246639481262.txt'

For more information about this error, try `rustc --explain E0275`.
error: could not compile `xrpl_async` due to previous error

我写的代码是在类型参数中从

Option<T>
下降到
T
,但是出于我不明白的原因,Rust 开始添加越来越多的
Option<Option<...>>
。为什么?

有趣的是,注释掉最后三个定义中的任何一个都会使文件编译。太离谱了,我在 Rust 中发现了一个错误吗?

rustc 1.67.1 (d5a82bbd2 2023-02-07)

rust compiler-errors traits dependent-type
© www.soinside.com 2019 - 2024. All rights reserved.