rust/tests/ui/derives
Matthias Krüger 800221b5b8
Rollup merge of #106477 - Nathan-Fenner:nathanf/refined-error-span-trait-impl, r=compiler-errors
Refine error spans for "The trait bound `T: Trait` is not satisfied" when passing literal structs/tuples

This PR adds a new heuristic which refines the error span reported for "`T: Trait` is not satisfied" errors, by "drilling down" into individual fields of structs/enums/tuples to point to the "problematic" value.

Here's a self-contained example of the difference in error span:

```rs
struct Burrito<Filling> {
    filling: Filling,
}
impl <Filling: Delicious> Delicious for Burrito<Filling> {}
fn eat_delicious_food<Food: Delicious>(food: Food) {}
fn will_type_error() {
    eat_delicious_food(Burrito { filling: Kale });
    //                 ^~~~~~~~~~~~~~~~~~~~~~~~~ (before) The trait bound `Kale: Delicious` is not satisfied
    //                                    ^~~~   (after)  The trait bound `Kale: Delicious` is not satisfied
}
```
(kale is fine, this is just a silly food-based example)

Before this PR, the error span is identified as the entire argument to the generic function `eat_delicious_food`. However, since only `Kale` is the "problematic" part, we can point at it specifically. In particular, the primary error message itself mentions the missing `Kale: Delicious` trait bound, so it's much clearer if this part is called out explicitly.

---

The _existing_ heuristic tries to label the right function argument in `point_at_arg_if_possible`. It goes something like this:
- Look at the broken base trait `Food: Delicious` and find which generics it mentions (in this case, only `Food`)
- Look at the parameter type definitions and find which of them mention `Filling` (in this case, only `food`)
- If there is exactly one relevant parameter, label the corresponding argument with the error span, instead of the entire call

This PR extends this heuristic by further refining the resulting expression span in the new `point_at_specific_expr_if_possible` function. For each `impl` in the (broken) chain, we apply the following strategy:

The strategy to determine this span involves connecting information about our generic `impl`
with information about our (struct) type and the (struct) literal expression:
- Find the `impl` (`impl <Filling: Delicious> Delicious for Burrito<Filling>`)
  that links our obligation (`Kale: Delicious`) with the parent obligation (`Burrito<Kale>: Delicious`)
- Find the "original" predicate constraint in the impl (`Filling: Delicious`) which produced our obligation.
- Find all of the generics that are mentioned in the predicate (`Filling`).
- Examine the `Self` type in the `impl`, and see which of its type argument(s) mention any of those generics.
- Examing the definition for the `Self` type, and identify (for each of its variants) if there's a unique field
  which uses those generic arguments.
- If there is a unique field mentioning the "blameable" arguments, use that field for the error span.

Before we do any of this logic, we recursively call `point_at_specific_expr_if_possible` on the parent
obligation. Hence we refine the `expr` "outwards-in" and bail at the first kind of expression/impl we don't recognize.

This function returns a `Result<&Expr, &Expr>` - either way, it returns the `Expr` whose span should be
reported as an error. If it is `Ok`, then it means it refined successfull. If it is `Err`, then it may be
only a partial success - but it cannot be refined even further.

---

I added a new test file which exercises this new behavior. A few existing tests were affected, since their error spans are now different. In one case, this leads to a different code suggestion for the autofix - although the new suggestion isn't _wrong_, it is different from what used to be.

This change doesn't create any new errors or remove any existing ones, it just adjusts the spans where they're presented.

---

Some considerations: right now, this check occurs in addition to some similar logic in `adjust_fulfillment_error_for_expr_obligation` function, which tidies up various kinds of error spans (not just trait-fulfillment error). It's possible that this new code would be better integrated into that function (or another one) - but I haven't looked into this yet.

Although this code only occurs when there's a type error, it's definitely not as efficient as possible. In particular, there are definitely some cases where it degrades to quadratic performance (e.g. for a trait `impl` with 100+ generic parameters or 100 levels deep nesting of generic types). I'm not sure if these are realistic enough to worry about optimizing yet.

There's also still a lot of repetition in some of the logic, where the behavior for different types (namely, `struct` vs `enum` variant) is _similar_ but not the same.

---

I think the biggest win here is better targeting for tuples; in particular, if you're using tuples + traits to express variadic-like functions, the compiler can't tell you which part of a tuple has the wrong type, since the span will cover the entire argument. This change allows the individual field in the tuple to be highlighted, as in this example:

```
// NEW
LL |     want(Wrapper { value: (3, q) });
   |     ----                      ^ the trait `T3` is not implemented for `Q`

// OLD
LL |     want(Wrapper { value: (3, q) });
   |     ---- ^~~~~~~~~~~~~~~~~~~~~~~~~ the trait `T3` is not implemented for `Q`
```
Especially with large tuples, the existing error spans are not very effective at quickly narrowing down the source of the problem.
2023-02-06 21:16:39 +01:00
..
auxiliary Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code-in-the-same-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code-in-the-same-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-assoc-type-not-impl.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-assoc-type-not-impl.stderr Tweak output 2023-01-11 19:31:34 +00:00
derive-deadlock.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-deadlock.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-Debug-use-ufcs-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-Debug-use-ufcs-tuple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-hygiene.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-macro-const-default.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-marker-tricky.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-multiple-with-packed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-on-trait-item-or-impl-item.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-on-trait-item-or-impl-item.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-partial-ord.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-renamed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-bounds.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-bounds.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-copyclone.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-copyclone.stderr Point at specific field in struct literal when trait fulfillment fails 2023-01-23 13:37:58 -08:00
deriving-meta-empty-trait-list.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta-unknown-trait.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta-unknown-trait.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-no-inner-impl-error-message.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-no-inner-impl-error-message.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-non-type.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-non-type.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-primitive.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-primitive.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-with-repr-packed-2.rs make unaligned_reference a hard error 2023-01-31 20:28:11 +01:00
deriving-with-repr-packed-2.stderr make unaligned_reference a hard error 2023-01-31 20:28:11 +01:00
deriving-with-repr-packed.rs make unaligned_reference a hard error 2023-01-31 20:28:11 +01:00
deriving-with-repr-packed.stderr make unaligned_reference a hard error 2023-01-31 20:28:11 +01:00
issue-36617.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-36617.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-43023.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-43023.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91492.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91492.stderr Conserve cause of ImplDerivedObligation in E0599 2023-01-11 19:31:33 +00:00
issue-91550.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91550.stderr Keep obligation chain when elaborating obligations 2023-01-13 18:20:23 +00:00
issue-97343.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-97343.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00