Skip to content

Commit cb1bc2c

Browse files
Rollup merge of rust-lang#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 rust-lang#112573/rust-lang#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 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::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 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: * 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: rust-lang#120752 (comment). * 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 rust-lang#120719. ### Implementation history: Limited to the significant behavioral changes and fixes and relevant PRs, ping me if I left something out-- * rust-lang#57428 * rust-lang#108063 * rust-lang#110512 * rust-lang#112629 * rust-lang#120719 * rust-lang#120584 Closes rust-lang#52662 [RFC 2289]: https://rust-lang.github.io/rfcs/2289-associated-type-bounds.html
2 parents d6a2918 + c63f3fe commit cb1bc2c

File tree

95 files changed

+147
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+147
-533
lines changed

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![doc(rust_logo)]
1212
#![allow(internal_features)]
1313
#![feature(rustdoc_internals)]
14-
#![feature(associated_type_bounds)]
14+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
1515
#![feature(associated_type_defaults)]
1616
#![feature(box_patterns)]
1717
#![feature(if_let_guard)]

compiler/rustc_ast_passes/src/feature_gate.rs

-8
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
463463
constraint.span,
464464
"return type notation is experimental"
465465
);
466-
} else {
467-
gate!(
468-
&self,
469-
associated_type_bounds,
470-
constraint.span,
471-
"associated type bounds are unstable"
472-
);
473466
}
474467
}
475468
visit::walk_assoc_constraint(self, constraint)
@@ -615,7 +608,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
615608
}
616609

617610
gate_all_legacy_dont_use!(trait_alias, "trait aliases are experimental");
618-
gate_all_legacy_dont_use!(associated_type_bounds, "associated type bounds are unstable");
619611
// Despite being a new feature, `where T: Trait<Assoc(): Sized>`, which is RTN syntax now,
620612
// used to be gated under associated_type_bounds, which are right above, so RTN needs to
621613
// be too.

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![feature(rustdoc_internals)]
55
#![doc(rust_logo)]
66
#![feature(assert_matches)]
7-
#![feature(associated_type_bounds)]
7+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
88
#![feature(box_patterns)]
99
#![feature(control_flow_enum)]
1010
#![feature(let_chains)]

compiler/rustc_codegen_gcc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#![feature(
2020
rustc_private,
2121
decl_macro,
22-
associated_type_bounds,
2322
never_type,
2423
trusted_len,
2524
hash_raw_entry
2625
)]
26+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
2727
#![allow(broken_intra_doc_links)]
2828
#![recursion_limit = "256"]
2929
#![warn(rust_2018_idioms)]

compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![allow(internal_features)]
55
#![allow(rustc::diagnostic_outside_of_impl)]
66
#![allow(rustc::untranslatable_diagnostic)]
7-
#![feature(associated_type_bounds)]
7+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
88
#![feature(box_patterns)]
99
#![feature(if_let_guard)]
1010
#![feature(let_chains)]

compiler/rustc_error_codes/src/error_codes/E0719.md

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ An associated type value was specified more than once.
33
Erroneous code example:
44

55
```compile_fail,E0719
6-
#![feature(associated_type_bounds)]
7-
86
trait FooTrait {}
97
trait BarTrait {}
108
@@ -19,8 +17,6 @@ specify the associated type with the new trait.
1917
Corrected example:
2018

2119
```
22-
#![feature(associated_type_bounds)]
23-
2420
trait FooTrait {}
2521
trait BarTrait {}
2622
trait FooBarTrait: FooTrait + BarTrait {}

