Skip to content

Commit fd30bd1

Browse files
bors[bot]iDawer
andauthored
Merge #10146
10146: fix: use placeholder as default type in `Generate function` and `Extract into function`. r=matklad a=iDawer Closes #10123 Co-authored-by: Dawer <[email protected]>
2 parents 5fb2eb2 + 535761e commit fd30bd1

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,23 +1319,23 @@ impl Function {
13191319
.type_arguments()
13201320
.nth(1)
13211321
.map(|ty| make_ty(&ty, ctx, module))
1322-
.unwrap_or_else(make::ty_unit);
1322+
.unwrap_or_else(make::ty_placeholder);
13231323
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
13241324
}
13251325
FlowHandler::If { .. } => make::ext::ty_bool(),
13261326
FlowHandler::IfOption { action } => {
13271327
let handler_ty = action
13281328
.expr_ty(ctx)
13291329
.map(|ty| make_ty(&ty, ctx, module))
1330-
.unwrap_or_else(make::ty_unit);
1330+
.unwrap_or_else(make::ty_placeholder);
13311331
make::ext::ty_option(handler_ty)
13321332
}
13331333
FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)),
13341334
FlowHandler::MatchResult { err } => {
13351335
let handler_ty = err
13361336
.expr_ty(ctx)
13371337
.map(|ty| make_ty(&ty, ctx, module))
1338-
.unwrap_or_else(make::ty_unit);
1338+
.unwrap_or_else(make::ty_placeholder);
13391339
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
13401340
}
13411341
};
@@ -1501,7 +1501,7 @@ fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr
15011501
}
15021502

15031503
fn format_type(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> String {
1504-
ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "()".to_string())
1504+
ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "_".to_string())
15051505
}
15061506

15071507
fn make_ty(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> ast::Type {
@@ -4191,6 +4191,29 @@ fn main() {
41914191
fn $0fun_name(bar: &str) {
41924192
m!(bar);
41934193
}
4194+
"#,
4195+
);
4196+
}
4197+
4198+
#[test]
4199+
fn unresolveable_types_default_to_placeholder() {
4200+
check_assist(
4201+
extract_function,
4202+
r#"
4203+
fn foo() {
4204+
let a = __unresolved;
4205+
let _ = $0{a}$0;
4206+
}
4207+
"#,
4208+
r#"
4209+
fn foo() {
4210+
let a = __unresolved;
4211+
let _ = fun_name(a);
4212+
}
4213+
4214+
fn $0fun_name(a: _) -> _ {
4215+
a
4216+
}
41944217
"#,
41954218
);
41964219
}

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::{
3737
// bar("", baz());
3838
// }
3939
//
40-
// fn bar(arg: &str, baz: Baz) ${0:-> ()} {
40+
// fn bar(arg: &str, baz: Baz) ${0:-> _} {
4141
// todo!()
4242
// }
4343
//
@@ -342,7 +342,7 @@ impl FunctionBuilder {
342342
}
343343

344344
/// Makes an optional return type along with whether the return type should be focused by the cursor.
345-
/// If we cannot infer what the return type should be, we create unit as a placeholder.
345+
/// If we cannot infer what the return type should be, we create a placeholder type.
346346
///
347347
/// The rule for whether we focus a return type or not (and thus focus the function body),
348348
/// is rather simple:
@@ -357,14 +357,14 @@ fn make_return_type(
357357
) -> (Option<ast::RetType>, bool) {
358358
let (ret_ty, should_focus_return_type) = {
359359
match ctx.sema.type_of_expr(call).map(TypeInfo::original) {
360-
Some(ty) if ty.is_unknown() => (Some(make::ty_unit()), true),
361-
None => (Some(make::ty_unit()), true),
360+
Some(ty) if ty.is_unknown() => (Some(make::ty_placeholder()), true),
361+
None => (Some(make::ty_placeholder()), true),
362362
Some(ty) if ty.is_unit() => (None, false),
363363
Some(ty) => {
364364
let rendered = ty.display_source_code(ctx.db(), target_module.into());
365365
match rendered {
366366
Ok(rendered) => (Some(make::ty(&rendered)), false),
367-
Err(_) => (Some(make::ty_unit()), true),
367+
Err(_) => (Some(make::ty_placeholder()), true),
368368
}
369369
}
370370
}
@@ -458,7 +458,7 @@ fn fn_args(
458458
ty
459459
}
460460
}
461-
None => String::from("()"),
461+
None => String::from("_"),
462462
});
463463
}
464464
deduplicate_arg_names(&mut arg_names);
@@ -639,7 +639,7 @@ fn foo() {
639639
bar();
640640
}
641641
642-
fn bar() ${0:-> ()} {
642+
fn bar() ${0:-> _} {
643643
todo!()
644644
}
645645
",
@@ -666,7 +666,7 @@ impl Foo {
666666
}
667667
}
668668
669-
fn bar() ${0:-> ()} {
669+
fn bar() ${0:-> _} {
670670
todo!()
671671
}
672672
",
@@ -690,7 +690,7 @@ fn foo1() {
690690
bar();
691691
}
692692
693-
fn bar() ${0:-> ()} {
693+
fn bar() ${0:-> _} {
694694
todo!()
695695
}
696696
@@ -716,7 +716,7 @@ mod baz {
716716
bar();
717717
}
718718
719-
fn bar() ${0:-> ()} {
719+
fn bar() ${0:-> _} {
720720
todo!()
721721
}
722722
}
@@ -740,7 +740,7 @@ fn foo() {
740740
bar(BazBaz);
741741
}
742742
743-
fn bar(baz_baz: BazBaz) ${0:-> ()} {
743+
fn bar(baz_baz: BazBaz) ${0:-> _} {
744744
todo!()
745745
}
746746
",
@@ -763,7 +763,7 @@ fn foo() {
763763
bar(&BazBaz as *const BazBaz);
764764
}
765765
766-
fn bar(baz_baz: *const BazBaz) ${0:-> ()} {
766+
fn bar(baz_baz: *const BazBaz) ${0:-> _} {
767767
todo!()
768768
}
769769
",
@@ -788,7 +788,7 @@ fn foo() {
788788
bar(baz());
789789
}
790790
791-
fn bar(baz: Baz) ${0:-> ()} {
791+
fn bar(baz: Baz) ${0:-> _} {
792792
todo!()
793793
}
794794
",
@@ -1091,7 +1091,7 @@ fn foo() {
10911091
bar(Baz::new);
10921092
}
10931093
1094-
fn bar(new: fn) ${0:-> ()} {
1094+
fn bar(new: fn) ${0:-> _} {
10951095
todo!()
10961096
}
10971097
",
@@ -1115,15 +1115,15 @@ fn foo() {
11151115
bar(closure)
11161116
}
11171117
1118-
fn bar(closure: ()) {
1118+
fn bar(closure: _) {
11191119
${0:todo!()}
11201120
}
11211121
",
11221122
)
11231123
}
11241124

11251125
#[test]
1126-
fn unresolveable_types_default_to_unit() {
1126+
fn unresolveable_types_default_to_placeholder() {
11271127
check_assist(
11281128
generate_function,
11291129
r"
@@ -1136,7 +1136,7 @@ fn foo() {
11361136
bar(baz)
11371137
}
11381138
1139-
fn bar(baz: ()) {
1139+
fn bar(baz: _) {
11401140
${0:todo!()}
11411141
}
11421142
",
@@ -1400,7 +1400,7 @@ impl Foo {
14001400
self.bar();
14011401
}
14021402
1403-
fn bar(&self) ${0:-> ()} {
1403+
fn bar(&self) ${0:-> _} {
14041404
todo!()
14051405
}
14061406
}
@@ -1422,7 +1422,7 @@ fn foo() {
14221422
bar(42).await();
14231423
}
14241424
1425-
async fn bar(arg: i32) ${0:-> ()} {
1425+
async fn bar(arg: i32) ${0:-> _} {
14261426
todo!()
14271427
}
14281428
",
@@ -1443,7 +1443,7 @@ fn foo() {S.bar();}
14431443
impl S {
14441444
14451445
1446-
fn bar(&self) ${0:-> ()} {
1446+
fn bar(&self) ${0:-> _} {
14471447
todo!()
14481448
}
14491449
}
@@ -1465,7 +1465,7 @@ impl S {}
14651465
struct S;
14661466
fn foo() {S.bar();}
14671467
impl S {
1468-
fn bar(&self) ${0:-> ()} {
1468+
fn bar(&self) ${0:-> _} {
14691469
todo!()
14701470
}
14711471
}
@@ -1490,7 +1490,7 @@ mod s {
14901490
impl S {
14911491
14921492
1493-
pub(crate) fn bar(&self) ${0:-> ()} {
1493+
pub(crate) fn bar(&self) ${0:-> _} {
14941494
todo!()
14951495
}
14961496
}
@@ -1523,7 +1523,7 @@ mod s {
15231523
impl S {
15241524
15251525
1526-
fn bar(&self) ${0:-> ()} {
1526+
fn bar(&self) ${0:-> _} {
15271527
todo!()
15281528
}
15291529
}
@@ -1546,7 +1546,7 @@ fn foo() {S.bar();}
15461546
impl S {
15471547
15481548
1549-
fn bar(&self) ${0:-> ()} {
1549+
fn bar(&self) ${0:-> _} {
15501550
todo!()
15511551
}
15521552
}
@@ -1568,7 +1568,7 @@ fn foo() {S::bar();}
15681568
impl S {
15691569
15701570
1571-
fn bar() ${0:-> ()} {
1571+
fn bar() ${0:-> _} {
15721572
todo!()
15731573
}
15741574
}
@@ -1590,7 +1590,7 @@ impl S {}
15901590
struct S;
15911591
fn foo() {S::bar();}
15921592
impl S {
1593-
fn bar() ${0:-> ()} {
1593+
fn bar() ${0:-> _} {
15941594
todo!()
15951595
}
15961596
}
@@ -1615,7 +1615,7 @@ mod s {
16151615
impl S {
16161616
16171617
1618-
pub(crate) fn bar() ${0:-> ()} {
1618+
pub(crate) fn bar() ${0:-> _} {
16191619
todo!()
16201620
}
16211621
}
@@ -1639,7 +1639,7 @@ fn foo() {S::bar();}
16391639
impl S {
16401640
16411641
1642-
fn bar() ${0:-> ()} {
1642+
fn bar() ${0:-> _} {
16431643
todo!()
16441644
}
16451645
}

crates/ide_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ fn foo() {
819819
bar("", baz());
820820
}
821821
822-
fn bar(arg: &str, baz: Baz) ${0:-> ()} {
822+
fn bar(arg: &str, baz: Baz) ${0:-> _} {
823823
todo!()
824824
}
825825

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ pub fn lifetime(text: &str) -> ast::Lifetime {
9595
pub fn ty(text: &str) -> ast::Type {
9696
ty_from_text(text)
9797
}
98+
pub fn ty_placeholder() -> ast::Type {
99+
ty_from_text("_")
100+
}
98101
pub fn ty_unit() -> ast::Type {
99102
ty_from_text("()")
100103
}

0 commit comments

Comments
 (0)