rust-lang / rust-lang/rust

`compare_method_predicate_entailment` is missing implied bounds from higher-ranked GAT

Open
#133,805 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs A-higher-ranked A-implied-bounds A-trait-system C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code (from tests/ui/generic-associated-types/extended/lending_iterator.rs):

pub trait FromLendingIterator<A>: Sized {
    fn from_iter<T: for<'x> LendingIterator<Item<'x> = A>>(iter: T) -> Self;
}

impl<A> FromLendingIterator<A> for Vec<A> {
    fn from_iter<I: for<'x> LendingIterator<Item<'x> = A>>(mut iter: I) -> Self {
        let mut v = vec![];
        while let Some(item) = iter.next() {
            v.push(item);
        }
        v
    }
}

pub trait LendingIterator {
    type Item<'z>
    where
        Self: 'z;
    fn next(&mut self) -> Option<Self::Item<'_>>;

    fn collect<A, B: FromLendingIterator<A>>(self) -> B
    where
        Self: Sized,
        Self: for<'q> LendingIterator<Item<'q> = A>,
    {
        <B as FromLendingIterator<A>>::from_iter(self)
    }
}

I expected to see this happen: Pass (probably?)

Instead, this happened:

error[E0276]: impl has stricter requirements than trait
  --> $DIR/lending_iterator.rs:6:45
   |
LL |     fn from_iter<T: for<'x> LendingIterator<Item<'x> = A>>(iter: T) -> Self;
   |     ------------------------------------------------------------------------ definition of `from_iter` from trait
...
LL |     fn from_iter<I: for<'x> LendingIterator<Item<'x> = A>>(mut iter: I) -> Self {
   |                                             ^^^^^^^^^^^^ impl has extra requirement `I: 'x`

error: `Self` does not live long enough
  --> $DIR/lending_iterator.rs:27:9
   |
LL |         <B as FromLendingIterator<A>>::from_iter(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tests/ui/generic-associated-types/extended/lending_iterator.rs and reproduce the two diagnostics shown in the issue. Investigate the compiler handling behind compare_method_predicate_entailment and higher-ranked GAT bounds; done means the test produces the expected result without the reported E0276 and lifetime errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.