Skip to content

Commit c5c11b8

Browse files
bors[bot]Veykril
andauthored
Merge #10720
10720: fix: Don't ascribe types in pattern completion for param patterns twice r=Veykril a=Veykril Fixes #10323 bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 2c0f433 + f7e8136 commit c5c11b8

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

crates/ide_completion/src/context.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) struct PathCompletionContext {
5858
pub(super) struct PatternContext {
5959
pub(super) refutability: PatternRefutability,
6060
pub(super) is_param: Option<ParamKind>,
61+
pub(super) has_type_ascription: bool,
6162
}
6263

6364
#[derive(Debug)]
@@ -597,7 +598,8 @@ impl<'a> CompletionContext<'a> {
597598
.map(|c| (Some(c.return_type()), None))
598599
.unwrap_or((None, None))
599600
},
600-
ast::Stmt(_it) => (None, None),
601+
ast::ParamList(__) => (None, None),
602+
ast::Stmt(__) => (None, None),
601603
ast::Item(__) => (None, None),
602604
_ => {
603605
match node.parent() {
@@ -708,15 +710,15 @@ impl<'a> CompletionContext<'a> {
708710
return None;
709711
}
710712
let mut is_param = None;
711-
let refutability = bind_pat
713+
let (refutability, has_type_ascription) = bind_pat
712714
.syntax()
713715
.ancestors()
714716
.skip_while(|it| ast::Pat::can_cast(it.kind()))
715717
.next()
716-
.map_or(PatternRefutability::Irrefutable, |node| {
717-
match_ast! {
718+
.map_or((PatternRefutability::Irrefutable, false), |node| {
719+
let refutability = match_ast! {
718720
match node {
719-
ast::LetStmt(__) => PatternRefutability::Irrefutable,
721+
ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
720722
ast::Param(param) => {
721723
let is_closure_param = param
722724
.syntax()
@@ -729,16 +731,17 @@ impl<'a> CompletionContext<'a> {
729731
} else {
730732
ParamKind::Function
731733
});
732-
PatternRefutability::Irrefutable
734+
return (PatternRefutability::Irrefutable, param.ty().is_some())
733735
},
734736
ast::MatchArm(__) => PatternRefutability::Refutable,
735737
ast::Condition(__) => PatternRefutability::Refutable,
736738
ast::ForExpr(__) => PatternRefutability::Irrefutable,
737739
_ => PatternRefutability::Irrefutable,
738740
}
739-
}
741+
};
742+
(refutability, false)
740743
});
741-
Some(PatternContext { refutability, is_param })
744+
Some(PatternContext { refutability, is_param, has_type_ascription })
742745
}
743746

744747
fn classify_name_ref(
@@ -1172,4 +1175,23 @@ fn foo() {
11721175
expect![[r#"ty: Foo, name: ?"#]],
11731176
);
11741177
}
1178+
1179+
#[test]
1180+
fn expected_type_param_pat() {
1181+
check_expected_type_and_name(
1182+
r#"
1183+
struct Foo { field: u32 }
1184+
fn foo(a$0: Foo) {}
1185+
"#,
1186+
expect![[r#"ty: Foo, name: ?"#]],
1187+
);
1188+
check_expected_type_and_name(
1189+
r#"
1190+
struct Foo { field: u32 }
1191+
fn foo($0: Foo) {}
1192+
"#,
1193+
// FIXME make this work, currently fails due to pattern recovery eating the `:`
1194+
expect![[r#"ty: ?, name: ?"#]],
1195+
);
1196+
}
11751197
}

crates/ide_completion/src/render/pattern.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ fn render_pat(
8686

8787
if matches!(
8888
ctx.completion.pattern_ctx,
89-
Some(PatternContext { is_param: Some(ParamKind::Function), .. })
89+
Some(PatternContext {
90+
is_param: Some(ParamKind::Function),
91+
has_type_ascription: false,
92+
..
93+
})
9094
) {
9195
pat.push(':');
9296
pat.push(' ');

crates/ide_completion/src/tests/pattern.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ fn foo(a$0) {
163163
ma makro!(…) #[macro_export] macro_rules! makro
164164
"##]],
165165
);
166+
check(
167+
r#"
168+
fn foo(a$0: Tuple) {
169+
}
170+
"#,
171+
expect![[r##"
172+
kw mut
173+
bn Record Record { field$1 }$0
174+
st Record
175+
bn Tuple Tuple($1)$0
176+
st Tuple
177+
st Unit
178+
ma makro!(…) #[macro_export] macro_rules! makro
179+
"##]],
180+
);
166181
}
167182

168183
#[test]

0 commit comments

Comments
 (0)