Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPIT associated type lifetime bound is ignored #135006

Open
LHolten opened this issue Jan 1, 2025 · 2 comments · May be fixed by #135008
Open

RPIT associated type lifetime bound is ignored #135006

LHolten opened this issue Jan 1, 2025 · 2 comments · May be fixed by #135008
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@LHolten
Copy link
Contributor

LHolten commented Jan 1, 2025

I tried this code:

trait Foo<'x> {
    type Out;
    fn foo(self) -> Self::Out;
}

struct Bar;

impl<'x> Foo<'x> for Bar {
    type Out = ();
    fn foo(self) -> Self::Out {
        todo!()
    }
}

fn make_static_foo<'x>(_: &'x ()) -> impl Foo<'x, Out: 'static> {
    Bar
}

fn assert_static<T: 'static>(_: T) {}

fn test() {
    let local = ();
    assert_static(make_static_foo(&local).foo());
}

I expect it to compile, but instead it gives the following error:

error[E0597]: `local` does not live long enough
  --> src/lib.rs:23:35
   |
22 |     let local = ();
   |         ----- binding `local` declared here
23 |     assert_static(make_static_foo(&local).foo());
   |                   ----------------^^^^^^-
   |                   |               |
   |                   |               borrowed value does not live long enough
   |                   argument requires that `local` is borrowed for `'static`
24 | }
   | - `local` dropped here while still borrowed

It looks like the Out: 'static bound on make_static_foo is just ignored.

The issue sounds similar to #106684, but it is actually the opposite:
In this issue the bound is in the RPIT instead of in the trait definition.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (7f75bfa1a 2024-12-30)
binary: rustc
commit-hash: 7f75bfa1ad4e9a9d33a179a90603001515e91991
commit-date: 2024-12-30
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Same happens on stable

@LHolten LHolten added the C-bug Category: This is a bug. label Jan 1, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 1, 2025
@LHolten LHolten changed the title RPIT associated lifetime bound is ignored RPIT associated type lifetime bound is ignored Jan 1, 2025
@traviscross traviscross added A-lifetimes Area: Lifetimes / regions A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Jan 1, 2025
@traviscross
Copy link
Contributor

traviscross commented Jan 1, 2025

Interestingly, this doesn't work:

trait Tr<'x> {
    type Ty;
    fn f(self) -> Self::Ty;
}

impl Tr<'_> for () {
    type Ty = ();
    fn f(self) -> Self::Ty {}
}

fn g(_: &()) -> impl Tr<'_, Ty: Sized + 'static> {} // <--

fn test(x: &()) -> impl Sized + 'static + use<> {
    g(x).f()
//~^ ERROR lifetime may not live long enough
}

But this does:

trait Tr<'x> {
    type Ty;
    fn f(self) -> Self::Ty;
}

impl Tr<'_> for () {
    type Ty = ();
    fn f(self) -> Self::Ty {}
}

fn g(_: &()) -> impl Tr<'_, Ty = impl Sized + 'static> {} // <--

fn test(x: &()) -> impl Sized + 'static + use<> {
    g(x).f()
}

Equivalently, this works:

Playground link

And this does not:

Playground link

cc @compiler-errors @oli-obk

@compiler-errors

This comment has been minimized.

@compiler-errors compiler-errors self-assigned this Jan 2, 2025
@traviscross traviscross added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 2, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 3, 2025
…=oli-obk

Some type-outlives computation tweaks

Some tweaks that I wrote when investigating rust-lang#135006.

The only commit that's probably interesting here is f364674 (the first commit). For some reason it was concerned with filtering out param-env outlives clauses when they matched item-bound outlives clauses. However, if you look at the rest of the control flow for that function, not filtering out those bounds doesn't actually affect the behavior materially.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 3, 2025
…=oli-obk

Some type-outlives computation tweaks

Some tweaks that I wrote when investigating rust-lang#135006.

The only commit that's probably interesting here is f364674 (the first commit). For some reason it was concerned with filtering out param-env outlives clauses when they matched item-bound outlives clauses. However, if you look at the rest of the control flow for that function, not filtering out those bounds doesn't actually affect the behavior materially.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 4, 2025
Rollup merge of rust-lang#135007 - compiler-errors:outlives-tweaks, r=oli-obk

Some type-outlives computation tweaks

Some tweaks that I wrote when investigating rust-lang#135006.

The only commit that's probably interesting here is f364674 (the first commit). For some reason it was concerned with filtering out param-env outlives clauses when they matched item-bound outlives clauses. However, if you look at the rest of the control flow for that function, not filtering out those bounds doesn't actually affect the behavior materially.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants