Skip to content

Support for + use<..> precise_capturing syntax #17598

@traviscross

Description

@traviscross

We're planning to stabilize #![feature(precise_capturing)] ahead of Rust 2024 (in all editions). This feature allows one to write:

fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T> {}
//                                  ~~~~~~~~~~~~~~~~~~~~~~~
//                          This opaque type does not capture `'a`.

fn outlives<'o, T: 'o>(_: T) {}

fn caller<'o, 'a, 'b: 'o, T: 'o>() {
    //        ~~
    //        ^ Note that we don't need `'a: 'o`.
    outlives::<'o>(captures::<'a, 'b, T>());
}

Playground link

That is, it allows for the precise capturing of generic parameters in RPIT-like impl Trait opaque types (the initial stabilization will require all in scope type and const parameters to be included in the use<..> bound; see the stabilization report for other restrictions). We need this to stabilize so that we can stabilize the Lifetime Capture Rules 2024 (RFC 3498) in Rust 2024. These are lang team priority items for the Rust 2024 edition.

Currently r-a gives errors about this syntax:

fn captures<'a: 'a, 'b: 'b, T>() -> impl Sized + use<'b, T> {}
//                                              ~   ~~ ~  ~ ^ Syntax Error: expected an item
//                                              |   |  |  ^ Syntax Error: expected BANG
//                                              |   |  |  ^ Syntax Error: expected `{`, `[`, `(`
//                                              |   |  ^ Syntax Error: expected an item
//                                              |   ^ Syntax Error: expected SEMICOLON
//                                              ^ Syntax Error: expected a block

The earliest this syntax could conceivably stabilize is with Rust 1.82, which will branch on 2024-08-30 and will be released on 2024-10-17.

Tracking:

Activity

added
A-tytype system / type inference / traits / method resolution
on Jul 15, 2024
Veykril

Veykril commented on Jul 15, 2024

@Veykril
Member

Given we dont really handle opaque types wrt to their generic captures fully anyways I reckon it ought to be enough for us to merely add the parsing here for starters which is a simple change (ah thats what the issue is mainly asking for anyways whoops)

Veykril

Veykril commented on Jul 15, 2024

@Veykril
Member

Needs a corresponding grammar rule in https://github.com/rust-lang/rust-analyzer/blob/master/crates/syntax/rust.ungram
Then run cargo codegen grammar and finally edit the parser here

fn type_bound(p: &mut Parser<'_>) -> bool {
let m = p.start();
let has_paren = p.eat(T!['(']);
match p.current() {
LIFETIME_IDENT => lifetime(p),
T![for] => types::for_type(p, false),
T![?] if p.nth_at(1, T![for]) => {
// test question_for_type_trait_bound
// fn f<T>() where T: ?for<> Sized {}
p.bump_any();
types::for_type(p, false)
}
current => {
match current {
T![?] => p.bump_any(),
T![~] => {
p.bump_any();
p.expect(T![const]);
}
// test const_trait_bound
// const fn foo(_: impl const Trait) {}
T![const] => {
p.bump_any();
}
// test async_trait_bound
// fn async_foo(_: impl async Fn(&i32)) {}
T![async] => {
p.bump_any();
}
_ => (),
}
if paths::is_use_path_start(p) {
types::path_type_bounds(p, false);
} else {
m.abandon(p);
return false;
}
}
}
if has_paren {
p.expect(T![')']);
}
m.complete(p, TYPE_BOUND);
true
}

If new inline parser tests were added you can then run cargo codegen inline-parser-tests to regenerate those

added
E-has-instructionsIssue has some instructions and pointers to code to get started
on Jul 15, 2024
winstxnhdw

winstxnhdw commented on Jul 16, 2024

@winstxnhdw
Contributor

I’ll give this a go :)

added a commit that references this issue on Jul 25, 2024
added 2 commits that reference this issue on Aug 20, 2024
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-parserparser issuesA-tytype system / type inference / traits / method resolutionC-featureCategory: feature requestE-easyE-has-instructionsIssue has some instructions and pointers to code to get started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lnicola@traviscross@Veykril@winstxnhdw

        Issue actions

          Support for `+ use<..>` `precise_capturing` syntax · Issue #17598 · rust-lang/rust-analyzer