@@ -17,11 +17,11 @@ use hir_def::{
1717 item_scope:: ItemScope ,
1818 keys,
1919 nameres:: CrateDefMap ,
20- AssocItemId , DefWithBodyId , LocalModuleId , Lookup , ModuleDefId , ModuleId ,
20+ AssocItemId , DefWithBodyId , LocalModuleId , Lookup , ModuleDefId ,
2121} ;
2222use hir_expand:: { db:: AstDatabase , InFile } ;
2323use insta:: assert_snapshot;
24- use ra_db:: { fixture:: WithFixture , salsa:: Database , FilePosition , SourceDatabase } ;
24+ use ra_db:: { fixture:: WithFixture , salsa:: Database , FileRange , SourceDatabase } ;
2525use ra_syntax:: {
2626 algo,
2727 ast:: { self , AstNode } ,
@@ -39,35 +39,37 @@ use crate::{
3939// update the snapshots.
4040
4141fn check_types ( ra_fixture : & str ) {
42+ check_types_impl ( ra_fixture, false )
43+ }
44+
45+ fn check_types_source_code ( ra_fixture : & str ) {
46+ check_types_impl ( ra_fixture, true )
47+ }
48+
49+ fn check_types_impl ( ra_fixture : & str , display_source : bool ) {
4250 let db = TestDB :: with_files ( ra_fixture) ;
4351 let mut checked_one = false ;
4452 for file_id in db. all_files ( ) {
4553 let text = db. parse ( file_id) . syntax_node ( ) . to_string ( ) ;
4654 let annotations = extract_annotations ( & text) ;
47- for ( offset, expected) in annotations {
48- let actual = type_at_pos ( & db, FilePosition { file_id, offset } ) ;
55+ for ( range, expected) in annotations {
56+ let ty = type_at_range ( & db, FileRange { file_id, range } ) ;
57+ let actual = if display_source {
58+ let module = db. module_for_file ( file_id) ;
59+ ty. display_source_code ( & db, module) . unwrap ( )
60+ } else {
61+ ty. display ( & db) . to_string ( )
62+ } ;
4963 assert_eq ! ( expected, actual) ;
5064 checked_one = true ;
5165 }
5266 }
5367 assert ! ( checked_one, "no `//^` annotations found" ) ;
5468}
5569
56- fn type_at_pos ( db : & TestDB , pos : FilePosition ) -> String {
57- type_at_pos_displayed ( db, pos, |ty, _| ty. display ( db) . to_string ( ) )
58- }
59-
60- fn displayed_source_at_pos ( db : & TestDB , pos : FilePosition ) -> String {
61- type_at_pos_displayed ( db, pos, |ty, module_id| ty. display_source_code ( db, module_id) . unwrap ( ) )
62- }
63-
64- fn type_at_pos_displayed (
65- db : & TestDB ,
66- pos : FilePosition ,
67- display_fn : impl FnOnce ( & Ty , ModuleId ) -> String ,
68- ) -> String {
70+ fn type_at_range ( db : & TestDB , pos : FileRange ) -> Ty {
6971 let file = db. parse ( pos. file_id ) . ok ( ) . unwrap ( ) ;
70- let expr = algo:: find_node_at_offset :: < ast:: Expr > ( file. syntax ( ) , pos. offset ) . unwrap ( ) ;
72+ let expr = algo:: find_node_at_range :: < ast:: Expr > ( file. syntax ( ) , pos. range ) . unwrap ( ) ;
7173 let fn_def = expr. syntax ( ) . ancestors ( ) . find_map ( ast:: FnDef :: cast) . unwrap ( ) ;
7274 let module = db. module_for_file ( pos. file_id ) ;
7375 let func = * module. child_by_source ( db) [ keys:: FUNCTION ]
@@ -77,17 +79,11 @@ fn type_at_pos_displayed(
7779 let ( _body, source_map) = db. body_with_source_map ( func. into ( ) ) ;
7880 if let Some ( expr_id) = source_map. node_expr ( InFile :: new ( pos. file_id . into ( ) , & expr) ) {
7981 let infer = db. infer ( func. into ( ) ) ;
80- let ty = & infer[ expr_id] ;
81- return display_fn ( ty, module) ;
82+ return infer[ expr_id] . clone ( ) ;
8283 }
8384 panic ! ( "Can't find expression" )
8485}
8586
86- fn type_at ( ra_fixture : & str ) -> String {
87- let ( db, file_pos) = TestDB :: with_position ( ra_fixture) ;
88- type_at_pos ( & db, file_pos)
89- }
90-
9187fn infer ( ra_fixture : & str ) -> String {
9288 infer_with_mismatches ( ra_fixture, false )
9389}
0 commit comments