Ban associated type bounds in bad positions by compiler-errors · Pull Request #108063 · rust-lang/rust (original) (raw)

…li-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:

The latter three are not expressible in surface Rust (though for associated type item bounds, this will change in rust-lang#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::Clauses internally. Specifically, it does not introduce a position-dependent desugaring as laid out in RFC 2289, and in particular:

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 rust-lang#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:

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 rust-lang#120719.

Implementation history:

Limited to the significant behavioral changes and fixes and relevant PRs, ping me if I left something out--

Closes rust-lang#52662