@@ -18,16 +18,19 @@ use hir_def::{
18
18
nameres:: CrateDefMap ,
19
19
AssocItemId , DefWithBodyId , LocalModuleId , Lookup , ModuleDefId ,
20
20
} ;
21
- use hir_expand:: InFile ;
21
+ use hir_expand:: { db :: AstDatabase , InFile } ;
22
22
use insta:: assert_snapshot;
23
23
use ra_db:: { fixture:: WithFixture , salsa:: Database , FilePosition , SourceDatabase } ;
24
24
use ra_syntax:: {
25
25
algo,
26
26
ast:: { self , AstNode } ,
27
+ SyntaxNode ,
27
28
} ;
28
29
use stdx:: format_to;
29
30
30
- use crate :: { db:: HirDatabase , display:: HirDisplay , test_db:: TestDB , InferenceResult } ;
31
+ use crate :: {
32
+ db:: HirDatabase , display:: HirDisplay , infer:: TypeMismatch , test_db:: TestDB , InferenceResult , Ty ,
33
+ } ;
31
34
32
35
// These tests compare the inference results for all expressions in a file
33
36
// against snapshots of the expected results using insta. Use cargo-insta to
@@ -67,43 +70,51 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
67
70
68
71
let mut infer_def = |inference_result : Arc < InferenceResult > ,
69
72
body_source_map : Arc < BodySourceMap > | {
70
- let mut types = Vec :: new ( ) ;
71
- let mut mismatches = Vec :: new ( ) ;
73
+ let mut types: Vec < ( InFile < SyntaxNode > , & Ty ) > = Vec :: new ( ) ;
74
+ let mut mismatches: Vec < ( InFile < SyntaxNode > , & TypeMismatch ) > = Vec :: new ( ) ;
72
75
73
76
for ( pat, ty) in inference_result. type_of_pat . iter ( ) {
74
77
let syntax_ptr = match body_source_map. pat_syntax ( pat) {
75
78
Ok ( sp) => {
76
- sp. map ( |ast| ast. either ( |it| it. syntax_node_ptr ( ) , |it| it. syntax_node_ptr ( ) ) )
79
+ let root = db. parse_or_expand ( sp. file_id ) . unwrap ( ) ;
80
+ sp. map ( |ptr| {
81
+ ptr. either (
82
+ |it| it. to_node ( & root) . syntax ( ) . clone ( ) ,
83
+ |it| it. to_node ( & root) . syntax ( ) . clone ( ) ,
84
+ )
85
+ } )
77
86
}
78
87
Err ( SyntheticSyntax ) => continue ,
79
88
} ;
80
89
types. push ( ( syntax_ptr, ty) ) ;
81
90
}
82
91
83
92
for ( expr, ty) in inference_result. type_of_expr . iter ( ) {
84
- let syntax_ptr = match body_source_map. expr_syntax ( expr) {
85
- Ok ( sp) => sp. map ( |ast| ast. syntax_node_ptr ( ) ) ,
93
+ let node = match body_source_map. expr_syntax ( expr) {
94
+ Ok ( sp) => {
95
+ let root = db. parse_or_expand ( sp. file_id ) . unwrap ( ) ;
96
+ sp. map ( |ptr| ptr. to_node ( & root) . syntax ( ) . clone ( ) )
97
+ }
86
98
Err ( SyntheticSyntax ) => continue ,
87
99
} ;
88
- types. push ( ( syntax_ptr . clone ( ) , ty) ) ;
100
+ types. push ( ( node . clone ( ) , ty) ) ;
89
101
if let Some ( mismatch) = inference_result. type_mismatch_for_expr ( expr) {
90
- mismatches. push ( ( syntax_ptr , mismatch) ) ;
102
+ mismatches. push ( ( node , mismatch) ) ;
91
103
}
92
104
}
93
105
94
106
// sort ranges for consistency
95
- types. sort_by_key ( |( src_ptr, _) | {
96
- ( src_ptr. value . range ( ) . start ( ) , src_ptr. value . range ( ) . end ( ) )
107
+ types. sort_by_key ( |( node, _) | {
108
+ let range = node. value . text_range ( ) ;
109
+ ( range. start ( ) , range. end ( ) )
97
110
} ) ;
98
- for ( src_ptr, ty) in & types {
99
- let node = src_ptr. value . to_node ( & src_ptr. file_syntax ( & db) ) ;
100
-
101
- let ( range, text) = if let Some ( self_param) = ast:: SelfParam :: cast ( node. clone ( ) ) {
111
+ for ( node, ty) in & types {
112
+ let ( range, text) = if let Some ( self_param) = ast:: SelfParam :: cast ( node. value . clone ( ) ) {
102
113
( self_param. self_token ( ) . unwrap ( ) . text_range ( ) , "self" . to_string ( ) )
103
114
} else {
104
- ( src_ptr . value . range ( ) , node. text ( ) . to_string ( ) . replace ( "\n " , " " ) )
115
+ ( node . value . text_range ( ) , node. value . text ( ) . to_string ( ) . replace ( "\n " , " " ) )
105
116
} ;
106
- let macro_prefix = if src_ptr . file_id != file_id. into ( ) { "!" } else { "" } ;
117
+ let macro_prefix = if node . file_id != file_id. into ( ) { "!" } else { "" } ;
107
118
format_to ! (
108
119
buf,
109
120
"{}{} '{}': {}\n " ,
@@ -114,11 +125,12 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
114
125
) ;
115
126
}
116
127
if include_mismatches {
117
- mismatches. sort_by_key ( |( src_ptr, _) | {
118
- ( src_ptr. value . range ( ) . start ( ) , src_ptr. value . range ( ) . end ( ) )
128
+ mismatches. sort_by_key ( |( node, _) | {
129
+ let range = node. value . text_range ( ) ;
130
+ ( range. start ( ) , range. end ( ) )
119
131
} ) ;
120
132
for ( src_ptr, mismatch) in & mismatches {
121
- let range = src_ptr. value . range ( ) ;
133
+ let range = src_ptr. value . text_range ( ) ;
122
134
let macro_prefix = if src_ptr. file_id != file_id. into ( ) { "!" } else { "" } ;
123
135
format_to ! (
124
136
buf,
0 commit comments