@@ -291,12 +291,9 @@ impl FunctionBuilder {
291
291
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
292
292
let is_async = await_expr. is_some ( ) ;
293
293
294
- let ( ret_type, should_focus_return_type) = make_return_type (
295
- ctx,
296
- & ast:: Expr :: CallExpr ( call. clone ( ) ) ,
297
- target_module,
298
- & mut necessary_generic_params,
299
- ) ;
294
+ let expr_for_ret_ty = await_expr. map_or_else ( || call. clone ( ) . into ( ) , |it| it. into ( ) ) ;
295
+ let ( ret_type, should_focus_return_type) =
296
+ make_return_type ( ctx, & expr_for_ret_ty, target_module, & mut necessary_generic_params) ;
300
297
301
298
let ( generic_param_list, where_clause) =
302
299
fn_generic_params ( ctx, necessary_generic_params, & target) ?;
@@ -338,12 +335,9 @@ impl FunctionBuilder {
338
335
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
339
336
let is_async = await_expr. is_some ( ) ;
340
337
341
- let ( ret_type, should_focus_return_type) = make_return_type (
342
- ctx,
343
- & ast:: Expr :: MethodCallExpr ( call. clone ( ) ) ,
344
- target_module,
345
- & mut necessary_generic_params,
346
- ) ;
338
+ let expr_for_ret_ty = await_expr. map_or_else ( || call. clone ( ) . into ( ) , |it| it. into ( ) ) ;
339
+ let ( ret_type, should_focus_return_type) =
340
+ make_return_type ( ctx, & expr_for_ret_ty, target_module, & mut necessary_generic_params) ;
347
341
348
342
let ( generic_param_list, where_clause) =
349
343
fn_generic_params ( ctx, necessary_generic_params, & target) ?;
@@ -429,12 +423,12 @@ impl FunctionBuilder {
429
423
/// user can change the `todo!` function body.
430
424
fn make_return_type (
431
425
ctx : & AssistContext < ' _ > ,
432
- call : & ast:: Expr ,
426
+ expr : & ast:: Expr ,
433
427
target_module : Module ,
434
428
necessary_generic_params : & mut FxHashSet < hir:: GenericParam > ,
435
429
) -> ( Option < ast:: RetType > , bool ) {
436
430
let ( ret_ty, should_focus_return_type) = {
437
- match ctx. sema . type_of_expr ( call ) . map ( TypeInfo :: original) {
431
+ match ctx. sema . type_of_expr ( expr ) . map ( TypeInfo :: original) {
438
432
Some ( ty) if ty. is_unknown ( ) => ( Some ( make:: ty_placeholder ( ) ) , true ) ,
439
433
None => ( Some ( make:: ty_placeholder ( ) ) , true ) ,
440
434
Some ( ty) if ty. is_unit ( ) => ( None , false ) ,
@@ -2268,13 +2262,13 @@ impl Foo {
2268
2262
check_assist (
2269
2263
generate_function,
2270
2264
r"
2271
- fn foo() {
2272
- $0bar(42).await() ;
2265
+ async fn foo() {
2266
+ $0bar(42).await;
2273
2267
}
2274
2268
" ,
2275
2269
r"
2276
- fn foo() {
2277
- bar(42).await() ;
2270
+ async fn foo() {
2271
+ bar(42).await;
2278
2272
}
2279
2273
2280
2274
async fn bar(arg: i32) ${0:-> _} {
@@ -2284,6 +2278,28 @@ async fn bar(arg: i32) ${0:-> _} {
2284
2278
)
2285
2279
}
2286
2280
2281
+ #[ test]
2282
+ fn return_type_for_async_fn ( ) {
2283
+ check_assist (
2284
+ generate_function,
2285
+ r"
2286
+ //- minicore: result
2287
+ async fn foo() {
2288
+ if Err(()) = $0bar(42).await {}
2289
+ }
2290
+ " ,
2291
+ r"
2292
+ async fn foo() {
2293
+ if Err(()) = bar(42).await {}
2294
+ }
2295
+
2296
+ async fn bar(arg: i32) -> Result<_, ()> {
2297
+ ${0:todo!()}
2298
+ }
2299
+ " ,
2300
+ ) ;
2301
+ }
2302
+
2287
2303
#[ test]
2288
2304
fn create_method ( ) {
2289
2305
check_assist (
@@ -2401,6 +2417,31 @@ fn foo() {S.bar();}
2401
2417
)
2402
2418
}
2403
2419
2420
+ #[ test]
2421
+ fn create_async_method ( ) {
2422
+ check_assist (
2423
+ generate_function,
2424
+ r"
2425
+ //- minicore: result
2426
+ struct S;
2427
+ async fn foo() {
2428
+ if let Err(()) = S.$0bar(42).await {}
2429
+ }
2430
+ " ,
2431
+ r"
2432
+ struct S;
2433
+ impl S {
2434
+ async fn bar(&self, arg: i32) -> Result<_, ()> {
2435
+ ${0:todo!()}
2436
+ }
2437
+ }
2438
+ async fn foo() {
2439
+ if let Err(()) = S.bar(42).await {}
2440
+ }
2441
+ " ,
2442
+ )
2443
+ }
2444
+
2404
2445
#[ test]
2405
2446
fn create_static_method ( ) {
2406
2447
check_assist (
@@ -2421,6 +2462,31 @@ fn foo() {S::bar();}
2421
2462
)
2422
2463
}
2423
2464
2465
+ #[ test]
2466
+ fn create_async_static_method ( ) {
2467
+ check_assist (
2468
+ generate_function,
2469
+ r"
2470
+ //- minicore: result
2471
+ struct S;
2472
+ async fn foo() {
2473
+ if let Err(()) = S::$0bar(42).await {}
2474
+ }
2475
+ " ,
2476
+ r"
2477
+ struct S;
2478
+ impl S {
2479
+ async fn bar(arg: i32) -> Result<_, ()> {
2480
+ ${0:todo!()}
2481
+ }
2482
+ }
2483
+ async fn foo() {
2484
+ if let Err(()) = S::bar(42).await {}
2485
+ }
2486
+ " ,
2487
+ )
2488
+ }
2489
+
2424
2490
#[ test]
2425
2491
fn create_generic_static_method ( ) {
2426
2492
check_assist (
0 commit comments