Skip to content

Commit b3b206f

Browse files
committed
move code to method outside of happy path
1 parent ac3d4cc commit b3b206f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/librustc/traits/error_reporting.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -2076,25 +2076,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20762076
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
20772077
err.note(&format!("cannot resolve `{}`", predicate));
20782078
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
2079-
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
2080-
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
2081-
err.note(&format!(
2082-
"{}s cannot be accessed directly on a `trait`, they can only be \
2083-
accessed through a specific `impl`",
2084-
assoc_item.kind.suggestion_descr(),
2085-
));
2086-
err.span_suggestion(
2087-
span,
2088-
"use the fully qualified path to an implementation",
2089-
format!(
2090-
"<Type as {}>::{}",
2091-
self.tcx.def_path_str(trait_ref.def_id()),
2092-
assoc_item.ident
2093-
),
2094-
Applicability::HasPlaceholders,
2095-
);
2096-
}
2097-
}
2079+
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
20982080
} else if let (
20992081
Ok(ref snippet),
21002082
ObligationCauseCode::BindingObligation(ref def_id, _),
@@ -2196,6 +2178,30 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21962178
err.emit();
21972179
}
21982180

2181+
fn suggest_fully_qualified_path(
2182+
&self,
2183+
err: &mut DiagnosticBuilder<'_>,
2184+
def_id: DefId,
2185+
span: Span,
2186+
trait_ref: DefId,
2187+
) {
2188+
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) {
2189+
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
2190+
err.note(&format!(
2191+
"{}s cannot be accessed directly on a `trait`, they can only be \
2192+
accessed through a specific `impl`",
2193+
assoc_item.kind.suggestion_descr(),
2194+
));
2195+
err.span_suggestion(
2196+
span,
2197+
"use the fully qualified path to an implementation",
2198+
format!("<Type as {}>::{}", self.tcx.def_path_str(trait_ref), assoc_item.ident),
2199+
Applicability::HasPlaceholders,
2200+
);
2201+
}
2202+
}
2203+
}
2204+
21992205
/// Returns `true` if the trait predicate may apply for *some* assignment
22002206
/// to the type parameters.
22012207
fn predicate_can_apply(

0 commit comments

Comments
 (0)