Skip to content

Commit 6b76d82

Browse files
committed
Move suggestion code to its own method
1 parent 33b0636 commit 6b76d82

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -376,48 +376,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
376376
};
377377
err.span_label(pattern.span, msg);
378378
} else if let Some(e) = local_visitor.found_method_call {
379-
if let ExprKind::MethodCall(segment, _call_sp, _args) = &e.kind {
380-
if let (Ok(snippet), Some(tables), None) = (
381-
self.tcx.sess.source_map().span_to_snippet(segment.ident.span),
382-
self.in_progress_tables,
383-
&segment.args,
384-
) {
385-
let borrow = tables.borrow();
386-
let sigs = borrow.node_method_sig();
387-
if let Some(sig) = sigs.get(e.hir_id) {
388-
let mut params = vec![];
389-
for arg in sig.inputs_and_output().skip_binder().iter() {
390-
if let ty::Param(param) = arg.kind {
391-
if param.name != kw::SelfUpper {
392-
let name = param.name.to_string();
393-
if !params.contains(&name) {
394-
params.push(name);
395-
}
396-
}
397-
}
398-
}
399-
if !params.is_empty() {
400-
err.span_suggestion(
401-
segment.ident.span,
402-
&format!(
403-
"consider specifying the type argument{} in the method call",
404-
if params.len() > 1 {
405-
"s"
406-
} else {
407-
""
408-
},
409-
),
410-
format!("{}::<{}>", snippet, params.join(", ")),
411-
Applicability::HasPlaceholders,
412-
);
413-
} else {
414-
err.span_label(e.span, &format!(
415-
"this method call resolves to `{:?}`",
416-
sig.output().skip_binder(),
417-
));
418-
}
419-
}
420-
}
379+
if let ExprKind::MethodCall(segment, ..) = &e.kind {
380+
// Suggest specifiying type params or point out the return type of the call.
381+
self.annotate_method_call(segment, e, &mut err);
421382
}
422383
}
423384
// Instead of the following:
@@ -447,6 +408,57 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
447408
err
448409
}
449410

411+
/// If the `FnSig` for the method call can be found and type arguments are identified as
412+
/// needed, suggest annotating the call, otherwise point out the resulting type of the call.
413+
fn annotate_method_call(
414+
&self,
415+
segment: &hir::ptr::P<hir::PathSegment>,
416+
e: &Expr,
417+
err: &mut DiagnosticBuilder<'_>,
418+
) {
419+
if let (Ok(snippet), Some(tables), None) = (
420+
self.tcx.sess.source_map().span_to_snippet(segment.ident.span),
421+
self.in_progress_tables,
422+
&segment.args,
423+
) {
424+
let borrow = tables.borrow();
425+
let sigs = borrow.node_method_sig();
426+
if let Some(sig) = sigs.get(e.hir_id) {
427+
let mut params = vec![];
428+
for arg in sig.inputs_and_output().skip_binder().iter() {
429+
if let ty::Param(param) = arg.kind {
430+
if param.name != kw::SelfUpper {
431+
let name = param.name.to_string();
432+
if !params.contains(&name) {
433+
params.push(name);
434+
}
435+
}
436+
}
437+
}
438+
if !params.is_empty() {
439+
err.span_suggestion(
440+
segment.ident.span,
441+
&format!(
442+
"consider specifying the type argument{} in the method call",
443+
if params.len() > 1 {
444+
"s"
445+
} else {
446+
""
447+
},
448+
),
449+
format!("{}::<{}>", snippet, params.join(", ")),
450+
Applicability::HasPlaceholders,
451+
);
452+
} else {
453+
err.span_label(e.span, &format!(
454+
"this method call resolves to `{:?}`",
455+
sig.output().skip_binder(),
456+
));
457+
}
458+
}
459+
}
460+
}
461+
450462
pub fn need_type_info_err_in_generator(
451463
&self,
452464
kind: hir::GeneratorKind,

0 commit comments

Comments
 (0)