Skip to content

Switch #[derive(PartialEq)] to have separate lhs and rhs lifetimes #83957

Open
@programmerjake

Description

@programmerjake

As far as I can tell the following code shouldn't cause a lifetime error but it does. If Iter::Item is changed to &'a () instead of PhantomData<&'a ()> the error goes away.

use core::marker::PhantomData;

fn g<A: Iterator, B: Iterator>(a: A, b: B) -> A
where
    A::Item: PartialEq<B::Item>,
{
    todo!()
}

pub struct Iter<'a>(&'a ());

impl<'a> Iterator for Iter<'a> {
    type Item = PhantomData<&'a ()>;
    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
    g(Iter(a), Iter(b.as_ref())).0
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0309]: the parameter type `B` may not live long enough
  --> src/lib.rs:20:21
   |
19 | pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
   |              -- help: consider adding an explicit lifetime bound...: `B: 'a +`
20 |     g(Iter(a), Iter(b.as_ref())).0
   |                     ^ ...so that the type `B` is not borrowed for too long

error[E0309]: the parameter type `B` may not live long enough
  --> src/lib.rs:20:23
   |
19 | pub fn f<'a, B: AsRef<()>>(a: &'a (), b: B) -> &'a () {
   |              -- help: consider adding an explicit lifetime bound...: `B: 'a +`
20 |     g(Iter(a), Iter(b.as_ref())).0
   |                       ^^^^^^ ...so that the reference type `&B` does not outlive the data it points at

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0309`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions