Skip to content

RPIT with 'static bound captures lifetimes from generic arguments #106750

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

Closed
jwong101 opened this issue Jan 12, 2023 · 2 comments
Closed

RPIT with 'static bound captures lifetimes from generic arguments #106750

jwong101 opened this issue Jan 12, 2023 · 2 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jwong101
Copy link
Contributor

jwong101 commented Jan 12, 2023

I tried this code:

fn bar<T>(_i: T) -> impl Iterator<Item = usize> + 'static {
    std::iter::once(3)
}

fn bar2<T: ?Sized>(_i: &T) -> impl Iterator<Item = usize> + 'static {
    std::iter::once(3)
}

fn main() {
    let mut b;
    {
        let a = 1;
        // doesn't work
        b = bar(&a);
        
        // this works
        // b = bar2(&a);
    }
    println!("{}", b.next().unwrap());
}

I expected to see this happen:

Both bar and bar2 should compile successfully, since both outputs have 'static bounds.

Instead, this happened:

The version that calls bar2 successfully compiles, whereas calling bar results in the following error:

error[[E0597]](https://doc.rust-lang.org/nightly/error-index.html#E0597): `a` does not live long enough
  --> src/main.rs:27:19
   |
27 |         b = bar(&a);
   |                   ^^ borrowed value does not live long enough
28 |     }
   |     - `a` dropped here while still borrowed
29 |     println!("{}", b.next().unwrap());
   |                    -------- borrow later used here

Using a TAIT as the return type of bar seems to not capture any lifetimes from T.

#![feature(type_alias_impl_trait)]

type Iter = impl Iterator<Item = usize> + 'static;

fn bar<T>(_i: T) -> Iter {
    std::iter::once(3)
}
fn main() {
    let mut b;
    {
        let a = 1;
        // this works fine
        b = bar(&a);
    }
    println!("{}", b.next().unwrap());
}

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (0442fbabe 2023-01-10)

This is also present on the latest stable version: rustc 1.66.1.

@jwong101 jwong101 added the C-bug Category: This is a bug. label Jan 12, 2023
@Noratrieb Noratrieb added A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. labels Jan 12, 2023
@Nugine
Copy link
Contributor

Nugine commented Mar 2, 2023

// #![feature(type_alias_impl_trait)]

pub fn compute1(data: impl AsRef<[u8]>) -> impl AsRef<[u8]> + 'static {
    [0u8; 4]
}

// type Output = impl AsRef<[u8]> + 'static;

// pub fn compute2(data: impl AsRef<[u8]>) -> Output {
//     [0u8; 4]
// }

pub fn foo() {
    let a: Vec<u8> = vec![0, 1, 2, 3];
    
    let b = compute1(a.as_slice()); // ERROR
    // let b = compute2(a.as_slice()); // ok

    drop(a);
    dbg!(b.as_ref());
}
error[E0505]: cannot move out of `a` because it is borrowed
  --> src/lib.rs:19:10
   |
14 |     let a: Vec<u8> = vec![0, 1, 2, 3];
   |         - binding `a` declared here
15 |     
16 |     let b = compute1(a.as_slice()); // ERROR
   |                      ------------ borrow of `a` occurs here
...
19 |     drop(a);
   |          ^ move out of `a` occurs here
20 |     dbg!(b.as_ref());
   |          ---------- borrow later used here

@jwong101
Copy link
Contributor Author

Duplicate of #42940

@jwong101 jwong101 closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants