为什么我会收到“评估需求溢出"?简单特征实现的错误? [英] Why do I get an "overflow evaluating the requirement" error for a simple trait implementation?

查看:52
本文介绍了为什么我会收到“评估需求溢出"?简单特征实现的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个简单的代码上收到一个错误[E0275]:溢出评估需求,我不知道如何解决它.错误消息建议为特征添加递归限制属性;但是,当我添加该属性时,它会产生一条新的错误消息,并建议我将递归限制加倍,直到它太大以至于 rustc 溢出其堆栈.关于此错误还有很多其他问题,但它们似乎与我的情况不同.

I'm getting an error[E0275]: overflow evaluating the requirement on this simple code, and I don't know how to resolve it. The error message suggests adding a recursion limit attribute to the trait; however, when I add the attribute it produces a new error message and suggests I double the recursion limit until it is so large that rustc overflows its stack. There are many other questions already about this error, but they seem different than my case.

struct Foo<T>(T);

impl<T> From<&'_ Foo<T>> for String
where
    for<'a> &'a T: Into<String>,
{
    fn from(s: &Foo<T>) -> String {
        (&s.0).into()
    }
}

fn main() {
    println!("{}", String::from(&Foo(String::new())));
}

error[E0275]: overflow evaluating the requirement `String: From<&'a Foo<_>>`
  --> src/main.rs:13:20
   |
13 |     println!("{}", String::from(&Foo(String::new())));
   |                    ^^^^^^^^^^^^
   |
   = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`playground`)
   = note: required because of the requirements on the impl of `for<'a> Into<String>` for `&'a Foo<_>`
   = note: required because of the requirements on the impl of `From<&'a Foo<Foo<_>>>` for `String`
   = note: 126 redundant requirements hidden
   = note: required because of the requirements on the impl of `From<&Foo<Foo<Foo<...>>>>` for `String`

推荐答案

这是一个未解决的编译器问题.2016 年首次报告为 #37748:在引用上递归实现的特征溢出可重现的例子:

This is an unresolved compiler issue. First reported in 2016 as #37748: Overflow for trait implemented recursively on references with the reproducible example:

trait Foo {}
impl<'a> Foo for &'a usize {}
impl<'a, T> Foo for &'a Vec<T> where &'a T: Foo {}

fn foo<T>(_: T) where for<'a> &'a T: Foo {}

fn main() {
    foo(0);
}

虽然错误消息的细节随着时间的推移发生了变化,但显然核心问题是相同的:

While the specifics of the error message have changed over time, its evident the core issue is the same:

error[E0275]: overflow evaluating the requirement `&Vec<_>: Foo`
 --> src\main.rs:8:5
  |
5 | fn foo<T>(_: T) where for<'a> &'a T: Foo {}
  |                                      --- required by this bound in `foo`
...
8 |     foo(0);
  |     ^^^
  |
  = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`guessing_game`)
  = note: required because of the requirements on the impl of `Foo` for `&Vec<Vec<_>>`
  = note: 127 redundant requirements hidden
  = note: required because of the requirements on the impl of `for<'a> Foo` for `&'a Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<Vec<_>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`

这种模式出现在其他问题中:#77158 #78982 #65420 #60603 #49017 #48767 #39959(这些可能与一些合并有关).不幸的是,这些都没有提供解决方案或一般解决方法.唯一真正的建议是将通用实现转换为具体实现,这可能也可能不可能:

This pattern is seen in other issues: #77158 #78982 #65420 #60603 #49017 #48767 #39959 (these could probably do with some consolidation). And unfortunately, none of these provide a solution or general workaround. The only real suggestion is to convert the generic implementation into concrete ones, which may or may not be possible:

impl From<&'_ Foo<String>> for String {
    fn from(s: &Foo<String>) -> String {
        (&s.0).into()
    }
}

根据我的天真理解,特征求解器将首先确定要在解析过程中选择的候选实现.即使使用递归定义,它也可以很好地找到具体和通用的候选对象,但如果被递归的类型包括生命周期,则似乎有问题.我不会假装知道哪里出错了,只是生命周期的处理方式不同.你可以在 Rustc 开发指南.

From my naive understanding, the trait solver will first determine candidate implementations to select from during the resolution process. It can find concrete and generic candidates well enough even with recursive definitions, but seems to have trouble if the types being recursed include lifetimes. I won't pretend to know exactly where it goes wrong, just that lifetimes are handled differently. You can read more about the trait solver in the Guide to Rustc Development.

我不清楚 Rust 团队对这个问题的看法.它可以作为 专业化 的边缘案例解决,或者他们可能正在等待chalk 来处理它.但很明显,这不是优先事项.

Its not clear to me the Rust team's perspective on this issue. It may be solved as an edge case for specialization or they may be waiting for chalk to handle it. But its evident this is not a priority.

这篇关于为什么我会收到“评估需求溢出"?简单特征实现的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