Add suggestion for impl_candidates with E0283
Update compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
This commit is contained in:
parent
86e0ff47a0
commit
db95b5ca9b
4 changed files with 55 additions and 7 deletions
|
@ -429,6 +429,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
body_id: Option<hir::BodyId>,
|
body_id: Option<hir::BodyId>,
|
||||||
span: Span,
|
span: Span,
|
||||||
arg: GenericArg<'tcx>,
|
arg: GenericArg<'tcx>,
|
||||||
|
impl_candidates: Vec<ty::TraitRef<'tcx>>,
|
||||||
error_code: TypeAnnotationNeeded,
|
error_code: TypeAnnotationNeeded,
|
||||||
) -> DiagnosticBuilder<'tcx> {
|
) -> DiagnosticBuilder<'tcx> {
|
||||||
let arg = self.resolve_vars_if_possible(arg);
|
let arg = self.resolve_vars_if_possible(arg);
|
||||||
|
@ -653,7 +654,44 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
};
|
};
|
||||||
err.span_label(pattern.span, msg);
|
err.span_label(pattern.span, msg);
|
||||||
} else if let Some(e) = local_visitor.found_method_call {
|
} else if let Some(e) = local_visitor.found_method_call {
|
||||||
if let ExprKind::MethodCall(segment, ..) = &e.kind {
|
if let ExprKind::MethodCall(segment, _, exprs, _) = &e.kind {
|
||||||
|
// Suggest impl candidates:
|
||||||
|
//
|
||||||
|
// error[E0283]: type annotations needed
|
||||||
|
// --> $DIR/E0283.rs:35:24
|
||||||
|
// |
|
||||||
|
// LL | let bar = foo_impl.into() * 1u32;
|
||||||
|
// | ---------^^^^--
|
||||||
|
// | | |
|
||||||
|
// | | cannot infer type for type parameter `T` declared on the trait `Into`
|
||||||
|
// | this method call resolves to `T`
|
||||||
|
// | help: specify type like: `<Impl as Into<u32>>::into(foo_impl)`
|
||||||
|
// |
|
||||||
|
// = note: cannot satisfy `Impl: Into<_>`
|
||||||
|
if !impl_candidates.is_empty() && e.span.contains(span) {
|
||||||
|
if let Some(expr) = exprs.first() {
|
||||||
|
if let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind {
|
||||||
|
if let [path_segment] = &path.segments[..] {
|
||||||
|
let candidate_len = impl_candidates.len();
|
||||||
|
let suggestions = impl_candidates.iter().map(|candidate| {
|
||||||
|
format!(
|
||||||
|
"{}::{}({})",
|
||||||
|
candidate, segment.ident, path_segment.ident
|
||||||
|
)
|
||||||
|
});
|
||||||
|
err.span_suggestions(
|
||||||
|
e.span,
|
||||||
|
&format!(
|
||||||
|
"use the fully qualified path for the potential candidate{}",
|
||||||
|
pluralize!(candidate_len),
|
||||||
|
),
|
||||||
|
suggestions,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
// Suggest specifying type params or point out the return type of the call:
|
// Suggest specifying type params or point out the return type of the call:
|
||||||
//
|
//
|
||||||
// error[E0282]: type annotations needed
|
// error[E0282]: type annotations needed
|
||||||
|
|
|
@ -1498,11 +1498,18 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
// check upstream for type errors and don't add the obligations to
|
// check upstream for type errors and don't add the obligations to
|
||||||
// begin with in those cases.
|
// begin with in those cases.
|
||||||
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
|
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
|
||||||
self.emit_inference_failure_err(body_id, span, subst, ErrorCode::E0282).emit();
|
self.emit_inference_failure_err(body_id, span, subst, vec![], ErrorCode::E0282)
|
||||||
|
.emit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mut err =
|
let impl_candidates = self.find_similar_impl_candidates(trait_ref);
|
||||||
self.emit_inference_failure_err(body_id, span, subst, ErrorCode::E0283);
|
let mut err = self.emit_inference_failure_err(
|
||||||
|
body_id,
|
||||||
|
span,
|
||||||
|
subst,
|
||||||
|
impl_candidates,
|
||||||
|
ErrorCode::E0283,
|
||||||
|
);
|
||||||
err.note(&format!("cannot satisfy `{}`", predicate));
|
err.note(&format!("cannot satisfy `{}`", predicate));
|
||||||
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
|
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
|
||||||
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
|
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
|
||||||
|
@ -1566,7 +1573,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.emit_inference_failure_err(body_id, span, arg, ErrorCode::E0282)
|
self.emit_inference_failure_err(body_id, span, arg, vec![], ErrorCode::E0282)
|
||||||
}
|
}
|
||||||
|
|
||||||
ty::PredicateKind::Subtype(data) => {
|
ty::PredicateKind::Subtype(data) => {
|
||||||
|
@ -1577,7 +1584,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
let SubtypePredicate { a_is_expected: _, a, b } = data;
|
let SubtypePredicate { a_is_expected: _, a, b } = data;
|
||||||
// both must be type variables, or the other would've been instantiated
|
// both must be type variables, or the other would've been instantiated
|
||||||
assert!(a.is_ty_var() && b.is_ty_var());
|
assert!(a.is_ty_var() && b.is_ty_var());
|
||||||
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282)
|
self.emit_inference_failure_err(body_id, span, a.into(), vec![], ErrorCode::E0282)
|
||||||
}
|
}
|
||||||
ty::PredicateKind::Projection(data) => {
|
ty::PredicateKind::Projection(data) => {
|
||||||
let trait_ref = bound_predicate.rebind(data).to_poly_trait_ref(self.tcx);
|
let trait_ref = bound_predicate.rebind(data).to_poly_trait_ref(self.tcx);
|
||||||
|
@ -1592,6 +1599,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
body_id,
|
body_id,
|
||||||
span,
|
span,
|
||||||
self_ty.into(),
|
self_ty.into(),
|
||||||
|
vec![],
|
||||||
ErrorCode::E0284,
|
ErrorCode::E0284,
|
||||||
);
|
);
|
||||||
err.note(&format!("cannot satisfy `{}`", predicate));
|
err.note(&format!("cannot satisfy `{}`", predicate));
|
||||||
|
|
|
@ -1479,7 +1479,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
ty
|
ty
|
||||||
} else {
|
} else {
|
||||||
if !self.is_tainted_by_errors() {
|
if !self.is_tainted_by_errors() {
|
||||||
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282)
|
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), vec![], E0282)
|
||||||
.note("type must be known at this point")
|
.note("type must be known at this point")
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -694,6 +694,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
|
||||||
Some(self.body.id()),
|
Some(self.body.id()),
|
||||||
self.span.to_span(self.tcx),
|
self.span.to_span(self.tcx),
|
||||||
t.into(),
|
t.into(),
|
||||||
|
vec![],
|
||||||
E0282,
|
E0282,
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
|
@ -707,6 +708,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
|
||||||
Some(self.body.id()),
|
Some(self.body.id()),
|
||||||
self.span.to_span(self.tcx),
|
self.span.to_span(self.tcx),
|
||||||
c.into(),
|
c.into(),
|
||||||
|
vec![],
|
||||||
E0282,
|
E0282,
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue