Skip to content

Commit

Permalink
Auto merge of rust-lang#122055 - compiler-errors:stabilize-atb, r=oli…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
bors committed Mar 19, 2024
2 parents 3c85e56 + c63f3fe commit 21d94a3
Show file tree
Hide file tree
Showing 95 changed files with 147 additions and 533 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![doc(rust_logo)]
#![allow(internal_features)]
#![feature(rustdoc_internals)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(associated_type_defaults)]
#![feature(box_patterns)]
#![feature(if_let_guard)]
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
constraint.span,
"return type notation is experimental"
);
} else {
gate!(
&self,
associated_type_bounds,
constraint.span,
"associated type bounds are unstable"
);
}
}
visit::walk_assoc_constraint(self, constraint)
Expand Down Expand Up @@ -615,7 +608,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
}

gate_all_legacy_dont_use!(trait_alias, "trait aliases are experimental");
gate_all_legacy_dont_use!(associated_type_bounds, "associated type bounds are unstable");
// Despite being a new feature, `where T: Trait<Assoc(): Sized>`, which is RTN syntax now,
// used to be gated under associated_type_bounds, which are right above, so RTN needs to
// be too.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![feature(rustdoc_internals)]
#![doc(rust_logo)]
#![feature(assert_matches)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(let_chains)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#![feature(
rustc_private,
decl_macro,
associated_type_bounds,
never_type,
trusted_len,
hash_raw_entry
)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![allow(broken_intra_doc_links)]
#![recursion_limit = "256"]
#![warn(rust_2018_idioms)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(internal_features)]
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(let_chains)]
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_error_codes/src/error_codes/E0719.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ An associated type value was specified more than once.
Erroneous code example:

```compile_fail,E0719
#![feature(associated_type_bounds)]
trait FooTrait {}
trait BarTrait {}
Expand All @@ -19,8 +17,6 @@ specify the associated type with the new trait.
Corrected example:

```
#![feature(associated_type_bounds)]
trait FooTrait {}
trait BarTrait {}
trait FooBarTrait: FooTrait + BarTrait {}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
#![feature(array_windows)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(associated_type_defaults)]
#![feature(if_let_guard)]
#![feature(let_chains)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ declare_features! (
(accepted, asm_sym, "1.66.0", Some(93333)),
/// Allows the definition of associated constants in `trait` or `impl` blocks.
(accepted, associated_consts, "1.20.0", Some(29646)),
/// Allows the user of associated type bounds.
(accepted, associated_type_bounds, "CURRENT_RUSTC_VERSION", Some(52662)),
/// Allows using associated `type`s in `trait`s.
(accepted, associated_types, "1.0.0", None),
/// Allows free and inherent `async fn`s, `async` blocks, and `<expr>.await` expressions.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ declare_features! (
(unstable, asm_unwind, "1.58.0", Some(93334)),
/// Allows users to enforce equality of associated constants `TraitImpl<AssocConst=3>`.
(unstable, associated_const_equality, "1.58.0", Some(92827)),
/// Allows the user of associated type bounds.
(unstable, associated_type_bounds, "1.34.0", Some(52662)),
/// Allows associated type defaults.
(unstable, associated_type_defaults, "1.2.0", Some(29661)),
/// Allows `async || body` closures.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![allow(internal_features)]
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(extend_one)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#![feature(trusted_len)]
#![feature(type_alias_impl_trait)]
#![feature(strict_provenance)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(rustc_attrs)]
#![feature(control_flow_enum)]
#![feature(trait_upcasting)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
#![feature(assert_matches)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(let_chains)]
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,6 @@ impl<'a> Parser<'a> {
&& matches!(args.output, ast::FnRetTy::Default(..))
{
self.psess.gated_spans.gate(sym::return_type_notation, span);
} else {
self.psess.gated_spans.gate(sym::associated_type_bounds, span);
}
}
let constraint =
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![doc(rust_logo)]
#![allow(internal_features)]
#![feature(rustdoc_internals)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(const_option)]
#![feature(core_intrinsics)]
#![feature(generic_nonzero)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
#![feature(assert_matches)]
#![feature(associated_type_bounds)]
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(associated_type_defaults)]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));

// Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
// FIXME: once `#![feature(associated_type_bounds)]` is stabilized, we should suggest
// `fn foo(t: impl Trait<A: Bound>)` instead.
// FIXME: we should suggest `fn foo(t: impl Trait<A: Bound>)` instead.
err.multipart_suggestion(
"introduce a type parameter with a trait bound instead of using `impl Trait`",
sugg,
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![cfg_attr(not(test), feature(coroutine_trait))]
#![cfg_attr(test, feature(panic_update_hook))]
#![cfg_attr(test, feature(test))]
#![feature(allocator_internals)]
#![feature(allow_internal_unstable)]
#![feature(associated_type_bounds)]
#![feature(c_unwind)]
#![feature(cfg_sanitize)]
#![feature(const_mut_refs)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![feature(allocator_api)]
#![feature(alloc_layout_extra)]
#![feature(iter_array_chunks)]
Expand All @@ -22,7 +23,6 @@
#![feature(try_reserve_kind)]
#![feature(try_with_capacity)]
#![feature(unboxed_closures)]
#![feature(associated_type_bounds)]
#![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_drain_sorted)]
#![feature(slice_ptr_get)]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
#![cfg_attr(bootstrap, feature(diagnostic_namespace))]
#![cfg_attr(bootstrap, feature(exhaustive_patterns))]
#![cfg_attr(bootstrap, feature(platform_intrinsics))]
Expand All @@ -213,7 +214,6 @@
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![feature(asm_const)]
#![feature(associated_type_bounds)]
#![feature(auto_traits)]
#![feature(c_unwind)]
#![feature(cfg_sanitize)]
Expand Down
1 change: 0 additions & 1 deletion tests/rustdoc-ui/issues/issue-110900.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ check-pass

#![crate_type="lib"]
#![feature(associated_type_bounds)]

trait A<'a> {}
trait B<'b> {}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/associated-consts/issue-93835.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ fn e() {
//~| ERROR cannot find value
//~| ERROR associated const equality
//~| ERROR cannot find trait `p` in this scope
//~| ERROR associated type bounds
}

fn main() {}
12 changes: 1 addition & 11 deletions tests/ui/associated-consts/issue-93835.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ LL | type_ascribe!(p, a<p:p<e=6>>);
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0405, E0412, E0425, E0658.
For more information about an error, try `rustc --explain E0405`.
2 changes: 0 additions & 2 deletions tests/ui/associated-type-bounds/ambiguous-associated-type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//@ check-pass

#![feature(associated_type_bounds)]

pub struct Flatten<I>
where
I: Iterator<Item: IntoIterator>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// Additionally, as reported in https://github.com/rust-lang/rust/issues/63594,
// we check that the spans for the error message are sane here.

#![feature(associated_type_bounds)]

fn main() {}

trait Bar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: associated type bounds are not allowed in `dyn` types
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:30:28
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:28:28
|
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
| ^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//@ check-pass

#![feature(associated_type_bounds)]

use std::fmt::Debug;
use std::iter::Once;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(associated_type_bounds)]

trait B {
type AssocType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: associated type bounds are not allowed in `dyn` types
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:7:19
|
LL | dyn for<'j> B<AssocType: 'j>:,
| ^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/associated-type-bounds/bad-universal-in-impl-sig.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(associated_type_bounds)]

trait Trait {
type Item;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: associated type bounds are not allowed in `dyn` types
--> $DIR/bad-universal-in-impl-sig.rs:10:16
--> $DIR/bad-universal-in-impl-sig.rs:8:16
|
LL | impl dyn Trait<Item: Trait2> {}
| ^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/associated-type-bounds/bounds-on-assoc-in-trait.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//@ check-pass

#![feature(associated_type_bounds)]

use std::fmt::Debug;
use std::iter::Empty;
use std::ops::Range;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/associated-type-bounds/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(associated_type_bounds)]

pub fn accept(_: impl Trait<K: Copy>) {}
//~^ ERROR expected type, found constant

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-type-bounds/consts.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: expected type, found constant
--> $DIR/consts.rs:3:29
--> $DIR/consts.rs:1:29
|
LL | pub fn accept(_: impl Trait<K: Copy>) {}
| ^------ bounds are not allowed on associated constants
| |
| unexpected constant
|
note: the associated constant is defined here
--> $DIR/consts.rs:7:5
--> $DIR/consts.rs:5:5
|
LL | const K: i32;
| ^^^^^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/associated-type-bounds/duplicate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(associated_type_bounds)]
#![feature(type_alias_impl_trait)]

use std::iter;
Expand Down
Loading

0 comments on commit 21d94a3

Please sign in to comment.