Skip to content

Commit 341f8bb

Browse files
committed
fix: avoid panics in match case diagnostic
1 parent 10b15b2 commit 341f8bb

File tree

1 file changed

+33
-108
lines changed

1 file changed

+33
-108
lines changed

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 33 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,11 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
150150
expected_case: CaseType::LowerSnakeCase,
151151
});
152152

153-
// Check the param names.
154-
let fn_param_replacements = body
155-
.params
156-
.iter()
157-
.filter_map(|&id| match &body[id] {
158-
Pat::Bind { name, .. } => Some(name),
159-
_ => None,
160-
})
161-
.filter_map(|param_name| {
162-
Some(Replacement {
163-
current_name: param_name.clone(),
164-
suggested_text: to_lower_snake_case(&param_name.to_string())?,
165-
expected_case: CaseType::LowerSnakeCase,
166-
})
167-
})
168-
.collect();
169-
170153
// Check the patterns inside the function body.
154+
// This includes function parameters.
171155
let pats_replacements = body
172156
.pats
173157
.iter()
174-
// We aren't interested in function parameters, we've processed them above.
175-
.filter(|(pat_idx, _)| !body.params.contains(&pat_idx))
176158
.filter_map(|(id, pat)| match pat {
177159
Pat::Bind { name, .. } => Some((id, name)),
178160
_ => None,
@@ -190,11 +172,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
190172
.collect();
191173

192174
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
193-
self.create_incorrect_case_diagnostic_for_func(
194-
func,
195-
fn_name_replacement,
196-
fn_param_replacements,
197-
);
175+
if let Some(fn_name_replacement) = fn_name_replacement {
176+
self.create_incorrect_case_diagnostic_for_func(func, fn_name_replacement);
177+
}
178+
198179
self.create_incorrect_case_diagnostic_for_variables(func, pats_replacements);
199180
}
200181

@@ -203,100 +184,34 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
203184
fn create_incorrect_case_diagnostic_for_func(
204185
&mut self,
205186
func: FunctionId,
206-
fn_name_replacement: Option<Replacement>,
207-
fn_param_replacements: Vec<Replacement>,
187+
fn_name_replacement: Replacement,
208188
) {
209-
// XXX: only look at sources if we do have incorrect names
210-
if fn_name_replacement.is_none() && fn_param_replacements.is_empty() {
211-
return;
212-
}
213-
214189
let fn_loc = func.lookup(self.db.upcast());
215190
let fn_src = fn_loc.source(self.db.upcast());
216191

217192
// Diagnostic for function name.
218-
if let Some(replacement) = fn_name_replacement {
219-
let ast_ptr = match fn_src.value.name() {
220-
Some(name) => name,
221-
None => {
222-
never!(
223-
"Replacement ({:?}) was generated for a function without a name: {:?}",
224-
replacement,
225-
fn_src
226-
);
227-
return;
228-
}
229-
};
230-
231-
let diagnostic = IncorrectCase {
232-
file: fn_src.file_id,
233-
ident_type: IdentType::Function,
234-
ident: AstPtr::new(&ast_ptr),
235-
expected_case: replacement.expected_case,
236-
ident_text: replacement.current_name.to_string(),
237-
suggested_text: replacement.suggested_text,
238-
};
239-
240-
self.sink.push(diagnostic);
241-
}
242-
243-
// Diagnostics for function params.
244-
let fn_params_list = match fn_src.value.param_list() {
245-
Some(params) => params,
193+
let ast_ptr = match fn_src.value.name() {
194+
Some(name) => name,
246195
None => {
247-
always!(
248-
fn_param_replacements.is_empty(),
249-
"Replacements ({:?}) were generated for a function parameters which had no parameters list: {:?}",
250-
fn_param_replacements,
196+
never!(
197+
"Replacement ({:?}) was generated for a function without a name: {:?}",
198+
fn_name_replacement,
251199
fn_src
252200
);
253201
return;
254202
}
255203
};
256-
let mut fn_params_iter = fn_params_list.params();
257-
for param_to_rename in fn_param_replacements {
258-
// We assume that parameters in replacement are in the same order as in the
259-
// actual params list, but just some of them (ones that named correctly) are skipped.
260-
let ast_ptr: ast::Name = loop {
261-
match fn_params_iter.next() {
262-
Some(element) => {
263-
if let Some(ast::Pat::IdentPat(pat)) = element.pat() {
264-
if pat.to_string() == param_to_rename.current_name.to_string() {
265-
if let Some(name) = pat.name() {
266-
break name;
267-
}
268-
// This is critical. If we consider this parameter the expected one,
269-
// it **must** have a name.
270-
never!(
271-
"Pattern {:?} equals to expected replacement {:?}, but has no name",
272-
element,
273-
param_to_rename
274-
);
275-
return;
276-
}
277-
}
278-
}
279-
None => {
280-
never!(
281-
"Replacement ({:?}) was generated for a function parameter which was not found: {:?}",
282-
param_to_rename, fn_src
283-
);
284-
return;
285-
}
286-
}
287-
};
288204

289-
let diagnostic = IncorrectCase {
290-
file: fn_src.file_id,
291-
ident_type: IdentType::Argument,
292-
ident: AstPtr::new(&ast_ptr),
293-
expected_case: param_to_rename.expected_case,
294-
ident_text: param_to_rename.current_name.to_string(),
295-
suggested_text: param_to_rename.suggested_text,
296-
};
205+
let diagnostic = IncorrectCase {
206+
file: fn_src.file_id,
207+
ident_type: IdentType::Function,
208+
ident: AstPtr::new(&ast_ptr),
209+
expected_case: fn_name_replacement.expected_case,
210+
ident_text: fn_name_replacement.current_name.to_string(),
211+
suggested_text: fn_name_replacement.suggested_text,
212+
};
297213

298-
self.sink.push(diagnostic);
299-
}
214+
self.sink.push(diagnostic);
300215
}
301216

302217
/// Given the information about incorrect variable names, looks up into the source code
@@ -327,20 +242,25 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
327242
None => continue,
328243
};
329244

245+
let is_param = ast::Param::can_cast(parent.kind());
246+
330247
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
331248
// because e.g. match arms are patterns as well.
332249
// In other words, we check that it's a named variable binding.
333250
let is_binding = ast::LetStmt::can_cast(parent.kind())
334251
|| (ast::MatchArm::can_cast(parent.kind())
335252
&& ident_pat.at_token().is_some());
336-
if !is_binding {
253+
if !(is_param || is_binding) {
337254
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
338255
continue;
339256
}
340257

258+
let ident_type =
259+
if is_param { IdentType::Argument } else { IdentType::Variable };
260+
341261
let diagnostic = IncorrectCase {
342262
file: source_ptr.file_id,
343-
ident_type: IdentType::Variable,
263+
ident_type,
344264
ident: AstPtr::new(&name_ast),
345265
expected_case: replacement.expected_case,
346266
ident_text: replacement.current_name.to_string(),
@@ -408,7 +328,7 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
408328
struct_name_replacement: Option<Replacement>,
409329
struct_fields_replacements: Vec<Replacement>,
410330
) {
411-
// XXX: only look at sources if we do have incorrect names
331+
// XXX: Only look at sources if we do have incorrect names.
412332
if struct_name_replacement.is_none() && struct_fields_replacements.is_empty() {
413333
return;
414334
}
@@ -1037,4 +957,9 @@ fn qualify() {
1037957
"#,
1038958
)
1039959
}
960+
961+
#[test] // Issue #8809.
962+
fn parenthesized_parameter() {
963+
check_diagnostics(r#"fn f((O): _) {}"#)
964+
}
1040965
}

0 commit comments

Comments
 (0)