@@ -81,6 +81,9 @@ pub struct Translator<'a> {
81
81
pub semantics : Option < & ' a Semantics < ' a , RootDatabase > > ,
82
82
}
83
83
84
+ const UNKNOWN_LOCATION : ( LineCol , LineCol ) =
85
+ ( LineCol { line : 0 , col : 0 } , LineCol { line : 0 , col : 0 } ) ;
86
+
84
87
impl < ' a > Translator < ' a > {
85
88
pub fn new (
86
89
trap : TrapFile ,
@@ -98,8 +101,8 @@ impl<'a> Translator<'a> {
98
101
semantics : semantic_info. map ( |i| i. semantics ) ,
99
102
}
100
103
}
101
- fn location ( & self , range : TextRange ) -> ( LineCol , LineCol ) {
102
- let start = self . line_index . line_col ( range. start ( ) ) ;
104
+ fn location ( & self , range : TextRange ) -> Option < ( LineCol , LineCol ) > {
105
+ let start = self . line_index . try_line_col ( range. start ( ) ) ? ;
103
106
let range_end = range. end ( ) ;
104
107
// QL end positions are inclusive, while TextRange offsets are exclusive and point at the position
105
108
// right after the last character of the range. We need to shift the end offset one character to the left to
@@ -111,11 +114,11 @@ impl<'a> Translator<'a> {
111
114
. checked_sub ( i. into ( ) )
112
115
. and_then ( |x| self . line_index . try_line_col ( x) )
113
116
{
114
- return ( start, end) ;
117
+ return Some ( ( start, end) ) ;
115
118
}
116
119
}
117
- let end = self . line_index . line_col ( range_end) ;
118
- ( start, end)
120
+ let end = self . line_index . try_line_col ( range_end) ? ;
121
+ Some ( ( start, end) )
119
122
}
120
123
121
124
pub fn text_range_for_node ( & mut self , node : & impl ast:: AstNode ) -> Option < TextRange > {
@@ -132,16 +135,18 @@ impl<'a> Translator<'a> {
132
135
}
133
136
}
134
137
pub fn emit_location < T : TrapClass > ( & mut self , label : Label < T > , node : & impl ast:: AstNode ) {
135
- if let Some ( range) = self . text_range_for_node ( node) {
136
- let ( start, end) = self . location ( range) ;
138
+ if let Some ( ( start, end) ) = self
139
+ . text_range_for_node ( node)
140
+ . and_then ( |r| self . location ( r) )
141
+ {
137
142
self . trap . emit_location ( self . label , label, start, end)
138
143
} else {
139
144
self . emit_diagnostic (
140
145
DiagnosticSeverity :: Debug ,
141
146
"locations" . to_owned ( ) ,
142
147
"missing location for AstNode" . to_owned ( ) ,
143
148
"missing location for AstNode" . to_owned ( ) ,
144
- ( LineCol { line : 0 , col : 0 } , LineCol { line : 0 , col : 0 } ) ,
149
+ UNKNOWN_LOCATION ,
145
150
) ;
146
151
}
147
152
}
@@ -156,8 +161,9 @@ impl<'a> Translator<'a> {
156
161
if let Some ( clipped_range) = token_range. intersect ( parent_range) {
157
162
if let Some ( parent_range2) = self . text_range_for_node ( parent) {
158
163
let token_range = clipped_range + parent_range2. start ( ) - parent_range. start ( ) ;
159
- let ( start, end) = self . location ( token_range) ;
160
- self . trap . emit_location ( self . label , label, start, end)
164
+ if let Some ( ( start, end) ) = self . location ( token_range) {
165
+ self . trap . emit_location ( self . label , label, start, end)
166
+ }
161
167
}
162
168
}
163
169
}
@@ -206,7 +212,7 @@ impl<'a> Translator<'a> {
206
212
"parse_error" . to_owned ( ) ,
207
213
message. clone ( ) ,
208
214
message,
209
- location,
215
+ location. unwrap_or ( UNKNOWN_LOCATION ) ,
210
216
) ;
211
217
}
212
218
}
0 commit comments