Skip to content
Permalink
Browse files

move code to method outside of happy path

  • Loading branch information
estebank committed Jan 8, 2020
1 parent ac3d4cc commit b3b206f6bdb62216bc03ec000e0a285691014703
Showing with 25 additions and 19 deletions.
  1. +25 −19 src/librustc/traits/error_reporting.rs
@@ -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(

0 comments on commit b3b206f

Please sign in to comment.
You can’t perform that action at this time.