Skip to content

Commit 51130d8

Browse files
committed
Auto merge of rust-lang#17736 - hyf0:hyf_09234908234, r=Veykril
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.
2 parents ca16c06 + 8250345 commit 51130d8

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/item_list/trait_impl.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ fn add_function_impl(
180180
) {
181181
let fn_name = func.name(ctx.db);
182182

183+
let is_async = func.is_async(ctx.db);
183184
let label = format_smolstr!(
184-
"fn {}({})",
185+
"{}fn {}({})",
186+
if is_async { "async " } else { "" },
185187
fn_name.display(ctx.db),
186188
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
187189
);
@@ -193,9 +195,13 @@ fn add_function_impl(
193195
});
194196

195197
let mut item = CompletionItem::new(completion_kind, replacement_range, label);
196-
item.lookup_by(format!("fn {}", fn_name.display(ctx.db)))
197-
.set_documentation(func.docs(ctx.db))
198-
.set_relevance(CompletionRelevance { is_item_from_trait: true, ..Default::default() });
198+
item.lookup_by(format!(
199+
"{}fn {}",
200+
if is_async { "async " } else { "" },
201+
fn_name.display(ctx.db)
202+
))
203+
.set_documentation(func.docs(ctx.db))
204+
.set_relevance(CompletionRelevance { is_item_from_trait: true, ..Default::default() });
199205

200206
if let Some(source) = ctx.sema.source(func) {
201207
let assoc_item = ast::AssocItem::Fn(source.value);

src/tools/rust-analyzer/crates/ide-completion/src/tests/item_list.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ trait Test {
299299
const CONST1: ();
300300
fn function0();
301301
fn function1();
302+
async fn function2();
302303
}
303304
304305
impl Test for () {
@@ -310,8 +311,9 @@ impl Test for () {
310311
"#,
311312
expect![[r#"
312313
ct const CONST1: () =
314+
fn async fn function2()
313315
fn fn function1()
314-
ma makro!(…) macro_rules! makro
316+
ma makro!(…) macro_rules! makro
315317
md module
316318
ta type Type1 =
317319
kw crate::

0 commit comments

Comments
 (0)