]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/traits/error_reporting.rs
Account for `rustc_on_unimplemented`
[rust.git] / src / librustc / traits / error_reporting.rs
index f511862b37b84e2fb5f297276b250a9b727c66cd..1321d5348a29962b4f45fb7fbbfd528343eda379 100644 (file)
@@ -771,6 +771,17 @@ pub fn report_selection_error(
                                 )
                             };
 
+                        if self.suggest_add_reference_to_arg(
+                            &obligation,
+                            &mut err,
+                            &trait_ref,
+                            points_at_arg,
+                            have_alt_message,
+                        ) {
+                            self.note_obligation_cause(&mut err, obligation);
+                            err.emit();
+                            return;
+                        }
                         if let Some(ref s) = label {
                             // If it has a custom `#[rustc_on_unimplemented]`
                             // error message, let's display it as the label!
@@ -787,16 +798,6 @@ pub fn report_selection_error(
                         self.suggest_borrow_on_unsized_slice(&obligation.cause.code, &mut err);
                         self.suggest_fn_call(&obligation, &mut err, &trait_ref, points_at_arg);
                         self.suggest_remove_reference(&obligation, &mut err, &trait_ref);
-                        if self.suggest_add_reference_to_arg(
-                            &obligation,
-                            &mut err,
-                            &trait_ref,
-                            points_at_arg,
-                        ) {
-                            self.note_obligation_cause(&mut err, obligation);
-                            err.emit();
-                            return;
-                        }
                         self.suggest_semicolon_removal(&obligation, &mut err, span, &trait_ref);
 
                         // Try to report a help message
@@ -1318,6 +1319,7 @@ fn suggest_add_reference_to_arg(
         err: &mut DiagnosticBuilder<'tcx>,
         trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
         points_at_arg: bool,
+        has_custom_message: bool,
     ) -> bool {
         if !points_at_arg {
             return false;
@@ -1346,19 +1348,25 @@ fn suggest_add_reference_to_arg(
                     // original type obligation, not the last one that failed, which is arbitrary.
                     // Because of this, we modify the error to refer to the original obligation and
                     // return early in the caller.
-                    err.message = vec![(
-                        format!(
-                            "the trait bound `{}: {}` is not satisfied",
-                            found,
-                            obligation.parent_trait_ref.skip_binder(),
-                        ),
-                        Style::NoStyle,
-                    )];
+                    let msg = format!(
+                        "the trait bound `{}: {}` is not satisfied",
+                        found,
+                        obligation.parent_trait_ref.skip_binder(),
+                    );
+                    if has_custom_message {
+                        err.note(&msg);
+                    } else {
+                        err.message = vec![(msg, Style::NoStyle)];
+                    }
                     if snippet.starts_with('&') {
                         // This is already a literal borrow and the obligation is failing
                         // somewhere else in the obligation chain. Do not suggest non-sense.
                         return false;
                     }
+                    err.span_label(span, &format!(
+                        "expected an implementor of trait `{}`",
+                        obligation.parent_trait_ref.skip_binder(),
+                    ));
                     err.span_suggestion(
                         span,
                         "consider borrowing here",