mirror of
https://github.com/rust-lang/rust.git
synced 2025-10-05 03:36:21 +00:00

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
93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
|
|
#![feature(allocator_api)]
|
|
#![feature(alloc_layout_extra)]
|
|
#![feature(iter_array_chunks)]
|
|
#![feature(assert_matches)]
|
|
#![feature(btree_extract_if)]
|
|
#![feature(cow_is_borrowed)]
|
|
#![feature(const_cow_is_borrowed)]
|
|
#![feature(const_heap)]
|
|
#![feature(const_mut_refs)]
|
|
#![feature(const_slice_from_raw_parts_mut)]
|
|
#![feature(const_ptr_write)]
|
|
#![feature(const_try)]
|
|
#![feature(core_intrinsics)]
|
|
#![feature(extract_if)]
|
|
#![feature(exact_size_is_empty)]
|
|
#![feature(generic_nonzero)]
|
|
#![feature(linked_list_cursors)]
|
|
#![feature(map_try_insert)]
|
|
#![feature(new_uninit)]
|
|
#![feature(pattern)]
|
|
#![feature(trusted_len)]
|
|
#![feature(try_reserve_kind)]
|
|
#![feature(try_with_capacity)]
|
|
#![feature(unboxed_closures)]
|
|
#![feature(binary_heap_into_iter_sorted)]
|
|
#![feature(binary_heap_drain_sorted)]
|
|
#![feature(slice_ptr_get)]
|
|
#![feature(binary_heap_as_slice)]
|
|
#![feature(inplace_iteration)]
|
|
#![feature(iter_advance_by)]
|
|
#![feature(iter_next_chunk)]
|
|
#![feature(round_char_boundary)]
|
|
#![feature(slice_partition_dedup)]
|
|
#![feature(string_remove_matches)]
|
|
#![feature(const_btree_len)]
|
|
#![feature(const_trait_impl)]
|
|
#![feature(const_str_from_utf8)]
|
|
#![feature(panic_update_hook)]
|
|
#![feature(pointer_is_aligned)]
|
|
#![feature(slice_flatten)]
|
|
#![feature(thin_box)]
|
|
#![feature(strict_provenance)]
|
|
#![feature(drain_keep_rest)]
|
|
#![feature(local_waker)]
|
|
#![allow(internal_features)]
|
|
#![deny(fuzzy_provenance_casts)]
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
|
|
|
use std::hash::{DefaultHasher, Hash, Hasher};
|
|
|
|
mod arc;
|
|
mod autotraits;
|
|
mod borrow;
|
|
mod boxed;
|
|
mod btree_set_hash;
|
|
mod c_str;
|
|
mod const_fns;
|
|
mod cow_str;
|
|
mod fmt;
|
|
mod heap;
|
|
mod linked_list;
|
|
mod rc;
|
|
mod slice;
|
|
mod str;
|
|
mod string;
|
|
mod task;
|
|
mod thin_box;
|
|
mod vec;
|
|
mod vec_deque;
|
|
|
|
fn hash<T: Hash>(t: &T) -> u64 {
|
|
let mut s = DefaultHasher::new();
|
|
t.hash(&mut s);
|
|
s.finish()
|
|
}
|
|
|
|
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
|
|
// See https://github.com/kripken/emscripten-fastcomp/issues/169
|
|
#[cfg(not(target_os = "emscripten"))]
|
|
#[test]
|
|
fn test_boxed_hasher() {
|
|
let ordinary_hash = hash(&5u32);
|
|
|
|
let mut hasher_1 = Box::new(DefaultHasher::new());
|
|
5u32.hash(&mut hasher_1);
|
|
assert_eq!(ordinary_hash, hasher_1.finish());
|
|
|
|
let mut hasher_2 = Box::new(DefaultHasher::new()) as Box<dyn Hasher>;
|
|
5u32.hash(&mut hasher_2);
|
|
assert_eq!(ordinary_hash, hasher_2.finish());
|
|
}
|