compiler/rustc_expand/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![doc(rust_logo)]
22
#![feature(rustdoc_internals)]
33
#![feature(array_windows)]
4-
#![feature(associated_type_bounds)]
4+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
55
#![feature(associated_type_defaults)]
66
#![feature(if_let_guard)]
77
#![feature(let_chains)]

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ declare_features! (
5959
(accepted, asm_sym, "1.66.0", Some(93333)),
6060
/// Allows the definition of associated constants in `trait` or `impl` blocks.
6161
(accepted, associated_consts, "1.20.0", Some(29646)),
62+
/// Allows the user of associated type bounds.
63+
(accepted, associated_type_bounds, "CURRENT_RUSTC_VERSION", Some(52662)),
6264
/// Allows using associated `type`s in `trait`s.
6365
(accepted, associated_types, "1.0.0", None),
6466
/// Allows free and inherent `async fn`s, `async` blocks, and `<expr>.await` expressions.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ declare_features! (
351351
(unstable, asm_unwind, "1.58.0", Some(93334)),
352352
/// Allows users to enforce equality of associated constants `TraitImpl<AssocConst=3>`.
353353
(unstable, associated_const_equality, "1.58.0", Some(92827)),
354-
/// Allows the user of associated type bounds.
355-
(unstable, associated_type_bounds, "1.34.0", Some(52662)),
356354
/// Allows associated type defaults.
357355
(unstable, associated_type_defaults, "1.2.0", Some(29661)),
358356
/// Allows `async || body` closures.

compiler/rustc_infer/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#![allow(internal_features)]
1919
#![allow(rustc::diagnostic_outside_of_impl)]
2020
#![allow(rustc::untranslatable_diagnostic)]
21-
#![feature(associated_type_bounds)]
21+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
2222
#![feature(box_patterns)]
2323
#![feature(control_flow_enum)]
2424
#![feature(extend_one)]

compiler/rustc_middle/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#![feature(trusted_len)]
4949
#![feature(type_alias_impl_trait)]
5050
#![feature(strict_provenance)]
51-
#![feature(associated_type_bounds)]
51+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
5252
#![feature(rustc_attrs)]
5353
#![feature(control_flow_enum)]
5454
#![feature(trait_upcasting)]

compiler/rustc_mir_build/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(rustc::diagnostic_outside_of_impl)]
66
#![allow(rustc::untranslatable_diagnostic)]
77
#![feature(assert_matches)]
8-
#![feature(associated_type_bounds)]
8+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
99
#![feature(box_patterns)]
1010
#![feature(if_let_guard)]
1111
#![feature(let_chains)]

compiler/rustc_parse/src/parser/path.rs

-2
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,6 @@ impl<'a> Parser<'a> {
731731
&& matches!(args.output, ast::FnRetTy::Default(..))
732732
{
733733
self.psess.gated_spans.gate(sym::return_type_notation, span);
734-
} else {
735-
self.psess.gated_spans.gate(sym::associated_type_bounds, span);
736734
}
737735
}
738736
let constraint =

compiler/rustc_serialize/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![doc(rust_logo)]
99
#![allow(internal_features)]
1010
#![feature(rustdoc_internals)]
11-
#![feature(associated_type_bounds)]
11+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
1212
#![feature(const_option)]
1313
#![feature(core_intrinsics)]
1414
#![feature(generic_nonzero)]

compiler/rustc_trait_selection/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#![allow(rustc::diagnostic_outside_of_impl)]
1818
#![allow(rustc::untranslatable_diagnostic)]
1919
#![feature(assert_matches)]
20-
#![feature(associated_type_bounds)]
20+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
2121
#![feature(associated_type_defaults)]
2222
#![feature(box_patterns)]
2323
#![feature(control_flow_enum)]

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
194194
sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
195195

196196
// Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
197-
// FIXME: once `#![feature(associated_type_bounds)]` is stabilized, we should suggest
198-
// `fn foo(t: impl Trait<A: Bound>)` instead.
197+
// FIXME: we should suggest `fn foo(t: impl Trait<A: Bound>)` instead.
199198
err.multipart_suggestion(
200199
"introduce a type parameter with a trait bound instead of using `impl Trait`",
201200
sugg,

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@
173173
//
174174
// Language features:
175175
// tidy-alphabetical-start
176+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
176177
#![cfg_attr(not(test), feature(coroutine_trait))]
177178
#![cfg_attr(test, feature(panic_update_hook))]
178179
#![cfg_attr(test, feature(test))]
179180
#![feature(allocator_internals)]
180181
#![feature(allow_internal_unstable)]
181-
#![feature(associated_type_bounds)]
182182
#![feature(c_unwind)]
183183
#![feature(cfg_sanitize)]
184184
#![feature(const_mut_refs)]

library/alloc/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
12
#![feature(allocator_api)]
23
#![feature(alloc_layout_extra)]
34
#![feature(iter_array_chunks)]
@@ -22,7 +23,6 @@
2223
#![feature(try_reserve_kind)]
2324
#![feature(try_with_capacity)]
2425
#![feature(unboxed_closures)]
25-
#![feature(associated_type_bounds)]
2626
#![feature(binary_heap_into_iter_sorted)]
2727
#![feature(binary_heap_drain_sorted)]
2828
#![feature(slice_ptr_get)]

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
//
204204
// Language features:
205205
// tidy-alphabetical-start
206+
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
206207
#![cfg_attr(bootstrap, feature(diagnostic_namespace))]
207208
#![cfg_attr(bootstrap, feature(exhaustive_patterns))]
208209
#![cfg_attr(bootstrap, feature(platform_intrinsics))]
@@ -213,7 +214,6 @@
213214
#![feature(allow_internal_unsafe)]
214215
#![feature(allow_internal_unstable)]
215216
#![feature(asm_const)]
216-
#![feature(associated_type_bounds)]
217217
#![feature(auto_traits)]
218218
#![feature(c_unwind)]
219219
#![feature(cfg_sanitize)]

tests/rustdoc-ui/issues/issue-110900.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ check-pass
22

33
#![crate_type="lib"]
4-
#![feature(associated_type_bounds)]
54

65
trait A<'a> {}
76
trait B<'b> {}

tests/ui/associated-consts/issue-93835.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ fn e() {
66
//~| ERROR cannot find value
77
//~| ERROR associated const equality
88
//~| ERROR cannot find trait `p` in this scope
9-
//~| ERROR associated type bounds
109
}
1110

1211
fn main() {}

tests/ui/associated-consts/issue-93835.stderr

+1-11
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@ LL | type_ascribe!(p, a<p:p<e=6>>);
2626
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
2727
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2828

29-
error[E0658]: associated type bounds are unstable
30-
--> $DIR/issue-93835.rs:4:24
31-
|
32-
LL | type_ascribe!(p, a<p:p<e=6>>);
33-
| ^^^^^^^^
34-
|
35-
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
36-
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable
37-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
38-
39-
error: aborting due to 5 previous errors
29+
error: aborting due to 4 previous errors
4030

4131
Some errors have detailed explanations: E0405, E0412, E0425, E0658.
4232
For more information about an error, try `rustc --explain E0405`.

tests/ui/associated-type-bounds/ambiguous-associated-type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ check-pass
22

3-
#![feature(associated_type_bounds)]
4-
53
pub struct Flatten<I>
64
where
75
I: Iterator<Item: IntoIterator>,

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// Additionally, as reported in https://github.com/rust-lang/rust/issues/63594,
99
// we check that the spans for the error message are sane here.
1010

11-
#![feature(associated_type_bounds)]
12-
1311
fn main() {}
1412

1513
trait Bar {

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: associated type bounds are not allowed in `dyn` types
2-
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:30:28
2+
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:28:28
33
|
44
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
55
| ^^^^^^^^^^^

tests/ui/associated-type-bounds/bad-bounds-on-assoc-in-trait.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ check-pass
22

3-
#![feature(associated_type_bounds)]
4-
53
use std::fmt::Debug;
64
use std::iter::Once;
75

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(associated_type_bounds)]
2-
31
trait B {
42
type AssocType;
53
}

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: associated type bounds are not allowed in `dyn` types
2-
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
2+
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:7:19
33
|
44
LL | dyn for<'j> B<AssocType: 'j>:,
55
| ^^^^^^^^^^^^^

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(associated_type_bounds)]
2-
31
trait Trait {
42
type Item;
53
}

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: associated type bounds are not allowed in `dyn` types
2-
--> $DIR/bad-universal-in-impl-sig.rs:10:16
2+
--> $DIR/bad-universal-in-impl-sig.rs:8:16
33
|
44
LL | impl dyn Trait<Item: Trait2> {}
55
| ^^^^^^^^^^^^

tests/ui/associated-type-bounds/bounds-on-assoc-in-trait.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ check-pass
22

3-
#![feature(associated_type_bounds)]
4-
53
use std::fmt::Debug;
64
use std::iter::Empty;
75
use std::ops::Range;

tests/ui/associated-type-bounds/consts.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(associated_type_bounds)]
2-
31
pub fn accept(_: impl Trait<K: Copy>) {}
42
//~^ ERROR expected type, found constant
53

tests/ui/associated-type-bounds/consts.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: expected type, found constant
2-
--> $DIR/consts.rs:3:29
2+
--> $DIR/consts.rs:1:29
33
|
44
LL | pub fn accept(_: impl Trait<K: Copy>) {}
55
| ^------ bounds are not allowed on associated constants
66
| |
77
| unexpected constant
88
|
99
note: the associated constant is defined here
10-
--> $DIR/consts.rs:7:5
10+
--> $DIR/consts.rs:5:5
1111
|
1212
LL | const K: i32;
1313
| ^^^^^^^^^^^^

tests/ui/associated-type-bounds/duplicate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(associated_type_bounds)]
21
#![feature(type_alias_impl_trait)]
32

43
use std::iter;

0 commit comments

Comments
 (0)