Type inference in the presence of recursive impls may result in an error message that mentions seemingly unrelated types #62755
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-trait-system
Area: Trait system
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Here's an example (tested with 1.36.0 and 1.38.0-nightly):
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=820e0d5fd61e7d9275a4378977488d8a
The error message:
I understand that Rust doesn't guarantee that trait resolution is decidable, so overflows are expected in certain cases. Of course, message "type annotations needed" would be much more helpful than "overflow evaluating the requirement", but I guess this is the price we have to pay for 'aggressive' type inference.
What is really confusing is the fact that the compiler reports that it fails to evaluate
Option<Struct2<_>>:Trait
, when the code clearly shows that the original obligation isOption<Struct1<_>>:Trait
(Struct2
is not mentioned anywhere besides the first impl).It seems that obligations are evaluated in the following order:
...
Impls that look like this
impl <T> Trait for Something<Struct<T>> where Something<T>:Trait {..}
are not so rare.For example,
impl<'a, E> Read for &'a PollEvented<E> where E: Evented, &'a E: Read {..}
from https://docs.rs/tokio-reactor/0.1.5/tokio_reactor/struct.PollEvented.html.Here's how this impl may cause confusing error message:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=da7d0c59e214a8536d6872d5069c6500
The error message:
PollEvented
is not mentioned anywhere in the code (but the impl is in scope).Interesting, that if I replace
use tokio;
with an impl for a type that is not in scope:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5cdebfaedd00f3142924dd256ac1116a
The error message is even more confusing:
It seems that there are a number of existing issues that have the same underlying cause (type inference in the presence of recursive impls):
#61800
#60603
#57854
#39959
#49017
#37748
#34137
The text was updated successfully, but these errors were encountered: