diff options
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 62 |
1 files changed, 25 insertions, 37 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index ecc63f376..d7c2ca132 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -4,15 +4,15 @@ use std::fmt::Write; | |||
4 | use insta::assert_snapshot_matches; | 4 | use insta::assert_snapshot_matches; |
5 | 5 | ||
6 | use ra_db::{SourceDatabase, salsa::Database, FilePosition}; | 6 | use ra_db::{SourceDatabase, salsa::Database, FilePosition}; |
7 | use ra_syntax::{algo, ast::{self, AstNode}}; | 7 | use ra_syntax::{algo, ast::{self, AstNode}, SyntaxKind::*}; |
8 | use test_utils::covers; | 8 | use test_utils::covers; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | source_binder, | ||
12 | mock::MockDatabase, | 11 | mock::MockDatabase, |
13 | ty::display::HirDisplay, | 12 | ty::display::HirDisplay, |
14 | ty::InferenceResult, | 13 | ty::InferenceResult, |
15 | expr::BodySourceMap | 14 | expr::BodySourceMap, |
15 | SourceAnalyzer, | ||
16 | }; | 16 | }; |
17 | 17 | ||
18 | // These tests compare the inference results for all expressions in a file | 18 | // These tests compare the inference results for all expressions in a file |
@@ -1862,14 +1862,14 @@ fn test() { | |||
1862 | @r###" | 1862 | @r###" |
1863 | [49; 50) '0': u32 | 1863 | [49; 50) '0': u32 |
1864 | [80; 83) '101': u32 | 1864 | [80; 83) '101': u32 |
1865 | [126; 128) '99': u32 | ||
1866 | [95; 213) '{ ...NST; }': () | 1865 | [95; 213) '{ ...NST; }': () |
1867 | [138; 139) 'x': {unknown} | 1866 | [138; 139) 'x': {unknown} |
1868 | [142; 153) 'LOCAL_CONST': {unknown} | 1867 | [142; 153) 'LOCAL_CONST': {unknown} |
1869 | [163; 164) 'z': u32 | 1868 | [163; 164) 'z': u32 |
1870 | [167; 179) 'GLOBAL_CONST': u32 | 1869 | [167; 179) 'GLOBAL_CONST': u32 |
1871 | [189; 191) 'id': u32 | 1870 | [189; 191) 'id': u32 |
1872 | [194; 210) 'Foo::A..._CONST': u32"### | 1871 | [194; 210) 'Foo::A..._CONST': u32 |
1872 | [126; 128) '99': u32"### | ||
1873 | ); | 1873 | ); |
1874 | } | 1874 | } |
1875 | 1875 | ||
@@ -1891,8 +1891,6 @@ fn test() { | |||
1891 | @r###" | 1891 | @r###" |
1892 | [29; 32) '101': u32 | 1892 | [29; 32) '101': u32 |
1893 | [70; 73) '101': u32 | 1893 | [70; 73) '101': u32 |
1894 | [118; 120) '99': u32 | ||
1895 | [161; 163) '99': u32 | ||
1896 | [85; 280) '{ ...MUT; }': () | 1894 | [85; 280) '{ ...MUT; }': () |
1897 | [173; 174) 'x': {unknown} | 1895 | [173; 174) 'x': {unknown} |
1898 | [177; 189) 'LOCAL_STATIC': {unknown} | 1896 | [177; 189) 'LOCAL_STATIC': {unknown} |
@@ -1901,7 +1899,9 @@ fn test() { | |||
1901 | [229; 230) 'z': u32 | 1899 | [229; 230) 'z': u32 |
1902 | [233; 246) 'GLOBAL_STATIC': u32 | 1900 | [233; 246) 'GLOBAL_STATIC': u32 |
1903 | [256; 257) 'w': u32 | 1901 | [256; 257) 'w': u32 |
1904 | [260; 277) 'GLOBAL...IC_MUT': u32"### | 1902 | [260; 277) 'GLOBAL...IC_MUT': u32 |
1903 | [118; 120) '99': u32 | ||
1904 | [161; 163) '99': u32"### | ||
1905 | ); | 1905 | ); |
1906 | } | 1906 | } |
1907 | 1907 | ||
@@ -2302,13 +2302,10 @@ fn test() -> u64 { | |||
2302 | } | 2302 | } |
2303 | 2303 | ||
2304 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { | 2304 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { |
2305 | let func = source_binder::function_from_position(db, pos).unwrap(); | 2305 | let file = db.parse(pos.file_id); |
2306 | let body_source_map = func.body_source_map(db); | 2306 | let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); |
2307 | let inference_result = func.infer(db); | 2307 | let analyzer = SourceAnalyzer::new(db, pos.file_id, expr.syntax(), Some(pos.offset)); |
2308 | let (_, syntax) = func.source(db); | 2308 | let ty = analyzer.type_of(db, expr).unwrap(); |
2309 | let node = algo::find_node_at_offset::<ast::Expr>(syntax.syntax(), pos.offset).unwrap(); | ||
2310 | let expr = body_source_map.node_expr(node).unwrap(); | ||
2311 | let ty = &inference_result[expr]; | ||
2312 | ty.display(db).to_string() | 2309 | ty.display(db).to_string() |
2313 | } | 2310 | } |
2314 | 2311 | ||
@@ -2350,25 +2347,11 @@ fn infer(content: &str) -> String { | |||
2350 | } | 2347 | } |
2351 | }; | 2348 | }; |
2352 | 2349 | ||
2353 | for const_def in source_file.syntax().descendants().filter_map(ast::ConstDef::cast) { | 2350 | for node in source_file.syntax().descendants() { |
2354 | let konst = source_binder::const_from_source(&db, file_id, const_def).unwrap(); | 2351 | if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { |
2355 | let inference_result = konst.infer(&db); | 2352 | let analyzer = SourceAnalyzer::new(&db, file_id, node, None); |
2356 | let body_source_map = konst.body_source_map(&db); | 2353 | infer_def(analyzer.inference_result(), analyzer.body_source_map()); |
2357 | infer_def(inference_result, body_source_map) | 2354 | } |
2358 | } | ||
2359 | |||
2360 | for static_def in source_file.syntax().descendants().filter_map(ast::StaticDef::cast) { | ||
2361 | let static_ = source_binder::static_from_source(&db, file_id, static_def).unwrap(); | ||
2362 | let inference_result = static_.infer(&db); | ||
2363 | let body_source_map = static_.body_source_map(&db); | ||
2364 | infer_def(inference_result, body_source_map) | ||
2365 | } | ||
2366 | |||
2367 | for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { | ||
2368 | let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); | ||
2369 | let inference_result = func.infer(&db); | ||
2370 | let body_source_map = func.body_source_map(&db); | ||
2371 | infer_def(inference_result, body_source_map) | ||
2372 | } | 2355 | } |
2373 | 2356 | ||
2374 | acc.truncate(acc.trim_end().len()); | 2357 | acc.truncate(acc.trim_end().len()); |
@@ -2403,10 +2386,12 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { | |||
2403 | } | 2386 | } |
2404 | ", | 2387 | ", |
2405 | ); | 2388 | ); |
2406 | let func = source_binder::function_from_position(&db, pos).unwrap(); | ||
2407 | { | 2389 | { |
2390 | let file = db.parse(pos.file_id); | ||
2391 | let node = | ||
2392 | algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); | ||
2408 | let events = db.log_executed(|| { | 2393 | let events = db.log_executed(|| { |
2409 | func.infer(&db); | 2394 | SourceAnalyzer::new(&db, pos.file_id, node, None); |
2410 | }); | 2395 | }); |
2411 | assert!(format!("{:?}", events).contains("infer")) | 2396 | assert!(format!("{:?}", events).contains("infer")) |
2412 | } | 2397 | } |
@@ -2423,8 +2408,11 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { | |||
2423 | db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text)); | 2408 | db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text)); |
2424 | 2409 | ||
2425 | { | 2410 | { |
2411 | let file = db.parse(pos.file_id); | ||
2412 | let node = | ||
2413 | algo::find_token_at_offset(file.syntax(), pos.offset).right_biased().unwrap().parent(); | ||
2426 | let events = db.log_executed(|| { | 2414 | let events = db.log_executed(|| { |
2427 | func.infer(&db); | 2415 | SourceAnalyzer::new(&db, pos.file_id, node, None); |
2428 | }); | 2416 | }); |
2429 | assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events) | 2417 | assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events) |
2430 | } | 2418 | } |