aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/tests.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-04-14 15:08:10 +0100
committerFlorian Diebold <[email protected]>2019-04-14 20:53:35 +0100
commit4f8a49f43cad086a656626c43062ff89b46f505a (patch)
treeb8275843aa56b922b6325b50be2aae063234cc2a /crates/ra_hir/src/ty/tests.rs
parent88be6f32172813f53dae60d73c9f5deb0c3fb29f (diff)
Refactor method candidate generation a bit
This fixes the order in which candidates are chosen a bit (not completely though, as the ignored test demonstrates), and makes autoderef work with trait methods. As a side effect, this also makes completion of trait methods work :)
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r--crates/ra_hir/src/ty/tests.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 291bc9ae5..82c4aeddb 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -2336,6 +2336,66 @@ fn test() -> u64 {
2336 ); 2336 );
2337} 2337}
2338 2338
2339#[ignore]
2340#[test]
2341fn method_resolution_trait_before_autoref() {
2342 let t = type_at(
2343 r#"
2344//- /main.rs
2345trait Trait { fn foo(self) -> u128; }
2346struct S;
2347impl S { fn foo(&self) -> i8 { 0 } }
2348impl Trait for S { fn foo(self) -> u128 { 0 } }
2349fn test() { S.foo()<|>; }
2350"#,
2351 );
2352 assert_eq!(t, "u128");
2353}
2354
2355#[test]
2356fn method_resolution_trait_before_autoderef() {
2357 let t = type_at(
2358 r#"
2359//- /main.rs
2360trait Trait { fn foo(self) -> u128; }
2361struct S;
2362impl S { fn foo(self) -> i8 { 0 } }
2363impl Trait for &S { fn foo(self) -> u128 { 0 } }
2364fn test() { (&S).foo()<|>; }
2365"#,
2366 );
2367 assert_eq!(t, "u128");
2368}
2369
2370#[test]
2371fn method_resolution_impl_before_trait() {
2372 let t = type_at(
2373 r#"
2374//- /main.rs
2375trait Trait { fn foo(self) -> u128; }
2376struct S;
2377impl S { fn foo(self) -> i8 { 0 } }
2378impl Trait for S { fn foo(self) -> u128 { 0 } }
2379fn test() { S.foo()<|>; }
2380"#,
2381 );
2382 assert_eq!(t, "i8");
2383}
2384
2385#[test]
2386fn method_resolution_trait_autoderef() {
2387 let t = type_at(
2388 r#"
2389//- /main.rs
2390trait Trait { fn foo(self) -> u128; }
2391struct S;
2392impl Trait for S { fn foo(self) -> u128 { 0 } }
2393fn test() { (&S).foo()<|>; }
2394"#,
2395 );
2396 assert_eq!(t, "u128");
2397}
2398
2339fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { 2399fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String {
2340 let file = db.parse(pos.file_id); 2400 let file = db.parse(pos.file_id);
2341 let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); 2401 let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
@@ -2344,6 +2404,11 @@ fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String {
2344 ty.display(db).to_string() 2404 ty.display(db).to_string()
2345} 2405}
2346 2406
2407fn type_at(content: &str) -> String {
2408 let (db, file_pos) = MockDatabase::with_position(content);
2409 type_at_pos(&db, file_pos)
2410}
2411
2347fn infer(content: &str) -> String { 2412fn infer(content: &str) -> String {
2348 let (db, _, file_id) = MockDatabase::with_single_file(content); 2413 let (db, _, file_id) = MockDatabase::with_single_file(content);
2349 let source_file = db.parse(file_id); 2414 let source_file = db.parse(file_id);