Skip to content

Auto traits in opaque types impose bounds on wrong lifetime #158777

Open

Description

@theemathas

This code is minimized from code discovered by @danielhenrymantilla.

I tried this code:

struct X<'a>(&'a ());

unsafe impl Send for X<'static> {} // non-`Send` otherwise.

fn require_send(_: impl Send) {}

fn foo1<'a>(_: &'a i32) -> impl use<'a> + Sized {
    X::<'static>(&())
}

fn foo2<'a>(_: &'a i32) -> impl use<'a> + Send {
    X::<'static>(&())
}

fn main() {
    let x = 1;
    require_send(foo1(&x));
    let y = 2;
    require_send(foo2(&y));
}
error[E0597]: `x` does not live long enough
  --> src/main.rs:17:23
   |
16 |     let x = 1;
   |         - binding `x` declared here
17 |     require_send(foo1(&x));
   |     ------------------^^--
   |     |                 |
   |     |                 borrowed value does not live long enough
   |     argument requires that `x` is borrowed for `'static`
...
20 | }
   | - `x` dropped here while still borrowed
   |
note: requirement that the value outlives `'static` introduced here
  --> src/main.rs:5:25
   |
 5 | fn require_send(_: impl Send) {}
   |                         ^^^^

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

I expected both the call to foo1 and foo2 to both compile. Instead, for some reason, only the call to foo1 caused a compile error. This seems incorrect, since auto traits ought to be seen through RPIT.

For some reason, in foo1, instead of the compiler imposing a requirement that the lifetime in X must be 'static, the compiler is instead imposing a requirement that 'a must be 'static.

Neither next-solver nor polonius fixes this.

cc @lcnr

Meta

Reproducible on the playground with version 1.98.0-nightly (2026-07-02 c397dae808f70caebab1)

Activity

  1. added
    A-borrow-checker
    A-impl-trait
    C-bug
    A-auto-traits
    T-types
    on Jul 4, 2026
  2. added
    needs-triage
    on Jul 4, 2026
  3. removed
    needs-triage
    on Jul 4, 2026
  4. zachs18 commented on Jul 5, 2026

    @zachs18
    Contributor

    I believe there is variance shennanigans happening in foo1 to make the actual return type be X<'a>; if you make X invariant in its lifetime parameter then the issue seems not to happen: playground

    struct X<'a>(&'a (), Option<fn(&'a ()) -> &'a ()>);
    
    unsafe impl Send for X<'static> {} // non-`Send` otherwise.
    
    fn require_send(_: impl Send) {}
    
    fn foo1<'a>(_: &'a i32) -> impl use<'a> + Sized {
        X::<'static>(&(), None)
    }
    
    fn foo2<'a>(_: &'a i32) -> impl use<'a> + Send {
        X::<'static>(&(), None)
    }
    
    fn main() {
        let x = 1;
        require_send(foo1(&x));
        let y = 2;
        require_send(foo2(&y));
    }
  5. added
    A-variance
    A-inference
    on Jul 5, 2026
  6. theemathas commented on Jul 5, 2026

    @theemathas
    ContributorAuthor

    Another instance of the issue (which similarly goes away when X is made invariant):

    struct X<'a>(&'a ());
    
    unsafe impl Send for X<'static> {} // non-`Send` otherwise.
    
    fn require_send(_: impl Send) {}
    
    fn foo1<'a, 'b: 'static>(_: &'a i32, x: X<'b>) -> impl use<'a, 'b> + Sized {
        x
    }
    
    fn foo2<'a, 'b: 'static>(_: &'a i32, x: X<'b>) -> impl use<'a, 'b> + Send {
        x
    }
    
    fn main() {
        let x = 1;
        require_send(foo1(&x, X::<'static>(&())));
        let y = 2;
        require_send(foo2(&y, X::<'static>(&())));
    }
    error[E0597]: `x` does not live long enough
      --> src/main.rs:17:23
       |
    16 |     let x = 1;
       |         - binding `x` declared here
    17 |     require_send(foo1(&x, X::<'static>(&())));
       |     ------------------^^---------------------
       |     |                 |
       |     |                 borrowed value does not live long enough
       |     argument requires that `x` is borrowed for `'static`
    ...
    20 | }
       | - `x` dropped here while still borrowed
       |
    note: requirement that the value outlives `'static` introduced here
      --> src/main.rs:5:25
       |
     5 | fn require_send(_: impl Send) {}
       |                         ^^^^
    
    For more information about this error, try `rustc --explain E0597`.
    

    Oddly enough: removing the : 'static from foo1 makes the code compile.

  7. lcnr commented on Jul 7, 2026

    @lcnr
    Contributor

    I believe there is variance shennanigans happening in foo1 to make the actual return type be X<'a>; if you make X invariant in its lifetime parameter then the issue seems not to happen: playground

    jup, the type of the RPIT is X<'infer> with 'static: 'infer. We get a member constraint 'infer member ['a, 'static]. As 'a is the smallest member region, we constrain 'infer to be 'a instead of 'static.

    This is intended behavior 🤔 I do think it's less than ideal, but don't have a good sense how we'd avoid this

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-auto-traitsA-borrow-checkerA-impl-traitA-inferenceA-varianceC-bugT-types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions