Skip to content

Adding Copy impl to type causes lifetime errors #24085

Closed
@bkoropoff

Description

@bkoropoff

Code

Playpen link

#[derive(Clone)]
// Uncommenting the next line causes an error
//#[derive(Copy)]
struct Path<'a:'b, 'b> {
    x: &'a i32,
    tail: Option<&'b Path<'a, 'b>>
}

#[allow(dead_code, unconditional_recursion)]
fn foo<'a,'b,F>(p: Path<'a, 'b>, mut f: F)
                where F: for<'c> FnMut(Path<'a, 'c>) {
    foo(p, |x| f(x))
}

Error

<anon>:12:5: 12:8 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
<anon>:12     foo(p, |x| f(x))
              ^~~
<anon>:12:16: 12:20 note: first, the lifetime cannot outlive the lifetime 'c as defined on the block at 12:15...
<anon>:12     foo(p, |x| f(x))
                         ^~~~
<anon>:12:13: 12:14 note: ...so that trait type parameters matches those specified on the impl (expected `core::marker::Copy`, found `core::marker::Copy`)
<anon>:12     foo(p, |x| f(x))
                      ^
<anon>:11:54: 13:2 note: but, the lifetime must be valid for the lifetime 'a as defined on the block at 11:53...
<anon>:11                 where F: for<'c> FnMut(Path<'a, 'c>) {
<anon>:12     foo(p, |x| f(x))
<anon>:13 }
<anon>:12:5: 12:8 note: ...so that trait type parameters matches those specified on the impl (expected `core::ops::FnOnce<(Path<'_, 'c>,)>`, found `for<'c> core::ops::FnOnce<(Path<'a, 'c>,)>`)
<anon>:12     foo(p, |x| f(x))
              ^~~

Analysis

Notably, this does not occur with a trait like:

trait FakeCopy where Self: Clone {}

This smells like a bug to me, unless a language change introduced special treatment of Copy with regard to lifetimes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions