Skip to content

Commit d021503

Browse files
authored
Unrolled build for rust-lang#117495
Rollup merge of rust-lang#117495 - compiler-errors:unsize-docs, r=lcnr Clarify `Unsize` documentation The documentation erroneously says that: ```rust /// - Types implementing a trait `Trait` also implement `Unsize<dyn Trait>`. /// - Structs `Foo<..., T, ...>` implement `Unsize<Foo<..., U, ...>>` if all of these conditions /// are met: /// - `T: Unsize<U>`. /// - Only the last field of `Foo` has a type involving `T`. /// - `Bar<T>: Unsize<Bar<U>>`, where `Bar<T>` stands for the actual type of that last field. ``` Specifically, `T: Unsize<U>` is not required to hold -- only the final field must implement `FinalField<T>: Unsize<FinalField<U>>`. This can be demonstrated by the test I added. --- Second commit fleshes out the documentation a lot more.
2 parents b800c30 + 1221b7b commit d021503

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

library/core/src/marker.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,18 @@ pub trait Sized {
155155
/// Those implementations are:
156156
///
157157
/// - Arrays `[T; N]` implement `Unsize<[T]>`.
158-
/// - Types implementing a trait `Trait` also implement `Unsize<dyn Trait>`.
159-
/// - Structs `Foo<..., T, ...>` implement `Unsize<Foo<..., U, ...>>` if all of these conditions
160-
/// are met:
161-
/// - `T: Unsize<U>`.
162-
/// - Only the last field of `Foo` has a type involving `T`.
163-
/// - `Bar<T>: Unsize<Bar<U>>`, where `Bar<T>` stands for the actual type of that last field.
158+
/// - A type implements `Unsize<dyn Trait + 'a>` if all of these conditions are met:
159+
/// - The type implements `Trait`.
160+
/// - `Trait` is object safe.
161+
/// - The type is sized.
162+
/// - The type outlives `'a`.
163+
/// - Structs `Foo<..., T1, ..., Tn, ...>` implement `Unsize<Foo<..., U1, ..., Un, ...>>`
164+
/// where any number of (type and const) parameters may be changed if all of these conditions
165+
/// are met:
166+
/// - Only the last field of `Foo` has a type involving the parameters `T1`, ..., `Tn`.
167+
/// - All other parameters of the struct are equal.
168+
/// - `Field<T1, ..., Tn>: Unsize<Field<U1, ..., Un>>`, where `Field<...>` stands for the actual
169+
/// type of the struct's last field.
164170
///
165171
/// `Unsize` is used along with [`ops::CoerceUnsized`] to allow
166172
/// "user-defined" containers such as [`Rc`] to contain dynamically-sized
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// check-pass
2+
3+
struct Foo<T, U>
4+
where
5+
(T, U): Trait,
6+
{
7+
f: <(T, U) as Trait>::Assoc,
8+
}
9+
10+
trait Trait {
11+
type Assoc: ?Sized;
12+
}
13+
14+
struct Count<const N: usize>;
15+
16+
impl<const N: usize> Trait for (i32, Count<N>) {
17+
type Assoc = [(); N];
18+
}
19+
20+
impl<'a> Trait for (u32, ()) {
21+
type Assoc = [()];
22+
}
23+
24+
// Test that we can unsize several trait params in creative ways.
25+
fn unsize<const N: usize>(x: &Foo<i32, Count<N>>) -> &Foo<u32, ()> {
26+
x
27+
}
28+
29+
fn main() {}

0 commit comments

Comments
 (0)