Suggest calling ctor when trait is unimplemented

This commit is contained in:
Michael Goulet 2022-10-09 21:02:12 +00:00
parent a24a020e6d
commit 63be7a2424
3 changed files with 59 additions and 1 deletions

View file

@ -817,7 +817,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let (def_id, output_ty, callable) = match *self_ty.kind() {
ty::Closure(def_id, substs) => (def_id, substs.as_closure().sig().output(), "closure"),
ty::FnDef(def_id, _) => (def_id, self_ty.fn_sig(self.tcx).output(), "function"),
ty::FnDef(def_id, _) => (
def_id,
self_ty.fn_sig(self.tcx).output(),
match self.tcx.def_kind(def_id) {
DefKind::Ctor(..) => "constructor",
_ => "function",
},
),
_ => return false,
};
let msg = format!("use parentheses to call the {}", callable);
@ -878,6 +885,16 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let sugg = format!("({})", args);
(format!("{}{}", ident, sugg), sugg)
}
Some(hir::Node::Ctor(data)) => {
let name = self.tcx.def_path_str(def_id);
err.span_label(
self.tcx.def_span(def_id),
format!("consider calling the constructor for `{}`", name),
);
let args = data.fields().iter().map(|_| "_").collect::<Vec<_>>().join(", ");
let sugg = format!("({})", args);
(format!("{name}{sugg}"), sugg)
}
_ => return false,
};
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. })