Skip to content
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

Evaluating trait requirements with higher-rank trait bounds overflows #106512

Open
misha-antonenko opened this issue Jan 5, 2023 · 0 comments
Open
Labels
A-associated-items Area: Associated items (types, constants & functions) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@misha-antonenko
Copy link

I am trying to implement a trait for types T such that &T is convertible into an iterator:

trait Scan {
    fn scan(&self);
}

impl<T> Scan for &T
where
    T: Scan,
{
    fn scan(&self) {
        self.scan();
    }
}

impl<Iterable> Scan for Iterable
where
    for<'a> &'a Iterable: IntoIterator,
    for<'a> <&'a Iterable as IntoIterator>::Item: Scan,
{
    fn scan(&self) {
        for item in self.into_iter() {
            item.scan();
        }
    }
}

struct Foo(i32);

impl Scan for Foo {
    fn scan(&self) {}
}

fn main() {
    let v = vec![Foo(1)];
    Scan::scan(&v);
}

The rationale behind this is to be able to call scan on objects without consuming them.

The compiler fails here, though, on both the stable 1.66.0 and the nightly versions:

error[[E0275]](https://doc.rust-lang.org/stable/error-index.html#E0275): overflow evaluating the requirement `for<'a> <&'a _ as IntoIterator>::Item: Scan`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`playground`)
note: required for `<&'a _ as IntoIterator>::Item` to implement `for<'a> Scan`
  --> src/main.rs:14:16
   |
14 | impl<Iterable> Scan for Iterable
   |                ^^^^     ^^^^^^^^
   = note: 126 redundant requirements hidden
   = note: required for `<&'a &_ as IntoIterator>::Item` to implement `for<'a> Scan`

For more information about this error, try `rustc --explain E0275`.

I am aware of a similar issue #37748.

@misha-antonenko misha-antonenko added the C-bug Category: This is a bug. label Jan 5, 2023
@fmease fmease added A-trait-system Area: Trait system A-associated-items Area: Associated items (types, constants & functions) T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage-legacy labels Jan 26, 2024
@fmease fmease added the A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) label Sep 24, 2024
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants