Rust compiler is hitting trait evaluation recursion limit trying to check for trait implementations on types unrelated to my code

huangapple go评论57阅读模式
英文:

Rust compiler is hitting trait evaluation recursion limit trying to check for trait implementations on types unrelated to my code

问题

我已构建了以下示例。 它至少是近似最小的。

尝试编译此代码会产生以下错误消息:

error[E0275]: overflow evaluating the requirement `for<'a> &'a Simd<_, _>: Mul<Simd<_, _>>`
  --> src/main.rs:47:56
   |
47 | impl <T> Mul <<Wrap <T> as ScalarMulCore>::Scalar> for Wrap <T>
   |                                                        ^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`mwe`)
note: required for `&'a Simd<_, _>` to implement `for<'a> Muls<Simd<_, _>>`
  --> src/main.rs:10:16
   |
10 | impl <T, R, O> Muls <R> for T
   |                ^^^^^^^^     ^
11 | where
12 |     T: Mul <R, Output = O>,
   |                ---------- unsatisfied trait bound introduced here
note: required for `Wrap<Simd<_, _>>` to implement `ScalarMulCore`
  --> src/main.rs:29:10
   |
29 | impl <T> ScalarMulCore for Wrap <T>
   |          ^^^^^^^^^^^^^     ^^^^^^^^
...
32 |     for <'a> &'a T: Muls <T, Output = T>
   |                              ---------- unsatisfied trait bound introduced here
   = note: 62 redundant requirements hidden
   = note: required for `Wrap<_>` to implement `ScalarMulCore`

error[E0275]: overflow evaluating the requirement `for<'a> &'a Simd<_, _>: Mul<Simd<_, _>>`
  --> src/main.rs:59:56
   |
59 | impl <T> Mul <<Wrap <T> as ScalarMulCore>::Scalar> for &Wrap <T>
   |                                                        ^^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`mwe`)
note: required for `&'a Simd<_, _>` to implement `for<'a> Muls<Simd<_, _>>`
  --> src/main.rs:10:16
   |
10 | impl <T, R, O> Muls <R> for T
   |                ^^^^^^^^     ^
11 | where
12 |     T: Mul <R, Output = O>,
   |                ---------- unsatisfied trait bound introduced here
note: required for `Wrap<Simd<_, _>>` to implement `ScalarMulCore`
  --> src/main.rs:29:10
   |
29 | impl <T> ScalarMulCore for Wrap <T>
   |          ^^^^^^^^^^^^^     ^^^^^^^^
...
32 |     for <'a> &'a T: Muls <T, Output = T>
   |                              ---------- unsatisfied trait bound introduced here
   = note: 62 redundant requirements hidden
   = note: required for `Wrap<_>` to implement `ScalarMulCore`

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

我使用的是夜间版本。

这个示例与我想要编写的一些真实代码非常接近,功能上与一些一直让我困扰的测试代码完全相同。令人奇怪的是,如果你注释掉第二个Mul的实现,它将无问题地编译。我不能在我的真实代码中这样做。

为什么rustc会尝试查看Wrap <Simd <_, _>>是否实现了ScalarMulCore?我从未要求这种类型。即使我这样做,为什么会导致rustc达到递归限制?此外,为什么rustc仅在我尝试另外实现&Wrap <T>Mul时才这样做?

英文:

I have constructed the following example. It is at least approximately minimal.

use std::ops::Mul;

trait Muls&lt;Rhs&gt;:
    Mul&lt;Rhs, Output = &lt;Self as Muls&lt;Rhs&gt;&gt;::Output&gt;
    + for&lt;&#39;a&gt; Mul&lt;&amp;&#39;a Rhs, Output = &lt;Self as Muls&lt;Rhs&gt;&gt;::Output&gt;
{
    type Output;
}

impl&lt;T, R, O&gt; Muls&lt;R&gt; for T
where
    T: Mul&lt;R, Output = O&gt;,
    T: for&lt;&#39;a&gt; Mul&lt;&amp;&#39;a R, Output = O&gt;,
{
    type Output = O;
}

trait ScalarMulCore {
    type Scalar;

    fn scalar_mul(&amp;self, rhs: &amp;Self::Scalar) -&gt; Self;

    fn scalar_mul_in_place(&amp;mut self, rhs: &amp;Self::Scalar);
}

struct Wrap&lt;T&gt; {
    x: T,
}

impl&lt;T&gt; ScalarMulCore for Wrap&lt;T&gt;
where
    T: Muls&lt;T, Output = T&gt;,
    for&lt;&#39;a&gt; &amp;&#39;a T: Muls&lt;T, Output = T&gt;,
{
    type Scalar = T;

    fn scalar_mul(&amp;self, rhs: &amp;Self::Scalar) -&gt; Self {
        Self { x: (&amp;self.x) * rhs }
    }

    fn scalar_mul_in_place(&amp;mut self, rhs: &amp;Self::Scalar) {
        self.x = (&amp;self.x) * rhs;
    }
}

impl&lt;T&gt; Mul&lt;&lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::Scalar&gt; for Wrap&lt;T&gt;
where
    Wrap&lt;T&gt;: ScalarMulCore,
{
    type Output = Wrap&lt;T&gt;;

    fn mul(mut self, rhs: &lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::Scalar) -&gt; Self::Output {
        &lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::scalar_mul_in_place(&amp;mut self, &amp;rhs);
        self
    }
}

impl&lt;T&gt; Mul&lt;&lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::Scalar&gt; for &amp;Wrap&lt;T&gt;
where
    Wrap&lt;T&gt;: ScalarMulCore,
{
    type Output = Wrap&lt;T&gt;;

    fn mul(self, rhs: &lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::Scalar) -&gt; Self::Output {
        &lt;Wrap&lt;T&gt; as ScalarMulCore&gt;::scalar_mul(self, &amp;rhs)
    }
}

fn main() {
    let a = Wrap::&lt;isize&gt; { x: 2 };
    let b: isize = 3;

    assert_eq!((a * b).x, 6);
}

Trying to compile this produces the following error message:

   Compiling mwe v0.1.0 (/home/zistack/Projects/mwe)
error[E0275]: overflow evaluating the requirement `for&lt;&#39;a&gt; &amp;&#39;a Simd&lt;_, _&gt;: Mul&lt;Simd&lt;_, _&gt;&gt;`
--&gt; src/main.rs:47:56
|
47 | impl &lt;T&gt; Mul &lt;&lt;Wrap &lt;T&gt; as ScalarMulCore&gt;::Scalar&gt; for Wrap &lt;T&gt;
|                                                        ^^^^^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = &quot;256&quot;]` attribute to your crate (`mwe`)
note: required for `&amp;&#39;a Simd&lt;_, _&gt;` to implement `for&lt;&#39;a&gt; Muls&lt;Simd&lt;_, _&gt;&gt;`
--&gt; src/main.rs:10:16
|
10 | impl &lt;T, R, O&gt; Muls &lt;R&gt; for T
|                ^^^^^^^^     ^
11 | where
12 |     T: Mul &lt;R, Output = O&gt;,
|                ---------- unsatisfied trait bound introduced here
note: required for `Wrap&lt;Simd&lt;_, _&gt;&gt;` to implement `ScalarMulCore`
--&gt; src/main.rs:29:10
|
29 | impl &lt;T&gt; ScalarMulCore for Wrap &lt;T&gt;
|          ^^^^^^^^^^^^^     ^^^^^^^^
...
32 |     for &lt;&#39;a&gt; &amp;&#39;a T: Muls &lt;T, Output = T&gt;
|                              ---------- unsatisfied trait bound introduced here
= note: 62 redundant requirements hidden
= note: required for `Wrap&lt;_&gt;` to implement `ScalarMulCore`
error[E0275]: overflow evaluating the requirement `for&lt;&#39;a&gt; &amp;&#39;a Simd&lt;_, _&gt;: Mul&lt;Simd&lt;_, _&gt;&gt;`
--&gt; src/main.rs:59:56
|
59 | impl &lt;T&gt; Mul &lt;&lt;Wrap &lt;T&gt; as ScalarMulCore&gt;::Scalar&gt; for &amp;Wrap &lt;T&gt;
|                                                        ^^^^^^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = &quot;256&quot;]` attribute to your crate (`mwe`)
note: required for `&amp;&#39;a Simd&lt;_, _&gt;` to implement `for&lt;&#39;a&gt; Muls&lt;Simd&lt;_, _&gt;&gt;`
--&gt; src/main.rs:10:16
|
10 | impl &lt;T, R, O&gt; Muls &lt;R&gt; for T
|                ^^^^^^^^     ^
11 | where
12 |     T: Mul &lt;R, Output = O&gt;,
|                ---------- unsatisfied trait bound introduced here
note: required for `Wrap&lt;Simd&lt;_, _&gt;&gt;` to implement `ScalarMulCore`
--&gt; src/main.rs:29:10
|
29 | impl &lt;T&gt; ScalarMulCore for Wrap &lt;T&gt;
|          ^^^^^^^^^^^^^     ^^^^^^^^
...
32 |     for &lt;&#39;a&gt; &amp;&#39;a T: Muls &lt;T, Output = T&gt;
|                              ---------- unsatisfied trait bound introduced here
= note: 62 redundant requirements hidden
= note: required for `Wrap&lt;_&gt;` to implement `ScalarMulCore`
For more information about this error, try `rustc --explain E0275`.
error: could not compile `mwe` due to 2 previous errors

I am using the nightly build.

The this example is pretty close to some real code that I want to write, and functionally identical to some test code that has been giving me some trouble. Strangely enough, if you comment out the second implementation of Mul, it compiles without issue. I cannot get away with doing this in my real code.

Why on earth is rustc trying to see if Wrap &lt;Simd &lt;_, _&gt;&gt; implements ScalarMulCore? I never asked for that type. Even if I did, why would that cause rustc to hit the recursion limit? Also, why is rustc only doing this when I attempt to additionally implement Mul for &amp;Wrap &lt;T&gt;?

答案1

得分: 0

我知道我正在链接到我的自己的评论,但似乎这个行为是 Rust 编译器中的一个长期存在的 bug。可以在此链接找到更多信息:https://github.com/rust-lang/rust/issues/107850。

英文:

I know I'm linking to my own comment here, but it would appear that this behavior is a long-standing bug in the rust compiler.

huangapple
  • 本文由 发表于 2023年2月9日 03:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定