rust-clippy/tests/ui/crashes
bors 3de0f19c41 Auto merge of #11437 - y21:issue-11422, r=xFrednet
[`implied_bounds_in_impls`]: don't ICE on default generic parameter and move to nursery

Fixes #11422

This fixes two ICEs ([1](https://github.com/rust-lang/rust-clippy/issues/11422#issue-1872351763), [2](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2901e6febb479d3bd2a74f8a5b8a9305)), and moves it to nursery for now, because this lint needs some improvements in its suggestion (see #11435, for one such example).

changelog: Moved [`implied_bounds_in_impls`] to nursery (Now allow-by-default)
[#11437](https://github.com/rust-lang/rust-clippy/pull/11437)
changelog: [`implied_bounds_in_impls`]: don't ICE on default generic parameter in supertrait clause

r? `@xFrednet` (since you reviewed my PR that added this lint, I figured it might make sense to have you review this as well since you have seen this code before. If you don't want to review this, sorry! Feel free to reroll then)

--------

As for the ICE, it's pretty complicated and very confusing imo, so I'm going to try to explain the idea here (partly for myself, too, because I've confused myself several times writing- and fixing this):
<details>
<summary>Expand</summary>

The general idea behind the lint is that, if we have this function:
```rs
fn f() -> impl PartialEq<i32> + PartialOrd<i32> { 0 }
```
We want to lint the `PartialEq` bound because it's unnecessary. That exact bound is already specified in `PartialOrd<i32>`'s supertrait clause:
```rs
trait PartialOrd<Rhs>: PartialEq<Rhs> {}
//    PartialOrd<i32>: PartialEq<i32>
```

 The way it does this is in two steps:
- Go through all of the bounds in the `impl Trait` return type and collect each of the trait's supertrait bounds into a vec. We also store the generic arguments for later.
  - `PartialEq` has no supertraits, nothing to add.
  - `PartialOrd` is defined as `trait PartialOrd: PartialEq`, so add `PartialEq` to the list, as well as the generic argument(s) `<i32>`

Once we are done, we have these entries in the vec: `[(PartialEq, [i32])]`

- Go through all the bounds again, and looking for those bounds that have their trait `DefId` in the implied bounds vec.
  - `PartialEq` is in that vec. However, that is not enough, because the trait is generic. If the user wrote `impl PartialEq<String> + PartialOrd<i32>`, then `PartialOrd` clearly doesn't imply `PartialEq`. Which means, we also need to check that the generic parameters match. This is why we also collected the generic arguments in `PartialOrd<i32>`. This process of checking generic arguments is pretty complicated and is also where the two ICEs happened.

The way it checks that the generic arguments match is by comparing the generic parameters in the super trait clause:
```rs
trait PartialOrd<Rhs>: PartialEq<Rhs> {}
//                     ^^^^^^^^^^^^^^
```
...this needs to match...
```rs
fn f() -> impl PartialEq<i32> + ...
//             ^^^^^^^^^^^^^^
```
In the compiler, the `Rhs` generic parameter is its own type and we cannot just compare it to `i32`. We need to "substitute" it.
Internally, `Rhs` is represented as `Rhs#1` (the number next to # represents the type parameter index. They start at 0, but 0 is "reserved" for the implicit `Self` generic parameter).

How do we go from `Rhs#1` to `i32`? Well, we know that all the generic parameters had to be substituted in the `impl ... + PartialOrd<i32>` type. So we subtract 1 from the type parameter index, giving us 0 (`Self` is not specified in that list of arguments). We use that as the index into the generic argument list `<i32>`. That's `i32`. Now we know that the supertrait clause looks like `: PartialEq<i32>`.

Then, we can compare that to what the user actually wrote on the bound that we think is being implied: `impl PartialEq<i32> + ...`.

Now to the actual bug: this whole logic doesn't take into account *default* generic parameters. Actually, `PartialOrd` is defined like this:
```rs
trait PartialOrd<Rhs = Self>: PartialEq<Rhs> {}
```
If we now have a function like this:
```rs
fn f() -> impl PartialOrd + PartialEq {}
```
that logic breaks apart... We look at the supertrait predicate `: PartialEq<Rhs>` (`Rhs` is `Rhs#1`), then take the first argument in the generic argument list `PartialEq<..>` to resolve the `Rhs`, but at this point we crash because there *is no* generic argument.
The index 0 is out of bounds. If this happens (and we even get to linting here, which could only happen if it passes typeck), it must mean that that generic parameter has a default type that is not required to be specified.

This PR changes the logic such that if we have a type parameter index that is out of bounds, it looks at the definition of the trait and check that there exists a default type that we can use instead.
So, we see `<Rhs = Self>`, and use `Self` for substitution, and end up with this predicate: `: PartialEq<Self>`. No crash this time.

</details>
2023-09-03 16:09:40 +00:00
..
auxiliary Manually add annotations for ui tests 2023-08-22 17:14:08 +02:00
third-party Avoid "whitelist" 2020-07-10 07:39:28 -04:00
associated-constant-ice.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
cc_seme.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
enum-glob-import-crate.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-360.rs never_loop catches loop { panic!() } 2023-09-02 08:18:53 -04:00
ice-360.stderr never_loop catches loop { panic!() } 2023-09-02 08:18:53 -04:00
ice-700.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-1588.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-1782.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-1969.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-2499.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-2594.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-2727.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-2760.rs Merge commit '2b2190cb5667cdd276a24ef8b9f3692209c54a89' into clippyup 2022-08-11 19:42:16 +02:00
ice-2774.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-2774.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-2774.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-2862.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-2865.rs Merge commit '7c21f91b15b7604f818565646b686d90f99d1baf' into clippyup 2022-05-05 15:12:52 +01:00
ice-3151.rs Merge commit '7c21f91b15b7604f818565646b686d90f99d1baf' into clippyup 2022-05-05 15:12:52 +01:00
ice-3462.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-3717.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-3717.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-3717.stderr update clippy 2021-08-11 14:21:33 +00:00
ice-3741.rs Update ui test crate to auto-detect aux build crate kind 2023-08-11 14:02:35 +00:00
ice-3747.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-3891.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-3891.stderr Update error to reflect that integer literals can have float suffixes 2020-11-27 19:08:24 -08:00
ice-3969.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-3969.stderr Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-4121.rs Move ICE tests to /crashes 2019-12-31 20:55:12 +09:00
ice-4545.rs Move ICE tests to /crashes 2019-12-31 20:55:12 +09:00
ice-4579.rs Move ICE tests to /crashes 2019-12-31 20:55:12 +09:00
ice-4671.rs Move use_self_macro into crashes/auxiliary 2020-01-03 17:03:07 +09:00
ice-4727.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-4760.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
ice-4775.rs Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup 2022-10-06 09:44:38 +02:00
ice-4968.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-5207.rs Merge commit '371120bdbf58a331db5dcfb2d9cddc040f486de8' into clippyup 2023-05-05 17:45:49 +02:00
ice-5223.rs feature(const_generics) -> feature(const_param_types) 2021-08-30 11:00:21 +02:00
ice-5238.rs add test for #5238 2020-02-28 22:00:20 +01:00
ice-5389.rs Merge commit 'c2c07fa9d095931eb5684a42942a7b573a0c5238' into clippyup 2020-06-23 17:05:22 +02:00
ice-5497.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-5497.stderr update clippy stderr file 2022-03-24 11:27:07 +00:00
ice-5579.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-5835.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-5835.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-5835.stderr Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup 2021-04-22 11:31:13 +02:00
ice-5872.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-5872.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-5872.stderr Merge commit '09bd400243ed6f7059fedc0c1623aae3792521d6' into clippyup 2020-08-11 17:50:45 +02:00
ice-5944.rs Merge commit '7c21f91b15b7604f818565646b686d90f99d1baf' into clippyup 2022-05-05 15:12:52 +01:00
ice-6139.rs Merge commit 'bf1c6f9871f430e284b17aa44059e0d0395e28a6' into clippyup 2020-10-23 22:16:59 +02:00
ice-6153.rs Merge commit 'bf1c6f9871f430e284b17aa44059e0d0395e28a6' into clippyup 2020-10-23 22:16:59 +02:00
ice-6179.rs Merge commit '3c06e0b1ce003912f8fe0536d3a7fe22558e38cf' into clippyup 2023-03-10 10:53:50 +01:00
ice-6250.rs Update ui test crate 2023-08-11 14:02:28 +00:00
ice-6250.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-6251.rs Update ui test crate 2023-08-11 14:02:28 +00:00
ice-6251.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-6252.rs Update ui test crate 2023-08-11 14:02:28 +00:00
ice-6252.stderr Update ui test crate 2023-08-11 14:02:28 +00:00
ice-6254.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-6254.stderr Add notes to non-structural const in pattern error message 2023-03-27 11:08:11 -04:00
ice-6255.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-6255.stderr Fix clippy with changed macro statement spans 2021-10-15 02:36:58 -05:00
ice-6256.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
ice-6256.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-6332.rs Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup 2020-11-23 13:51:04 +01:00
ice-6539.rs Merge commit '95c0459217d1661edfa794c8bb122452b92fb485' into clippyup 2021-01-30 18:06:34 +01:00
ice-6792.rs Merge commit '984330a6ee3c4d15626685d6dc8b7b759ff630bd' into clippyup 2022-04-08 10:06:10 +01:00
ice-6793.rs Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup 2021-03-12 15:30:50 +01:00
ice-6840.rs Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup 2021-03-12 15:30:50 +01:00
ice-7012.rs Merge commit 'b40ea209e7f14c8193ddfc98143967b6a2f4f5c9' into clippyup 2021-04-08 17:50:13 +02:00
ice-7126.rs Stabilize const BTree{Map,Set}::new 2022-09-23 20:55:37 +02:00
ice-7169.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-7169.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-7169.stderr Merge commit 'd9c24d1b1ee61f276e550b967409c9f155eac4e3' into clippyup 2023-07-17 10:22:32 +02:00
ice-7231.rs Merge commit 'e18101137866b79045fee0ef996e696e68c920b4' into clippyup 2021-11-04 12:52:36 +00:00
ice-7272.rs Merge commit 'a3ed905928a03b6e433d0b429190bf3a847128b3' into clippyup 2023-04-23 13:28:56 +02:00
ice-7340.rs Merge commit '4c41a222ca5d1325fb4b6709395bd06e766cc042' into clippyup 2021-07-19 11:52:05 +02:00
ice-7410.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-7423.rs Merge commit '54a20a02ecd0e1352a871aa0990bcc8b8b03173e' into clippyup 2021-07-15 10:44:10 +02:00
ice-7868.rs Merge commit 'e18101137866b79045fee0ef996e696e68c920b4' into clippyup 2021-11-04 12:52:36 +00:00
ice-7868.stderr bless clippy 2022-10-01 10:03:06 +00:00
ice-7869.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-7869.stderr Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-7934.rs Merge commit 'a5d597637dcb78dc73f93561ce474f23d4177c35' into clippyup 2021-12-06 12:33:31 +01:00
ice-8250.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8250.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8250.stderr Merge commit 'd9c24d1b1ee61f276e550b967409c9f155eac4e3' into clippyup 2023-07-17 10:22:32 +02:00
ice-8386.rs Merge commit '57b3c4b90f4346b3990c1be387c3b3ca7b78412c' into clippyup 2022-02-10 18:40:06 +01:00
ice-8681.rs Merge commit 'a3ed905928a03b6e433d0b429190bf3a847128b3' into clippyup 2023-04-23 13:28:56 +02:00
ice-8821.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8821.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8821.stderr Merge 'rust-clippy/master' into clippyup 2022-05-21 13:24:00 +02:00
ice-8850.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8850.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-8850.stderr Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-9041.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-9041.stderr Merge commit 'fdb84cbfd25908df5683f8f62388f663d9260e39' into clippyup 2022-07-18 09:39:37 +02:00
ice-9238.rs Merge commit '3c7e7dbc1583a0b06df5bd7623dd354a4debd23d' into clippyup 2022-07-28 19:08:22 +02:00
ice-9242.rs Merge commit '3c7e7dbc1583a0b06df5bd7623dd354a4debd23d' into clippyup 2022-07-28 19:08:22 +02:00
ice-9405.rs Merge commit 'b52fb5234cd7c11ecfae51897a6f7fa52e8777fc' into clippyup 2022-09-09 13:36:26 +02:00
ice-9405.stderr Use ui_test's Windows path backslash heuristic 2023-08-14 15:59:00 +00:00
ice-9414.rs Merge commit 'b52fb5234cd7c11ecfae51897a6f7fa52e8777fc' into clippyup 2022-09-09 13:36:26 +02:00
ice-9445.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-9445.stderr Make simd_shuffle_indices use valtrees 2023-06-26 09:34:52 +00:00
ice-9459.rs Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup 2022-10-06 09:44:38 +02:00
ice-9463.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-9463.stderr Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-9625.rs Merge commit '4f142aa1058f14f153f8bfd2d82f04ddb9982388' into clippyup 2022-10-23 15:18:45 +02:00
ice-9746.rs Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup 2022-11-21 20:51:52 +01:00
ice-10148.rs Update ui test crate to auto-detect aux build crate kind 2023-08-11 14:02:35 +00:00
ice-10148.stderr Merge commit '3c06e0b1ce003912f8fe0536d3a7fe22558e38cf' into clippyup 2023-03-10 10:53:50 +01:00
ice-10645.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-10645.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-10912.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-10912.stderr Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
ice-11065.rs [useless_conversion]: fix FP in macro and add test 2023-08-14 16:28:04 +02:00
ice-11337.rs allow trait alias DefIds in implements_trait_with_env_from_iter 2023-08-14 19:10:33 +02:00
ice-11422.fixed [implied_bounds_in_impls]: move to nursery and fix ICEs 2023-08-30 22:08:05 +02:00
ice-11422.rs [implied_bounds_in_impls]: move to nursery and fix ICEs 2023-08-30 22:08:05 +02:00
ice-11422.stderr [implied_bounds_in_impls]: move to nursery and fix ICEs 2023-08-30 22:08:05 +02:00
ice-96721.fixed Update ui test crate 2023-08-11 14:02:28 +00:00
ice-96721.rs Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-96721.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
ice-rust-107877.rs Merge commit '3c06e0b1ce003912f8fe0536d3a7fe22558e38cf' into clippyup 2023-03-10 10:53:50 +01:00
ice_exact_size.rs Merge commit '371120bdbf58a331db5dcfb2d9cddc040f486de8' into clippyup 2023-05-05 17:45:49 +02:00
if_same_then_else.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
implements-trait.rs Fix ICE in utils::implements_trait 2020-11-23 13:52:27 +01:00
inherent_impl.rs Ignore impls derived from macros 2019-09-27 20:47:00 -05:00
issue-825.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
issues_loop_mut_cond.rs Add desugaring mark to while loop 2021-10-02 17:41:14 -05:00
match_same_arms_const.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
mut_mut_macro.rs Drop uplifted clippy:cmp_nan 2023-06-10 11:13:01 +02:00
needless_borrow_fp.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
needless_lifetimes_impl_trait.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
needless_lifetimes_impl_trait.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
needless_lifetimes_impl_trait.stderr Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup 2023-07-02 14:59:02 +02:00
needless_pass_by_value-w-late-bound.fixed Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
needless_pass_by_value-w-late-bound.rs Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
needless_pass_by_value-w-late-bound.stderr Merge commit '149392b0baa4730c68f3c3eadf5c6ed7b16b85a4' into clippyup 2023-02-25 19:28:50 -05:00
regressions.rs Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup 2022-10-06 09:44:38 +02:00
returns.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
shadow.rs Use node_type_opt over node_type 2020-03-10 18:18:34 +09:00
single-match-else.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
trivial_bounds.rs Merge commit '2f6439ae6a6803d030cceb3ee14c9150e91b328b' into clippyup 2020-10-09 12:45:29 +02:00
used_underscore_binding_macro.rs Merge commit 'e18101137866b79045fee0ef996e696e68c920b4' into clippyup 2021-11-04 12:52:36 +00:00