Files
rust/compiler/rustc_error_codes/src/error_codes
bors 21d94a3d2c Auto merge of #122055 - compiler-errors:stabilize-atb, r=oli-obk
Stabilize associated type bounds (RFC 2289)

This PR stabilizes associated type bounds, which were laid out in [RFC 2289]. This gives us a shorthand to express nested type bounds that would otherwise need to be expressed with nested `impl Trait` or broken into several `where` clauses.

### What are we stabilizing?

We're stabilizing the associated item bounds syntax, which allows us to put bounds in associated type position within other bounds, i.e. `T: Trait<Assoc: Bounds...>`. See [RFC 2289] for motivation.

In all position, the associated type bound syntax expands into a set of two (or more) bounds, and never anything else (see "How does this differ[...]" section for more info).

Associated type bounds are stabilized in four positions:
* **`where` clauses (and APIT)** - This is equivalent to breaking up the bound into two (or more) `where` clauses. For example, `where T: Trait<Assoc: Bound>` is equivalent to `where T: Trait, <T as Trait>::Assoc: Bound`.
* **Supertraits** - Similar to above, `trait CopyIterator: Iterator<Item: Copy> {}`. This is almost equivalent to breaking up the bound into two (or more) `where` clauses; however, the bound on the associated item is implied whenever the trait is used. See #112573/#112629.
* **Associated type item bounds** - This allows constraining the *nested* rigid projections that are associated with a trait's associated types. e.g. `trait Trait { type Assoc: Trait2<Assoc2: Copy>; }`.
* **opaque item bounds (RPIT, TAIT)** - This allows constraining associated types that are associated with the opaque without having to *name* the opaque. For example, `impl Iterator<Item: Copy>` defines an iterator whose item is `Copy` without having to actually name that item bound.

The latter three are not expressible in surface Rust (though for associated type item bounds, this will change in #120752, which I don't believe should block this PR), so this does represent a slight expansion of what can be expressed in trait bounds.

### How does this differ from the RFC?

Compared to the RFC, the current implementation *always* desugars associated type bounds to sets of `ty::Clause`s internally. Specifically, it does *not* introduce a position-dependent desugaring as laid out in [RFC 2289], and in particular:
* It does *not* desugar to anonymous associated items in associated type item bounds.
* It does *not* desugar to nested RPITs in RPIT bounds, nor nested TAITs in TAIT bounds.

This position-dependent desugaring laid out in the RFC existed simply to side-step limitations of the trait solver, which have mostly been fixed in #120584. The desugaring laid out in the RFC also added unnecessary complication to the design of the feature, and introduces its own limitations to, for example:
* Conditionally lowering to nested `impl Trait` in certain positions such as RPIT and TAIT means that we inherit the limitations of RPIT/TAIT, namely lack of support for higher-ranked opaque inference. See this code example: https://github.com/rust-lang/rust/pull/120752#issuecomment-1979412531.
* Introducing anonymous associated types makes traits no longer object safe, since anonymous associated types are not nameable, and all associated types must be named in `dyn` types.

This last point motivates why this PR is *not* stabilizing support for associated type bounds in `dyn` types, e.g, `dyn Assoc<Item: Bound>`. Why? Because `dyn` types need to have *concrete* types for all associated items, this would necessitate a distinct lowering for associated type bounds, which seems both complicated and unnecessary compared to just requiring the user to write `impl Trait` themselves. See #120719.

### Implementation history:

Limited to the significant behavioral changes and fixes and relevant PRs, ping me if I left something out--
* #57428
* #108063
* #110512
* #112629
* #120719
* #120584

Closes #52662

[RFC 2289]: https://rust-lang.github.io/rfcs/2289-associated-type-bounds.html
2024-03-19 00:04:09 +00:00
..
…
…
…
…
…
…
…
…
…
2023-04-17 16:09:18 -04:00
…
…
…
…
…
2023-09-26 09:37:55 +02:00
…
…
2022-10-03 08:53:06 +02:00
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2023-03-27 18:58:07 +00:00
…
…
…
…
…
…
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
…
…
…
…
…
…
2022-07-19 08:52:24 -07:00
…
…
…
…
2021-03-23 17:16:20 +00:00
…
…
2021-04-16 13:04:02 +08:00
…
2024-02-12 15:46:35 +09:00
…
2022-10-03 08:53:06 +02:00
…
…
…
…
…
…
…
…
2023-08-21 18:45:51 +07:00
…
…
…
…
…
…
…
…
…
…
2023-08-03 14:50:50 +02:00
2022-10-24 00:52:26 -07:00
…
…
…
…
2021-10-02 08:05:44 +07:00
2023-03-27 18:00:22 +00:00
…
…
…
…
…
2024-02-12 15:46:35 +09:00
2024-02-12 15:46:35 +09:00
2023-08-03 14:50:50 +02:00
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2023-04-17 16:09:18 -04:00
2022-06-03 17:16:41 -04:00
2021-06-07 14:20:39 +02:00
…
…
2022-11-14 03:23:41 +00:00
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2024-01-02 13:49:47 +01:00
…
…
2022-10-31 00:41:12 -04:00
…
…
…
…
…
…
2024-02-12 15:46:35 +09:00
…
…
2023-09-10 23:06:14 +02:00
…
2021-04-02 04:27:18 +09:00
…
…
…
…
…
…
…
2023-03-15 23:55:48 +05:30
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2023-11-04 08:50:46 +11:00
…
2022-12-19 22:50:31 +13:00
2023-11-04 08:50:46 +11:00
2023-02-06 06:58:30 -05:00
…
…
…
2022-06-03 17:16:41 -04:00
…
…
2022-06-03 17:16:41 -04:00
…
…
2023-11-04 08:50:46 +11:00
…
…
…
…
…
…
…
…
…
2021-06-22 22:24:46 +10:00
…
…
…
…
…
…
…
…
…
2023-02-06 06:58:30 -05:00
…
…
…
…
…
2021-08-03 11:11:17 +01:00
…
…
…
…
…
…
…
…
2023-08-03 14:50:50 +02:00
…
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2023-08-03 14:50:50 +02:00
2022-04-14 22:44:21 -04:00
2023-10-04 18:12:20 +11:00
…
2021-04-08 02:52:00 +08:00
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2023-04-17 16:09:18 -04:00
2023-08-02 15:30:29 +00:00
…
2022-10-03 08:53:06 +02:00
…
…
…
…
…
…
2023-01-27 10:59:51 +01:00
…
…
2022-10-13 00:53:46 +08:00
…
…
…
…
…
…
…
…
…
…
…
…
…
2023-04-17 16:09:18 -04:00
…
…
…
…
2021-07-28 16:33:02 -04:00
…
…
…
…
2023-08-03 14:50:50 +02:00
2022-06-03 17:16:41 -04:00
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
2021-07-27 15:47:49 -04:00
…
2022-03-27 14:09:52 +02:00
…
2022-02-24 18:50:33 -08:00
2022-02-24 18:50:33 -08:00
…
2021-06-14 07:42:15 +09:00
2023-08-29 14:11:50 +02:00
…
…
…
…
…
…
…
…
…
2023-12-10 13:03:28 -08:00
2023-10-13 21:01:36 +00:00
…
…
2024-03-01 17:31:02 -06:00
…
2024-03-08 20:56:25 +00:00
…
2021-07-21 10:58:35 +01:00
2024-01-30 22:09:09 +00:00
…
…
2023-01-02 09:11:36 +13:00
…
…
2024-01-08 20:32:06 +00:00
2022-04-14 21:19:46 -04:00
…
…
…
…
…
2022-10-03 08:53:06 +02:00
2023-11-28 20:40:38 +00:00
…
…
…
…
…
…
…
…
…
2021-04-08 02:52:00 +08:00
…
…
…
2022-06-03 17:16:41 -04:00
2023-09-08 03:45:36 +00:00
…
…
…
…
…
…
…
2023-05-05 21:31:35 +01:00
2022-06-03 17:16:41 -04:00
2023-08-03 14:50:50 +02:00
…
…
…
…
…
…
…
…
2021-04-13 16:55:54 +02:00
2021-04-13 16:55:54 +02:00
2021-08-30 22:18:55 -07:00
2022-01-21 17:38:21 +01:00
2023-09-08 12:46:06 +01:00
2023-08-03 14:50:50 +02:00
2023-10-20 21:10:38 +00:00
2023-01-31 20:28:11 +01:00
2024-02-12 15:46:35 +09:00
2024-01-19 20:38:51 +00:00
2024-02-18 06:01:40 +03:00