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

use<> bounds ("precise capture") require all type parameters to be listed #130031

Closed
nikomatsakis opened this issue Sep 6, 2024 · 4 comments
Closed
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. F-precise_capturing `#![feature(precise_capturing)]` T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

The current implementation of precise capture requires that use<> bounds include all type parameters in scope. This is a temporary limitation that should be lifted. Example:

#![allow(warnings)]

fn main() {
    let mut data = vec![1, 2, 3];
    let mut i = indices(&data);
    data.push(4);
    i.next();
}

fn indices<T>(
    slice: &[T],
) -> impl Iterator<Item = usize> + use<> {
    0 .. slice.len()
}

This code should compile, but it current gives an error (playground):

error: `impl Trait` must mention all type parameters in scope in `use<...>`
  --> src/main.rs:12:6
   |
10 | fn indices<T>(
   |            - type parameter is implicitly captured by this `impl Trait`
11 |     slice: &[T],
12 | ) -> impl Iterator<Item = usize> + use<> {
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: currently, all type parameters are required to be mentioned in the precise captures list
@nikomatsakis nikomatsakis added T-types Relevant to the types team, which will review and decide on the PR/issue. F-precise_capturing `#![feature(precise_capturing)]` labels Sep 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2024
@compiler-errors
Copy link
Member

There are certainly some cases where it will be impossible to "fix" this, though. I expect that all non-method type and const parameters will always need to be captured for RPITITs to be sound. We should probably explore which parameters are obligatory to capture in what scenarios.

@lolbinarycat lolbinarycat added the C-discussion Category: Discussion or questions that doesn't represent real issues. label Sep 6, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 7, 2024
@nikomatsakis
Copy link
Contributor Author

Closing in favor of #130043

@theemathas
Copy link
Contributor

I think that this example code is not very good, since changing the use<> to use<T> causes the code to compile.

@compiler-errors
Copy link
Member

@theemathas: The whole point of the example is to return an opaque that does not capture T. This has observable side-effects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. F-precise_capturing `#![feature(precise_capturing)]` T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants