简体   繁体   English

rustc 可以给我更多关于类型歧义错误的细节吗?

[英]Can rustc give me more detail about type ambiguity errors?

There is this familiar compilation error:有这个熟悉的编译错误:

error[E0283]: type annotations required: cannot resolve `T: Constraint`

...

And then rustc --explain E0283 says: This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation , and gives a helpful example .然后rustc --explain E0283说: This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation ,并给出了一个有用的示例

I know exactly how to fix the error - see eg this answer .我确切地知道如何修复错误 - 参见例如这个答案

I'm wondering, though, if rustc can be encouraged to tell me more.不过,我想知道是否可以鼓励rustc告诉我更多信息。 Specifically, some representation of the space of possible concrete types that the type checker ended up with before bailing.具体来说,类型检查器在保释之前最终得到的可能具体类型的空间的一些表示。

I cannot find any rustc options to provide more information on E0283;我找不到任何rustc选项来提供有关 E0283 的更多信息; however the code for this error includes a comment that might give you a little more insight.但是,此错误的代码包含一个注释,可能会给您更多的洞察力。 Otherwise it seems that the answer to your question is no.否则,您的问题的答案似乎是否定的。 Sorry I am not able to get more help.抱歉,我无法获得更多帮助。

When the type checker can't deduce an unambiguous type, it doesn't necessarily mean that it can't choose from some finite set of known contenders.当类型检查器不能推断出明确的类型时,并不一定意味着它不能从一些有限的已知竞争者集中进行选择。 Here is the example you are referring to from the documentation:这是您从文档中引用的示例

trait Generator {
    fn create() -> u32;
}

struct Impl;

impl Generator for Impl {
    fn create() -> u32 { 1 }
}

struct AnotherImpl;

impl Generator for AnotherImpl {
    fn create() -> u32 { 2 }
}

fn main() {
    let cont: u32 = Generator::create();
    // error, impossible to choose one of Generator trait implementation
    // Should it be Impl or AnotherImpl, maybe something else?
}

If AnotherImpl wasn't there, and Impl was the only implementation of Generator , this still wouldn't work.如果AnotherImpl不存在,并且ImplGenerator唯一实现,这仍然行不通。 If it did, you could later add AnotherImpl — even in another module or crate — and break this code.如果是这样,您可以稍后添加AnotherImpl — 甚至在另一个模块或 crate 中 — 并破坏此代码。 In general, adding a new definition should be non-breaking, and certainly shouldn't be able to break code in another module.一般来说,添加新定义应该不会破坏,当然也不应该破坏另一个模块中的代码。 If the compiler automatically chose the "only" implementation of Generator here, it would violate that.如果编译器在这里自动选择了Generator的“唯一”实现,那就违反了。

Coming back to your original question, the only information that the error message could give you is pretty much what it's already giving you.回到您最初的问题,错误消息可以给您的唯一信息几乎就是它已经给您的信息。 The type checker hasn't looked around for possible implementations, rather it has simply refused to even try to choose a type from the information given.类型检查器并没有四处寻找可能的实现,而是简单地拒绝尝试从给定的信息中选择一种类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM