rust/tests/ui/associated-consts
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
..
auxiliary Move /src/test to /tests 2023-01-11 09:32:08 +00:00
assoc-const-eq-ambiguity.rs Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const-eq-ambiguity.stderr Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const-eq-bound-var-in-ty-not-wf.rs Reject escaping bound vars in the type of assoc const bindings 2024-03-13 14:29:27 +01:00
assoc-const-eq-bound-var-in-ty-not-wf.stderr Reject escaping bound vars in the type of assoc const bindings 2024-03-13 14:29:27 +01:00
assoc-const-eq-bound-var-in-ty.rs Reject escaping bound vars in the type of assoc const bindings 2024-03-13 14:29:27 +01:00
assoc-const-eq-esc-bound-var-in-ty.rs Reject escaping bound vars in the type of assoc const bindings 2024-03-13 14:29:27 +01:00
assoc-const-eq-esc-bound-var-in-ty.stderr Reject escaping bound vars in the type of assoc const bindings 2024-03-13 14:29:27 +01:00
assoc-const-eq-missing.rs Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const-eq-missing.stderr Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const-eq-param-in-ty.rs Reject early-bound params in the type of assoc const bindings 2024-03-13 13:32:54 +01:00
assoc-const-eq-param-in-ty.stderr Reject early-bound params in the type of assoc const bindings 2024-03-13 13:32:54 +01:00
assoc-const-eq-supertraits.rs Propagate the resolved type of assoc const bindings via query feeding 2024-02-18 10:29:22 +01:00
assoc-const-eq-ty-alias-noninteracting.rs Propagate the resolved type of assoc const bindings via query feeding 2024-02-18 10:29:22 +01:00
assoc-const-ty-mismatch.rs Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const-ty-mismatch.stderr Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
assoc-const.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-ambiguity-report.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-ambiguity-report.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-array-len.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-array-len.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-const-eval.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-cross-crate-const-eval.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-cross-crate-defaults.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-cross-crate.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-dead-code.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-dead-code.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-generic-obligations.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-generic-obligations.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-impl-wrong-lifetime.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-impl-wrong-lifetime.stderr recurse into refs when comparing tys for diagnostics 2023-12-07 23:00:46 -05:00
associated-const-impl-wrong-type.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-impl-wrong-type.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-in-global-const.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-in-trait.rs Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
associated-const-in-trait.stderr Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
associated-const-inherent-impl.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-marks-live-code.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-match-patterns.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-no-item.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-no-item.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-outer-ty-refs.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-overwrite-default.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-private-impl.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-private-impl.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-public-impl.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-range-match-patterns.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-resolution-order.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-self-type.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-trait-bound.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-type-parameter-arms.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-type-parameter-arms.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-type-parameter-arrays-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-type-parameter-arrays-2.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
associated-const-type-parameter-arrays.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
associated-const-type-parameter-arrays.stderr generic_const_exprs: suggest to add the feature, not use it 2023-11-30 20:59:51 +01:00
associated-const-type-parameters.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-type-parameters.stderr Update tests 2024-02-07 10:42:01 +08:00
associated-const-ufcs-infer-trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-use-default.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const-use-impl-of-same-trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
associated-const.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
defaults-cyclic-fail.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
defaults-cyclic-fail.stderr Enable ConstPropLint for promoteds 2024-02-17 10:44:46 +05:30
defaults-cyclic-pass.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
defaults-not-assumed-fail.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
defaults-not-assumed-fail.stderr Enable ConstPropLint for promoteds 2024-02-17 10:44:46 +05:30
defaults-not-assumed-pass.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
double-elided.rs Fall through when resolving elided assoc const lifetimes 2023-09-01 07:23:39 +00:00
double-elided.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
freeze.rs Expose Freeze trait again 2024-02-29 13:55:11 +00:00
infer-placeholder-in-non-suggestable-pos.rs Warn on elided lifetimes in associated constants 2023-08-20 00:21:47 +00:00
infer-placeholder-in-non-suggestable-pos.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-24949-assoc-const-static-recursion-impl.rs Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
issue-24949-assoc-const-static-recursion-impl.stderr Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
issue-24949-assoc-const-static-recursion-trait-default.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-24949-assoc-const-static-recursion-trait-default.stderr Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
issue-24949-assoc-const-static-recursion-trait.rs Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
issue-24949-assoc-const-static-recursion-trait.stderr Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
issue-47814.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-47814.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-58022.rs Avoid a track_errors by bubbling up most errors from check_well_formed 2023-10-20 08:46:27 +00:00
issue-58022.stderr Provide more context on derived obligation error primary label 2024-01-30 21:28:18 +00:00
issue-63496.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-63496.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-69020-assoc-const-arith-overflow.noopt.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-69020-assoc-const-arith-overflow.opt_with_overflow_checks.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-69020-assoc-const-arith-overflow.opt.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-69020-assoc-const-arith-overflow.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-88599-ref-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-93775.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-93835.rs Stabilize associated type bounds 2024-03-08 20:56:25 +00:00
issue-93835.stderr Stabilize associated type bounds 2024-03-08 20:56:25 +00:00
issue-102335-const.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-102335-const.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-105330.rs Continue compilation after check_mod_type_wf errors 2024-02-14 11:00:30 +00:00
issue-105330.stderr Never say "Trait is implemented for {type error}" 2024-03-01 02:12:47 +00:00
issue-110933.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
mismatched_impl_ty_1.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
mismatched_impl_ty_2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
mismatched_impl_ty_3.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
projection-unspecified-but-bounded.rs Don't create projection ty for const projection 2023-04-23 18:09:30 +00:00
projection-unspecified-but-bounded.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
shadowed-const.rs Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00
shadowed-const.stderr Resolve assoc item bindings by namespace 2023-12-07 22:33:56 +01:00