Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-completion/src/context/tests.rs')
-rw-r--r--crates/ide-completion/src/context/tests.rs177
1 files changed, 174 insertions, 3 deletions
diff --git a/crates/ide-completion/src/context/tests.rs b/crates/ide-completion/src/context/tests.rs
index 75c20968e1..51d28bd4ff 100644
--- a/crates/ide-completion/src/context/tests.rs
+++ b/crates/ide-completion/src/context/tests.rs
@@ -9,11 +9,16 @@ use crate::{
fn check_expected_type_and_name(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let (db, pos) = position(ra_fixture);
let config = TEST_CONFIG;
- let (completion_context, _analysis) = CompletionContext::new(&db, pos, &config).unwrap();
+ let (completion_context, _analysis) =
+ hir::attach_db(&db, || CompletionContext::new(&db, pos, &config, None).unwrap());
let ty = completion_context
.expected_type
- .map(|t| t.display_test(&db, completion_context.krate.to_display_target(&db)).to_string())
+ .map(|t| {
+ hir::attach_db(&db, || {
+ t.display_test(&db, completion_context.krate.to_display_target(&db)).to_string()
+ })
+ })
.unwrap_or("?".to_owned());
let name =
@@ -273,6 +278,62 @@ fn foo() {
}
#[test]
+fn expected_type_if_let_chain_bool() {
+ check_expected_type_and_name(
+ r#"
+fn foo() {
+ let f = Foo::Quux;
+ if let c = f && $0 { }
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_if_condition() {
+ check_expected_type_and_name(
+ r#"
+fn foo() {
+ if a$0 { }
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_if_body() {
+ check_expected_type_and_name(
+ r#"
+enum Foo { Bar, Baz, Quux }
+
+fn foo() {
+ let _: Foo = if true {
+ $0
+ };
+}
+"#,
+ expect![[r#"ty: Foo, name: ?"#]],
+ );
+
+ check_expected_type_and_name(
+ r#"
+enum Foo { Bar, Baz, Quux }
+
+fn foo() {
+ let _: Foo = if true {
+ Foo::Bar
+ } else {
+ $0
+ };
+}
+"#,
+ expect![[r#"ty: Foo, name: ?"#]],
+ );
+}
+
+#[test]
fn expected_type_fn_ret_without_leading_char() {
cov_mark::check!(expected_type_fn_ret_without_leading_char);
check_expected_type_and_name(
@@ -312,7 +373,6 @@ fn foo() -> u32 {
#[test]
fn expected_type_closure_param_return() {
- // FIXME: make this work with `|| $0`
check_expected_type_and_name(
r#"
//- minicore: fn
@@ -324,6 +384,18 @@ fn bar(f: impl FnOnce() -> u32) {}
"#,
expect![[r#"ty: u32, name: ?"#]],
);
+
+ check_expected_type_and_name(
+ r#"
+//- minicore: fn
+fn foo() {
+ bar(|| $0);
+}
+
+fn bar(f: impl FnOnce() -> u32) {}
+"#,
+ expect![[r#"ty: u32, name: ?"#]],
+ );
}
#[test]
@@ -434,3 +506,102 @@ fn f(thing: u32) -> &u32 {
expect!["ty: u32, name: ?"],
);
}
+
+#[test]
+fn expected_type_assign() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ let x: &mut State = &mut State::Stop;
+ x = $0;
+}
+"#,
+ expect![[r#"ty: &'_ mut State, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_deref_assign() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ let x: &mut State = &mut State::Stop;
+ match x {
+ State::Stop => {
+ *x = $0;
+ },
+ }
+}
+"#,
+ expect![[r#"ty: State, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_deref_assign_at_block_end() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ let x: &mut State = &mut State::Stop;
+ match x {
+ State::Stop => {
+ *x = $0
+ },
+ }
+}
+"#,
+ expect![[r#"ty: State, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_return_expr() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() -> State {
+ let _: i32 = if true {
+ 8
+ } else {
+ return $0;
+ };
+}
+"#,
+ expect![[r#"ty: State, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_return_expr_in_closure() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ let _f: fn() -> State = || {
+ let _: i32 = if true {
+ 8
+ } else {
+ return $0;
+ };
+ };
+}
+"#,
+ expect![[r#"ty: State, name: ?"#]],
+ );
+}
+
+#[test]
+fn expected_type_logic_op() {
+ check_expected_type_and_name(
+ r#"
+enum State { Stop }
+fn foo() {
+ true && $0;
+}
+"#,
+ expect![[r#"ty: bool, name: ?"#]],
+ );
+}