-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
Consider this code:
#![recursion_limit = "256"]
pub trait Visit<T: ?Sized> {
fn visit(&mut self, elem: &T);
}
pub enum Ast {
Null,
App(Box<Ast>,Box<Ast>)
}
impl<T> Visit<Ast> for T
where T: Visit<Box<Ast>> {
fn visit(&mut self, elem: &Ast) {}
}
impl<T,S:?Sized> Visit<Box<S>> for T
where T: Visit<S> {
fn visit(&mut self, elem: &Box<S>) {}
}
pub struct MyVisitor{}
fn test() {
MyVisitor{}.visit(&Ast::Null);
}
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9014c0167fc65f14c8a2668428fc5df4
It gives the error:
error[[E0275]](https://doc.rust-lang.org/stable/error-index.html#E0275): overflow evaluating the requirement `MyVisitor: Visit<_>`
[--> src/lib.rs:25:17
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9014c0167fc65f14c8a2668428fc5df4#) |
25 | MyVisitor{}.visit(&Ast::Null);
| ^^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "512"]` attribute to your crate (`playground`)
= note: required because of the requirements on the impl of `Visit<Box<_>>` for `MyVisitor`
= note: 256 redundant requirements hidden
= note: required because of the requirements on the impl of `Visit<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<Box<_>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>` for `MyVisitor`
For more information about this error, try `rustc --explain E0275`.
error: could not compile `playground` due to previous error
Which does not make sense. Let me explain:
- We do
MyVisitor{}.visit(&Ast::Null)
, which requires a boundMyVisitor: Visit<Ast>
. - This bound is implemented (
impl<T> Visit<Ast> for T
), so another bound to be checked is added, which isMyVisitor: Visit<Box<Ast>>
. - This bound is again implemented (
impl<T,S:?Sized> Visit<Box<S>> for T
), which while resolving (T
isMyVisitor
andS
isAst
) results in another bound to be resolved:MyVisitor: Visit<Ast>
. However, this is our initial bound that we are currently resolving so it should compile fine.
A few things to be noted:
- The code compiles if the function
test
is commented out (only the usage throws the error (the impls are correct)). - The
Box<Box<Box<...
situation should never happen in the Rustc type resolver - there is something wrong happening under the hood.
nazar-pc
Metadata
Metadata
Assignees
Labels
A-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.