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

Auto completion of async fn xxxx for desugared async fn in trait form #17719

Closed
hyf0 opened this issue Jul 27, 2024 · 3 comments
Closed

Auto completion of async fn xxxx for desugared async fn in trait form #17719

hyf0 opened this issue Jul 27, 2024 · 3 comments
Labels
A-completion autocompletion C-feature Category: feature request

Comments

@hyf0
Copy link
Contributor

hyf0 commented Jul 27, 2024

Hi. I'm thinking about improving the dx of writing impl DesugaredAsyncTrait for Foo. So far the rust analyzer will only infer auto completion like

image

However, I found that the compiler will allow the following code compiles. If this is an expected behavior(It's expected, see the following zulipchat link), maybe it's ok to make rust analyzer to support auto completion that starts with async fn foo( the sugar version rather than only desugared version like now) for DesugaredAsyncTrait .

This will surely improve the dx of writing impl DesugaredAsyncTrait for XXX greatly without the need to write impl Future<Output=XXXX> + Send over and over again(Though there are also auto completed by the rust-analyzer :).

use std::future::Future;

trait DesugaredAsyncTrait {
    fn foo(&self) -> impl Future<Output = usize> + Send;
    fn bar(&self) -> impl Future<Output = usize> + Send;
}

struct Foo;

impl DesugaredAsyncTrait for Foo {
    fn foo(&self) -> impl Future<Output = usize> + Send {
        async { 1 }
    }

    //
    async fn bar(&self) -> usize {
        1
    }
}

fn main() {
    let fut = Foo.bar();
    fn _assert_send<T: Send>(_: T) {}
    _assert_send(fut);
}

Related:

@hyf0 hyf0 added the C-feature Category: feature request label Jul 27, 2024
@Veykril Veykril added the A-completion autocompletion label Jul 28, 2024
@hyf0
Copy link
Contributor Author

hyf0 commented Jul 29, 2024

There's a question on Quick Fix "Implement missing members" if we support this PR.

On "Implement missing members", should we fill them in sugar version or original form for async fn in trait in desugar form?

@Veykril
Copy link
Member

Veykril commented Jul 29, 2024

I'd say sugared version, and then if we don't have it yet, have a desugar assist that goes from async to impl Future (I think we already have this though)

bors added a commit that referenced this issue Jul 29, 2024
feat(ide-completion): explictly show `async` keyword on `impl trait` methods

OLD:

<img width="676" alt="image" src="https://github.com/user-attachments/assets/f6fa626f-6b6d-4c22-af27-b0755e7a6bf8">

Now:

<img width="684" alt="image" src="https://github.com/user-attachments/assets/efbaac0e-c805-4dd2-859d-3e44b2886dbb">

---

This is an preparation for #17719.

```rust
use std::future::Future;

trait DesugaredAsyncTrait {
    fn foo(&self) -> impl Future<Output = usize> + Send;
    fn bar(&self) -> impl Future<Output = usize> + Send;
}

struct Foo;

impl DesugaredAsyncTrait for Foo {
    fn foo(&self) -> impl Future<Output = usize> + Send {
        async { 1 }
    }

    //
    async fn bar(&self) -> usize {
        1
    }
}

fn main() {
    let fut = Foo.bar();
    fn _assert_send<T: Send>(_: T) {}
    _assert_send(fut);
}
```

If we don't distinguish `async` or not. It would be confusing to generate sugared version `async fn foo ....` and original form `fn foo`  for `async fn in trait` that is defined in desugar form.
RalfJung pushed a commit to RalfJung/rust that referenced this issue Aug 1, 2024
feat(ide-completion): explictly show `async` keyword on `impl trait` methods

OLD:

<img width="676" alt="image" src="https://github.com/user-attachments/assets/f6fa626f-6b6d-4c22-af27-b0755e7a6bf8">

Now:

<img width="684" alt="image" src="https://github.com/user-attachments/assets/efbaac0e-c805-4dd2-859d-3e44b2886dbb">

---

This is an preparation for rust-lang/rust-analyzer#17719.

```rust
use std::future::Future;

trait DesugaredAsyncTrait {
    fn foo(&self) -> impl Future<Output = usize> + Send;
    fn bar(&self) -> impl Future<Output = usize> + Send;
}

struct Foo;

impl DesugaredAsyncTrait for Foo {
    fn foo(&self) -> impl Future<Output = usize> + Send {
        async { 1 }
    }

    //
    async fn bar(&self) -> usize {
        1
    }
}

fn main() {
    let fut = Foo.bar();
    fn _assert_send<T: Send>(_: T) {}
    _assert_send(fut);
}
```

If we don't distinguish `async` or not. It would be confusing to generate sugared version `async fn foo ....` and original form `fn foo`  for `async fn in trait` that is defined in desugar form.
bors added a commit that referenced this issue Sep 1, 2024
feat(ide-completion): extra sugar auto-completion `async fn ...` in `impl trait` for `async fn in trait` that's defined in desugar form

Solves #17719.

---

Preview

<img width="670" alt="image" src="https://github.com/user-attachments/assets/64ccef84-4062-4702-8760-89220585f422">

<img width="540" alt="image" src="https://github.com/user-attachments/assets/d22637f9-d531-43b2-a9f1-cd40a002903a">

<img width="631" alt="image" src="https://github.com/user-attachments/assets/21cd2142-bb8e-4493-9ac7-e6a9e7076904">
bors added a commit that referenced this issue Sep 1, 2024
feat(ide-completion): extra sugar auto-completion `async fn ...` in `impl trait` for `async fn in trait` that's defined in desugar form

Solves #17719.

---

Preview

<img width="670" alt="image" src="https://github.com/user-attachments/assets/64ccef84-4062-4702-8760-89220585f422">

<img width="540" alt="image" src="https://github.com/user-attachments/assets/d22637f9-d531-43b2-a9f1-cd40a002903a">

<img width="631" alt="image" src="https://github.com/user-attachments/assets/21cd2142-bb8e-4493-9ac7-e6a9e7076904">
@lnicola
Copy link
Member

lnicola commented Sep 1, 2024

Fixed in #17737

@lnicola lnicola closed this as completed Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion C-feature Category: feature request
Projects
None yet
Development

No branches or pull requests

3 participants