-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Type inference results in infinite recursion in trait resolution #112991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#![recursion_limit = "10"]
fn main() {
let _x: () = of(); // doesn't work
let _x: () = of::<()>(); // works
}
trait MyTrait {}
impl MyTrait for &'static () {}
impl<T> MyTrait for &'static (T,) where &'static T: MyTrait {}
fn of<T: 'static>() -> T
where
&'static T: MyTrait,
{
todo!()
} Turns out 2 of the |
Removing the |
After playing around with it some more I found out having the impl anywhere in the module tree including inside a function's scope results in the same thing. #![recursion_limit = "6"]
fn main() {
let _x: () = of(); // doesn't work
let _x: () = of::<()>(); // works
}
trait MyTrait {}
impl MyTrait for &'static () {}
mod other {
use super::MyTrait;
fn some_function() {
struct hello_from_inside_other_mod<T>(T);
impl<T> MyTrait for &'_ hello_from_inside_other_mod<T> where for<'a> &'a T: MyTrait {}
}
}
fn of<T: 'static>() -> T
where
&'static T: MyTrait,
{
todo!()
} Which results in the following. error[[E0275]](https://doc.rust-lang.org/nightly/error_codes/E0275.html): overflow evaluating the requirement `for<'a> &'a hello_from_inside_other_mod<_>: MyTrait`
--> src/main.rs:4:18
|
4 | let _x: () = of(); // doesn't work
| ^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "12"]` attribute to your crate (`playground`)
note: required for `&'a hello_from_inside_other_mod<hello_from_inside_other_mod<_>>` to implement `for<'a> MyTrait`
--> src/main.rs:19:17
|
19 | impl<T> MyTrait for &'_ hello_from_inside_other_mod<T> where for<'a> &'a T: MyTrait {}
| ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ------- unsatisfied trait bound introduced here
= note: 4 redundant requirements hidden
= note: required for `&hello_from_inside_other_mod<hello_from_inside_other_mod<hello_from_inside_other_mod<...>>>` to implement `MyTrait`
= note: the full type name has been written to '/playground/target/debug/deps/playground-729223f269b31276.long-type-2301279540383687721.txt'
note: required by a bound in `of`
--> src/main.rs:25:17
|
23 | fn of<T: 'static>() -> T
| -- required by a bound in this function
24 | where
25 | &'static T: MyTrait,
| ^^^^^^^ required by this bound in `of`
For more information about this error, try `rustc --explain E0275`. |
I think I found a similar issue. This fails exactly in the same way as yours: struct Wrapped<T>(T);
trait Trait {
}
impl<T> Trait for ((), T) {
}
impl<T, V> Trait for (Wrapped<T>, Wrapped<V>)
where
(T, V): Trait,
{
}
fn test<T, U>(_x: T, _y: U)
where
(T, U): Trait,
{
}
fn main() {
test::<(), ()>((), ()); // compiles
test((), ()); // does not compile
} Instead of a reference to |
Same issue here pub trait Serialize<As: ?Sized = Self> {}
pub fn empty<T: ?Sized>() -> Vec<u8>
where
T: Serialize,
{
todo!();
}
pub struct Int<A>(pub A);
impl Serialize for Int<[u8; 1]> {}
impl<T, As> Serialize<Int<&As>> for Int<&T>
where
Int<T>: Serialize<Int<As>>,
{}
fn weird_error() {
let _: Vec<u8> = empty();
} results in: Actual error
instead of the expected Expected error
Both on stable and |
I ran into a type inference issue with some code and minimized it to the following.
playground link
The above results in the following (both stable and nightly).
I expected that rustc would have inferred the type to be
()
in the first case. If you comment out the first line ofmain
it works.This may be related to #39959 but I am not sure.
Meta
rustc --version --verbose
(from the playground):The text was updated successfully, but these errors were encountered: