]> git.lizzy.rs Git - rust.git/commitdiff
Conserve cause of `ImplDerivedObligation` in E0599
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 11 Jan 2023 03:07:14 +0000 (03:07 +0000)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 11 Jan 2023 19:31:33 +0000 (19:31 +0000)
CC #86377.

compiler/rustc_hir_typeck/src/method/probe.rs
compiler/rustc_hir_typeck/src/method/suggest.rs
tests/ui/const-generics/generic_const_exprs/issue-69654.stderr
tests/ui/const-generics/generic_const_exprs/issue-80742.stderr
tests/ui/derives/issue-91492.stderr
tests/ui/derives/issue-91550.stderr
tests/ui/generic-associated-types/method-unsatified-assoc-type-predicate.rs
tests/ui/impl-trait/issues/issue-62742.stderr
tests/ui/methods/method-not-found-generic-arg-elision.stderr
tests/ui/suggestions/derive-trait-for-method-call.stderr

index 5d8383170f0dcd7508acc8a2a78a7359206faf70..63067deb7b07b79eacf7925f5a3f671fe7ffcb1f 100644 (file)
@@ -1556,7 +1556,23 @@ fn consider_probe(
 
                     // Convert the bounds into obligations.
                     let impl_obligations = traits::predicates_for_generics(
-                        |_, _| cause.clone(),
+                        |_idx, span| {
+                            let misc = traits::ObligationCause::misc(span, self.body_id);
+                            let parent_trait_pred = ty::Binder::dummy(ty::TraitPredicate {
+                                trait_ref: ty::TraitRef::from_method(self.tcx, impl_def_id, substs),
+                                constness: ty::BoundConstness::NotConst,
+                                polarity: ty::ImplPolarity::Positive,
+                            });
+                            misc.derived_cause(parent_trait_pred, |derived| {
+                                traits::ImplDerivedObligation(Box::new(
+                                    traits::ImplDerivedObligationCause {
+                                        derived,
+                                        impl_def_id,
+                                        span,
+                                    },
+                                ))
+                            })
+                        },
                         self.param_env,
                         impl_bounds,
                     );
index 62e80659486a870429503b19c32e3113f2b37a6a..8c39e4413c534a9e53caf240bbb51da3df8aa13e 100644 (file)
@@ -101,6 +101,7 @@ fn is_slice_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
         self.autoderef(span, ty).any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
     }
 
+    #[instrument(level = "debug", skip(self))]
     pub fn report_method_error(
         &self,
         span: Span,
@@ -587,21 +588,18 @@ pub fn report_no_match_method_error(
             // Find all the requirements that come from a local `impl` block.
             let mut skip_list: FxHashSet<_> = Default::default();
             let mut spanned_predicates: FxHashMap<MultiSpan, _> = Default::default();
-            for (data, p, parent_p, impl_def_id, cause) in unsatisfied_predicates
+            for (p, parent_p, impl_def_id, cause) in unsatisfied_predicates
                 .iter()
                 .filter_map(|(p, parent, c)| c.as_ref().map(|c| (p, parent, c)))
                 .filter_map(|(p, parent, c)| match c.code() {
-                    ObligationCauseCode::ImplDerivedObligation(data) => {
-                        Some((&data.derived, p, parent, data.impl_def_id, data))
+                    ObligationCauseCode::ImplDerivedObligation(data)
+                        if matches!(p.kind().skip_binder(), ty::PredicateKind::Clause(_)) =>
+                    {
+                        Some((p, parent, data.impl_def_id, data))
                     }
                     _ => None,
                 })
             {
-                let parent_trait_ref = data.parent_trait_pred;
-                let path = parent_trait_ref.print_modifiers_and_trait_path();
-                let tr_self_ty = parent_trait_ref.skip_binder().self_ty();
-                let unsatisfied_msg = "unsatisfied trait bound introduced here";
-                let derive_msg = "unsatisfied trait bound introduced in this `derive` macro";
                 match self.tcx.hir().get_if_local(impl_def_id) {
                     // Unmet obligation comes from a `derive` macro, point at it once to
                     // avoid multiple span labels pointing at the same place.
@@ -618,9 +616,12 @@ pub fn report_no_match_method_error(
                     {
                         let span = self_ty.span.ctxt().outer_expn_data().call_site;
                         let mut spans: MultiSpan = span.into();
-                        spans.push_span_label(span, derive_msg);
+                        spans.push_span_label(
+                            span,
+                            "unsatisfied trait bound introduced in this `derive` macro",
+                        );
                         let entry = spanned_predicates.entry(spans);
-                        entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
+                        entry.or_insert_with(|| Vec::new()).push(p);
                     }
 
                     // Unmet obligation coming from an `impl`.
@@ -647,8 +648,7 @@ pub fn report_no_match_method_error(
                                 };
                                 err.span_suggestion_verbose(
                                     sp,
-                                    "consider relaxing the type parameter's implicit \
-                                     `Sized` bound",
+                                    "consider relaxing the type parameter's implicit `Sized` bound",
                                     sugg,
                                     Applicability::MachineApplicable,
                                 );
@@ -661,7 +661,10 @@ pub fn report_no_match_method_error(
                         skip_list.insert(p);
                         let mut spans = if cause.span != *item_span {
                             let mut spans: MultiSpan = cause.span.into();
-                            spans.push_span_label(cause.span, unsatisfied_msg);
+                            spans.push_span_label(
+                                cause.span,
+                                "unsatisfied trait bound introduced here",
+                            );
                             spans
                         } else {
                             let mut spans = Vec::with_capacity(2);
@@ -677,7 +680,7 @@ pub fn report_no_match_method_error(
                         spans.push_span_label(self_ty.span, "");
 
                         let entry = spanned_predicates.entry(spans);
-                        entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
+                        entry.or_insert_with(|| Vec::new()).push(p);
                     }
                     Some(Node::Item(hir::Item {
                         kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
@@ -694,11 +697,11 @@ pub fn report_no_match_method_error(
                 }
             }
             let mut spanned_predicates: Vec<_> = spanned_predicates.into_iter().collect();
-            spanned_predicates.sort_by_key(|(span, (_, _, _))| span.primary_span());
-            for (span, (_path, _self_ty, preds)) in spanned_predicates {
-                let mut preds: Vec<_> = preds
-                    .into_iter()
-                    .filter_map(|pred| format_pred(*pred))
+            spanned_predicates.sort_by_key(|(span, _)| span.primary_span());
+            for (span, predicates) in spanned_predicates {
+                let mut preds: Vec<_> = predicates
+                    .iter()
+                    .filter_map(|pred| format_pred(**pred))
                     .map(|(p, _)| format!("`{}`", p))
                     .collect();
                 preds.sort();
index c3e2f8e1646f12dd9c0e6956e46001c9fa599020..eb4ff8305dac898011449358b8d822de6e9eac27 100644 (file)
@@ -15,8 +15,14 @@ LL | struct Foo<const N: usize> {}
 LL |     Foo::foo();
    |          ^^^ function or associated item cannot be called on `Foo<_>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `[u8; _]: Bar<[(); _]>`
+note: trait bound `[u8; _]: Bar<[(); _]>` was not satisfied
+  --> $DIR/issue-69654.rs:11:14
+   |
+LL | impl<const N: usize> Foo<N>
+   |                      ------
+LL | where
+LL |     [u8; N]: Bar<[(); N]>,
+   |              ^^^^^^^^^^^^ unsatisfied trait bound introduced here
 
 error: aborting due to 2 previous errors
 
index a08c9912527c732eb8de4b647310e3c2628d019d..6aa8ee13b790f57cd8dd8ba2dff2ba475ea28335 100644 (file)
@@ -23,8 +23,17 @@ LL |     let dst = Inline::<dyn Debug>::new(0);
    |
    = note: doesn't satisfy `dyn Debug: Sized`
    |
-   = note: the following trait bounds were not satisfied:
-           `dyn Debug: Sized`
+note: trait bound `dyn Debug: Sized` was not satisfied
+  --> $DIR/issue-80742.rs:20:6
+   |
+LL | impl<T> Inline<T>
+   |      ^  ---------
+   |      |
+   |      unsatisfied trait bound introduced here
+help: consider relaxing the type parameter's implicit `Sized` bound
+   |
+LL | impl<T: ?Sized> Inline<T>
+   |       ++++++++
 
 error[E0080]: evaluation of `Inline::<dyn std::fmt::Debug>::{constant#0}` failed
   --> $SRC_DIR/core/src/mem/mod.rs:LL:COL
index fbd48336d9126532ee6eb55e115deb088bd22cba..cee30ac50a6a2ed1335a379ae6aaefe29c362d39 100644 (file)
@@ -42,8 +42,13 @@ LL | struct Object<T, A>(T, A);
 LL |     foo.use_clone();
    |         ^^^^^^^^^ method cannot be called on `Object<NoDerives, SomeDerives>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `NoDerives: Clone`
+note: trait bound `NoDerives: Clone` was not satisfied
+  --> $DIR/issue-91492.rs:18:9
+   |
+LL | impl<T: Clone, A: Default> Object<T, A> {
+   |         ^^^^^              ------------
+   |         |
+   |         unsatisfied trait bound introduced here
 help: consider annotating `NoDerives` with `#[derive(Clone)]`
    |
 LL | #[derive(Clone)]
index 12be269565da1233aa52cadb950d9a8d4f78278e..e8d67ccb3592b7e1ee8d64d934838ba418a23c37 100644 (file)
@@ -30,8 +30,13 @@ LL | struct Object<T>(T);
 LL |     foo.use_eq();
    |         ^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `NoDerives: Eq`
+note: trait bound `NoDerives: Eq` was not satisfied
+  --> $DIR/issue-91550.rs:15:9
+   |
+LL | impl<T: Eq> Object<T> {
+   |         ^^  ---------
+   |         |
+   |         unsatisfied trait bound introduced here
 help: consider annotating `NoDerives` with `#[derive(Eq, PartialEq)]`
    |
 LL | #[derive(Eq, PartialEq)]
@@ -49,8 +54,13 @@ LL | struct Object<T>(T);
 LL |     foo.use_ord();
    |         ^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `NoDerives: Ord`
+note: trait bound `NoDerives: Ord` was not satisfied
+  --> $DIR/issue-91550.rs:18:9
+   |
+LL | impl<T: Ord> Object<T> {
+   |         ^^^  ---------
+   |         |
+   |         unsatisfied trait bound introduced here
 help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
    |
 LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
@@ -71,9 +81,20 @@ LL | struct Object<T>(T);
 LL |     foo.use_ord_and_partial_ord();
    |         ^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `Object<NoDerives>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `NoDerives: Ord`
-           `NoDerives: PartialOrd`
+note: trait bound `NoDerives: Ord` was not satisfied
+  --> $DIR/issue-91550.rs:21:9
+   |
+LL | impl<T: Ord + PartialOrd> Object<T> {
+   |         ^^^               ---------
+   |         |
+   |         unsatisfied trait bound introduced here
+note: trait bound `NoDerives: PartialOrd` was not satisfied
+  --> $DIR/issue-91550.rs:21:15
+   |
+LL | impl<T: Ord + PartialOrd> Object<T> {
+   |               ^^^^^^^^^^  ---------
+   |               |
+   |               unsatisfied trait bound introduced here
 help: consider annotating `NoDerives` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
    |
 LL | #[derive(Eq, Ord, PartialEq, PartialOrd)]
index 36974b3df5e6442e497dffe9d306a96f5868f4d2..83655341d6a243756412fcf1a2369eaf324f5e94 100644 (file)
@@ -11,7 +11,7 @@ fn f(&self) {}
 
 impl<T: X<Y<i32> = i32>> M for T {}
 //~^ NOTE trait bound `<S as X>::Y<i32> = i32` was not satisfied
-//~| NOTE unsatisfied trait bound introduced here
+//~| NOTE
 //~| NOTE
 //~| NOTE
 
index bc342dc46893b80e02778235fc7437037bb9d406..d872291c87054e2aaf11c7e6d8ea97624d3a6ef4 100644 (file)
@@ -23,8 +23,13 @@ LL | pub struct RawImpl<T>(PhantomData<T>);
 LL | pub struct SafeImpl<T: ?Sized, A: Raw<T>>(PhantomData<(A, T)>);
    | ----------------------------------------- function or associated item `foo` not found for this struct
    |
-   = note: the following trait bounds were not satisfied:
-           `RawImpl<()>: Raw<()>`
+note: trait bound `RawImpl<()>: Raw<()>` was not satisfied
+  --> $DIR/issue-62742.rs:28:20
+   |
+LL | impl<T: ?Sized, A: Raw<T>> SafeImpl<T, A> {
+   |                    ^^^^^^  --------------
+   |                    |
+   |                    unsatisfied trait bound introduced here
 note: the trait `Raw` must be implemented
   --> $DIR/issue-62742.rs:12:1
    |
index 8846efba871b9cd1f845b86b58dfbaa5a0880d1b..6ec369644a056f7c2286eb18323e83c05b027203 100644 (file)
@@ -88,9 +88,20 @@ LL | struct Struct<T> {
 LL |     s.method();
    |       ^^^^^^ method cannot be called on `Struct<f64>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `f64: Eq`
-           `f64: Ord`
+note: trait bound `f64: Eq` was not satisfied
+  --> $DIR/method-not-found-generic-arg-elision.rs:74:36
+   |
+LL | impl<T: Clone + Copy + PartialEq + Eq + PartialOrd + Ord> Struct<T> {
+   |                                    ^^                     ---------
+   |                                    |
+   |                                    unsatisfied trait bound introduced here
+note: trait bound `f64: Ord` was not satisfied
+  --> $DIR/method-not-found-generic-arg-elision.rs:74:54
+   |
+LL | impl<T: Clone + Copy + PartialEq + Eq + PartialOrd + Ord> Struct<T> {
+   |                                                      ^^^  ---------
+   |                                                      |
+   |                                                      unsatisfied trait bound introduced here
 
 error: aborting due to 9 previous errors
 
index 14e8a2675dd18dca6b2dc57fc4c4a973d1d2ebee..8d5957bd7191c42f87a72ab2443098fa6e9d0d54 100644 (file)
@@ -16,10 +16,27 @@ LL | struct Foo<X, Y> (X, Y);
 LL |     let y = x.test();
    |               ^^^^ method cannot be called on `Foo<Enum, CloneEnum>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `Enum: Clone`
-           `Enum: Default`
-           `CloneEnum: Default`
+note: trait bound `Enum: Clone` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:9
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |         ^^^^^                                   ---------
+   |         |
+   |         unsatisfied trait bound introduced here
+note: trait bound `Enum: Default` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:17
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |                 ^^^^^^^                         ---------
+   |                 |
+   |                 unsatisfied trait bound introduced here
+note: trait bound `CloneEnum: Default` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:40
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |                                        ^^^^^^^  ---------
+   |                                        |
+   |                                        unsatisfied trait bound introduced here
 note: the trait `Default` must be implemented
   --> $SRC_DIR/core/src/default.rs:LL:COL
 help: consider annotating `Enum` with `#[derive(Clone)]`
@@ -45,10 +62,27 @@ LL | struct Foo<X, Y> (X, Y);
 LL |     let y = x.test();
    |               ^^^^ method cannot be called on `Foo<Struct, CloneStruct>` due to unsatisfied trait bounds
    |
-   = note: the following trait bounds were not satisfied:
-           `Struct: Clone`
-           `Struct: Default`
-           `CloneStruct: Default`
+note: trait bound `Struct: Clone` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:9
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |         ^^^^^                                   ---------
+   |         |
+   |         unsatisfied trait bound introduced here
+note: trait bound `Struct: Default` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:17
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |                 ^^^^^^^                         ---------
+   |                 |
+   |                 unsatisfied trait bound introduced here
+note: trait bound `CloneStruct: Default` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:40
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |                                        ^^^^^^^  ---------
+   |                                        |
+   |                                        unsatisfied trait bound introduced here
 help: consider annotating `CloneStruct` with `#[derive(Default)]`
    |
 LL | #[derive(Default)]
@@ -73,9 +107,20 @@ LL |     let y = x.test();
    |
    = note: doesn't satisfy `Vec<Enum>: Clone`
    |
-   = note: the following trait bounds were not satisfied:
-           `Vec<Enum>: Clone`
-           `Instant: Default`
+note: trait bound `Vec<Enum>: Clone` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:9
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |         ^^^^^                                   ---------
+   |         |
+   |         unsatisfied trait bound introduced here
+note: trait bound `Instant: Default` was not satisfied
+  --> $DIR/derive-trait-for-method-call.rs:20:40
+   |
+LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
+   |                                        ^^^^^^^  ---------
+   |                                        |
+   |                                        unsatisfied trait bound introduced here
 
 error: aborting due to 3 previous errors