move code to method outside of happy path

This commit is contained in:
Esteban Küber 2020-01-08 10:10:51 -08:00
parent ac3d4cccea
commit b3b206f6bd
1 changed files with 25 additions and 19 deletions

View File

@ -2076,25 +2076,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
err.note(&format!("cannot resolve `{}`", predicate));
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \
accessed through a specific `impl`",
assoc_item.kind.suggestion_descr(),
));
err.span_suggestion(
span,
"use the fully qualified path to an implementation",
format!(
"<Type as {}>::{}",
self.tcx.def_path_str(trait_ref.def_id()),
assoc_item.ident
),
Applicability::HasPlaceholders,
);
}
}
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
} else if let (
Ok(ref snippet),
ObligationCauseCode::BindingObligation(ref def_id, _),
@ -2196,6 +2178,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err.emit();
}
fn suggest_fully_qualified_path(
&self,
err: &mut DiagnosticBuilder<'_>,
def_id: DefId,
span: Span,
trait_ref: DefId,
) {
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \
accessed through a specific `impl`",
assoc_item.kind.suggestion_descr(),
));
err.span_suggestion(
span,
"use the fully qualified path to an implementation",
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.ident),
Applicability::HasPlaceholders,
);
}
}
}
/// Returns `true` if the trait predicate may apply for *some* assignment
/// to the type parameters.
fn predicate_can_apply(