Skip to content

1.100 regression: E0277 "is not an iterator" when overriding Iterator::nth/nth_back in an impl bounded on <Self as Deref>::Target (breaks objc2 0.4.1) #166229

Done

Description

@yavor7512

Code

Minimal reproduction with no dependencies; build with rustc --edition 2021 --crate-type lib repro.rs.

// Minimal reproduction distilled from objc2 0.4.1 (src/declare/ivar.rs and
// src/declare/ivar_forwarding_impls.rs). No dependencies.
use std::marker::PhantomData;
use std::ops::Deref;

pub trait InnerIvarType {
    type Output;
}

pub trait IvarType {
    type Type: InnerIvarType;
}

pub struct Ivar<I: IvarType> {
    item: PhantomData<I>,
}

impl<I: IvarType> Deref for Ivar<I> {
    type Target = <I::Type as InnerIvarType>::Output;
    fn deref(&self) -> &Self::Target {
        unimplemented!()
    }
}

impl<I: IvarType> std::ops::DerefMut for Ivar<I> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unimplemented!()
    }
}

impl<I: IvarType> Iterator for Ivar<I>
where
    <Self as Deref>::Target: Iterator,
{
    type Item = <<Self as Deref>::Target as Iterator>::Item;
    fn next(&mut self) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
        (**self).next()
    }
    fn nth(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
        (**self).nth(n)
    }
}

impl<I: IvarType> DoubleEndedIterator for Ivar<I>
where
    <Self as Deref>::Target: DoubleEndedIterator,
{
    fn next_back(&mut self) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
        (**self).next_back()
    }
    fn nth_back(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
        (**self).nth_back(n)
    }
}

I expected to see this happen

It compiles, as it does on 1.97.0, 1.98.0 and 1.99.0.

Instead, this happened

On 1.100.0:

error[E0277]: `<<I as IvarType>::Type as InnerIvarType>::Output` is not an iterator
  --> /tmp/repro.rs:39:5
   |
39 |     fn nth(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `<<I as IvarType>::Type as InnerIvarType>::Output` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `<<I as IvarType>::Type as InnerIvarType>::Output`
note: required for `Ivar<I>` to implement `Iterator`
  --> /tmp/repro.rs:31:19
   |
31 | impl<I: IvarType> Iterator for Ivar<I>
   |                   ^^^^^^^^     ^^^^^^^
32 | where
33 |     <Self as Deref>::Target: Iterator,
   |                              -------- unsatisfied trait bound introduced here
help: consider further restricting the associated type
   |
39 |     fn nth(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> where <<I as IvarType>::Type as InnerIvarType>::Output: Iterator {
   |                                                                                        ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

error[E0277]: `<<I as IvarType>::Type as InnerIvarType>::Output` is not an iterator
  --> /tmp/repro.rs:51:5
   |
51 |     fn nth_back(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `<<I as IvarType>::Type as InnerIvarType>::Output` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `<<I as IvarType>::Type as InnerIvarType>::Output`
note: required for `Ivar<I>` to implement `Iterator`
  --> /tmp/repro.rs:31:19
   |
31 | impl<I: IvarType> Iterator for Ivar<I>
   |                   ^^^^^^^^     ^^^^^^^
32 | where
33 |     <Self as Deref>::Target: Iterator,
   |                              -------- unsatisfied trait bound introduced here
help: consider further restricting the associated type
   |
51 |     fn nth_back(&mut self, n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> where <<I as IvarType>::Type as InnerIvarType>::Output: Iterator {
   |                                                                                             ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

error: aborting due to 2 previous errors

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

The error is reported only on the overridden nth and nth_back; next and next_back are accepted.

Version it worked on

It most recently worked on: Rust 1.99.0 (also checked 1.97.0 and 1.98.0).

Version with regression

rustc --version --verbose:

rustc 1.100.0 (0ef1bc386 2026-11-09)
binary: rustc
commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
commit-date: 2026-11-09
host: aarch64-apple-darwin
release: 1.100.0
LLVM version: 23.1.5

Likely cause

Not bisected to a nightly yet. Between 1.99.0 and 1.100.0, #156216 ("implement const Iterator for Range", merged via rollup #162853) removed #[rustc_non_const_trait_method] from Iterator::nth and DoubleEndedIterator::nth_back and gave both a new bound:

fn nth(&mut self, n: usize) -> Option<Self::Item>
where
    Self::Item: [const] Destruct,

That matches the symptom exactly: only the two methods that gained a where-clause fail.

This looks like the same problem as #109632 (2023), where the identical error in objc2 was bisected to #104100, which added a ~const Destruct bound to Iterator::nth. There it was described as a general limitation of the old trait solver, and the code built with the new solver.

It doesn't appear to be const-specific: on 1.97.0, a local trait whose provided nth has a plain where Self::Item: Marker bound (with a blanket impl<T: ?Sized> Marker for T {}) fails the same way with the default solver and compiles with -Znext-solver. The reproduction above also compiles with -Znext-solver on 1.97.0.

Non-const analog (fails on 1.97.0 with the default solver)
use std::marker::PhantomData;
use std::ops::Deref;
pub trait Marker {}
impl<T: ?Sized> Marker for T {}
pub trait MyIter {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;
    fn nth(&mut self, _n: usize) -> Option<Self::Item> where Self::Item: Marker { self.next() }
}
pub trait InnerIvarType { type Output; }
pub trait IvarType { type Type: InnerIvarType; }
pub struct Ivar<I: IvarType> { item: PhantomData<I> }
impl<I: IvarType> Deref for Ivar<I> {
    type Target = <I::Type as InnerIvarType>::Output;
    fn deref(&self) -> &Self::Target { unimplemented!() }
}
impl<I: IvarType> std::ops::DerefMut for Ivar<I> {
    fn deref_mut(&mut self) -> &mut Self::Target { unimplemented!() }
}
impl<I: IvarType> MyIter for Ivar<I>
where
    <Self as Deref>::Target: MyIter,
{
    type Item = <<Self as Deref>::Target as MyIter>::Item;
    fn next(&mut self) -> Option<<<Self as Deref>::Target as MyIter>::Item> { (**self).next() }
    fn nth(&mut self, n: usize) -> Option<<<Self as Deref>::Target as MyIter>::Item> { (**self).nth(n) }
}
error[E0277]: the trait bound `<<I as IvarType>::Type as InnerIvarType>::Output: MyIter` is not satisfied

Impact

objc2 0.4.1 is a dependency of winit 0.29 and glutin 0.31 on Apple targets, so macOS builds of projects still on winit 0.29 (for example anything using eframe 0.27) fail on 1.100.0. objc2 0.5+ no longer contains this code, so no upstream 0.4.x fix exists. Downstream workarounds: remove the nth/nth_back overrides via [patch.crates-io], or build with 1.99.0 or earlier.

@rustbot label +regression-from-stable-to-stable

Activity

  1. added
    C-bug
    regression-untriaged
    on Nov 20, 2026
  2. added
    needs-triage
    I-prioritize
    regression-from-stable-to-stable
    on Nov 20, 2026
  3. theemathas commented on Nov 20, 2026

    @theemathas
    Contributor

    Smaller reproducer (compiles on 1.99, errors on 1.100):

    use std::marker::PhantomData;
    use std::ops::Deref;
    
    pub struct Ivar<I> {
        item: PhantomData<I>,
    }
    
    impl<I> Deref for Ivar<I> {
        type Target = I;
        fn deref(&self) -> &Self::Target {
            unimplemented!()
        }
    }
    
    
    impl<I> Iterator for Ivar<I>
    where
        <Self as Deref>::Target: Iterator,
    {
        type Item = <<Self as Deref>::Target as Iterator>::Item;
        fn next(&mut self) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
            None
        }
        fn nth(&mut self, _n: usize) -> Option<<<Self as Deref>::Target as Iterator>::Item> {
            None
        }
    }
  4. added
    A-trait-system
    T-types
    E-needs-bisection
    and removed
    regression-untriaged
    on Nov 20, 2026
  5. theemathas commented on Nov 20, 2026

    @theemathas
    Contributor

    Bisection results:

    searched nightlies: from nightly-2026-08-15 to nightly-2026-09-26
    regressed nightly: nightly-2026-09-17
    searched commit range: 215a8af...923c95c
    regressed commit: 92801ed

    bisected with cargo-bisect-rustc v0.6.12

    Host triple: aarch64-apple-darwin
    Reproduce with:

    cargo bisect-rustc --start=1.99.0 --end=1.100.0 --regress=error

    It does in fact appear to be regressed by #156216. cc @Randl (author), @clarfonthey (reviewer)

  6. theemathas commented on Nov 20, 2026

    @theemathas
    Contributor

    cc @rust-lang/project-const-traits

  7. theemathas commented on Nov 20, 2026

    @theemathas
    Contributor

    Self-contained reproducer:

    trait MyDestruct {}
    impl<T> MyDestruct for T {}
    
    trait MyDeref {
        type Target;
    }
    
    impl<I> MyDeref for I {
        type Target = Option<I>;
    }
    
    trait MyIterator<T> {
        fn nth() -> T
        where
            // This code compiles if we remove this bound.
            // The corresponding bound in the real `Iterator` trait was added in 1.100.0
            T: MyDestruct;
    }
    
    trait MyIterator2 {
        type Item2;
    }
    
    impl<I> MyIterator<<Option<I> as MyIterator2>::Item2> for I
    where
        <I as MyDeref>::Target: MyIterator2,
    {
        fn nth() -> <Option<I> as MyIterator2>::Item2 {
            unimplemented!()
        }
    }

    Error:

    error[E0277]: the trait bound `Option<I>: MyIterator2` is not satisfied
      --> src/lib.rs:28:5
       |
    28 |     fn nth() -> <Option<I> as MyIterator2>::Item2 {
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyIterator2` is not implemented for `Option<I>`
       |
    help: this trait has no implementations, consider adding one
      --> src/lib.rs:20:1
       |
    20 | trait MyIterator2 {
       | ^^^^^^^^^^^^^^^^^
    
    For more information about this error, try `rustc --explain E0277`.
    

    Next-solver does not fix this.

  8. added
    A-iterators
    and removed
    E-needs-bisection
    on Nov 20, 2026
  9. 8 remaining items

  10. added
    D-confusing
    on Nov 22, 2026
  11. RalfJung commented on Nov 22, 2026

    @RalfJung
    Member

    This looks like the same problem as #109632 (2023), where the identical error in objc2 was bisected to #104100, which added a ~const Destruct bound to Iterator::nth.

    Ouch, and #109684 did not add a test.... :/

  12. lcnr commented on Nov 23, 2026

    @lcnr
    Contributor

    This is due to ParamEnv normalization, fixed by -Zdisable-param-env-normalization-hack https://rust.godbolt.org/z/nvdvo4Yh8 with the new solver enabled. Actually changing ParamEnv normalization to support this does feel somewhat far off, unsure when exactly we'll do that

    https://github.com/rust-lang/trait-system-refactor-initiative/blob/main/notes/stabilization-report/aliases-and-type-relations.md#paramenv-normalization-jank

  13. lcnr commented on Nov 23, 2026

    @lcnr
    Contributor

    From a Types POV this feels like a bug that's quite hard to avoid. At least I can't think of an immediate fix on our side. I would very much revert this change on stable.

    We do have an interesting option to fix this separate from the revert (and requiring a T-types FCP)

    let mut hybrid_clauses = impl_clauses.instantiate_identity(tcx).clauses;
    hybrid_clauses
    .extend(trait_m_clauses.instantiate_own(tcx, trait_to_impl_args).map(|(clause, _)| clause));

    We could normalize the hybrid env in two steps:

    • normalize impl param_env in unnormalized impl param_env
    • use normalized impl param_env + unnormalized trait method param_env to normalize the full env

    That should fix issues like this. It is still rather unprincipled, would be more generally desirable however

  14. removed
    I-prioritize
    I-libs-nominated
    on Nov 23, 2026
  15. clarfonthey commented on Nov 23, 2026

    @clarfonthey
    Contributor

    (Removing prioritize label + libs nomination since we're doing that discussion on the revert PR.)

  16. added a commit that references this issue on Nov 24, 2026
  17. added a commit that references this issue on Nov 24, 2026
  18. added a commit that references this issue on Nov 28, 2026
  19. clarfonthey commented on Nov 28, 2026

    @clarfonthey
    Contributor

    Closing since we released 1.100.1 to fix this. Forgot to when we merged the PR :p

  20. removed
    I-types-nominated
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsA-trait-systemC-bugD-confusingPG-const-traitsS-has-bisectionT-libsT-typesregression-from-stable-to-stable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions