From 25e9e650b685f5ab9eb0168c0e3e9bf81137d6a8 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Sat, 3 Sep 2022 01:16:09 +0800 Subject: [PATCH 1/5] Add trait_upcasting related languages changes --- src/type-coercions.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 26e27eb1d..5dfa252ae 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -195,7 +195,7 @@ r[coerce.unsize] r[coerce.unsize.intro] The following coercions are called `unsized coercions`, since they -relate to converting sized types to unsized types, and are permitted in a few +relate to converting types to unsized types, and are permitted in a few cases where other coercions are not, as described above. They can still happen anywhere else a coercion can occur. @@ -211,6 +211,9 @@ r[coerce.unsize.slice] r[coerce.unsize.trait-object] * `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible]. +r[coerce.unsize.trait-upcast] +* `dyn T` to `dyn U`, when `T` has `U` as one of its ancestor trait. + r[coerce.unsized.composite] * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: * `Foo` is a struct. From 5e57afccb05b82309a66bad241ebaab5f809c5d9 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Wed, 22 Nov 2023 13:07:36 +0800 Subject: [PATCH 2/5] Address review comments. Co-authored-by: Michael Goulet --- src/type-coercions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 5dfa252ae..b4dfc4a3a 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -212,7 +212,7 @@ r[coerce.unsize.trait-object] * `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible]. r[coerce.unsize.trait-upcast] -* `dyn T` to `dyn U`, when `T` has `U` as one of its ancestor trait. +* `dyn T` to `dyn U`, when `U` is one of `T`'s supertraits. r[coerce.unsized.composite] * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: From 7451a0e9063c0a026df14f02f9c5832def2bd7c0 Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Fri, 20 Sep 2024 10:33:24 +0200 Subject: [PATCH 3/5] Add information about casting pointers to unsized types This aligns the reference with the results of r-l/r/120248. --- src/expressions/operator-expr.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 530d41d10..383086a78 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -385,7 +385,11 @@ reference types and `mut` or `const` in pointer types. | [Function pointer] | Integer | Function pointer to address cast | | Closure \*\*\* | Function pointer | Closure to function pointer cast | -\* or `T` and `V` are compatible unsized types, e.g., both slices, both the same trait object. +\* or `T` and `V` are unsized types with compatible metadata: +* both slice metadata (`*[u16]` -> `*[u8]`, `*str` -> `*(u8, [u32])`). +* both the same trait object metadata, modulo dropping auto traits (`*dyn Debug` -> `*(u16, dyn Debug)`, `*dyn Debug + Send` -> `*dyn Debug`). + * **Note**: *adding* auto traits is not allowed (`*dyn Debug` -> `*dyn Debug + Send` is invalid). + * **Note**: generics (including lifetimes) must match (`*dyn T<'a, A>` -> `*dyn T<'b, B>` requires `'a = 'b` and `A = B`). \*\* only when `m₁` is `mut` or `m₂` is `const`. Casting `mut` reference to `const` pointer is allowed. From a2624a2c18ab191fe764e701356e96056fabb461 Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Fri, 20 Sep 2024 10:35:17 +0200 Subject: [PATCH 4/5] Apply review comments to trait upcasting description --- src/type-coercions.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index b4dfc4a3a..0433a5009 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -199,6 +199,10 @@ relate to converting types to unsized types, and are permitted in a few cases where other coercions are not, as described above. They can still happen anywhere else a coercion can occur. +r[coerce.unsize.misnomer] +> Note: "unsizing" is a bit of a misnomer, +> since this covers unsized->unsized coercions too. + r[coerce.unsize.trait] Two traits, [`Unsize`] and [`CoerceUnsized`], are used to assist in this process and expose it for library use. The following @@ -212,7 +216,8 @@ r[coerce.unsize.trait-object] * `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible]. r[coerce.unsize.trait-upcast] -* `dyn T` to `dyn U`, when `U` is one of `T`'s supertraits. +* `dyn T` to `dyn U`, when `U` is one of `T`'s [supertraits]. + * This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed. r[coerce.unsized.composite] * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: @@ -330,3 +335,4 @@ precisely. [`Unsize`]: std::marker::Unsize [`CoerceUnsized`]: std::ops::CoerceUnsized [method-call expressions]: expressions/method-call-expr.md +[supertraits]: items/traits.md#supertraits From 6c4f538fad1e380c5ac08a8c2f72d9425607801a Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Sat, 21 Sep 2024 11:22:32 +0200 Subject: [PATCH 5/5] Mention that you can add auto traits if principal has them as supers i.e. document the behavior after r-l/r/119338 --- src/expressions/operator-expr.md | 3 ++- src/type-coercions.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 383086a78..09ff8ed49 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -388,7 +388,8 @@ reference types and `mut` or `const` in pointer types. \* or `T` and `V` are unsized types with compatible metadata: * both slice metadata (`*[u16]` -> `*[u8]`, `*str` -> `*(u8, [u32])`). * both the same trait object metadata, modulo dropping auto traits (`*dyn Debug` -> `*(u16, dyn Debug)`, `*dyn Debug + Send` -> `*dyn Debug`). - * **Note**: *adding* auto traits is not allowed (`*dyn Debug` -> `*dyn Debug + Send` is invalid). + * **Note**: *adding* auto traits is only allowed if the principal trait has the auto trait as a super trait + (given `trait T: Send {}`, `*dyn T` -> `*dyn T + Send` is valid, but `*dyn Debug` -> `*dyn Debug + Send` is not). * **Note**: generics (including lifetimes) must match (`*dyn T<'a, A>` -> `*dyn T<'b, B>` requires `'a = 'b` and `A = B`). \*\* only when `m₁` is `mut` or `m₂` is `const`. Casting `mut` reference to diff --git a/src/type-coercions.md b/src/type-coercions.md index 0433a5009..f02abd9e4 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -218,6 +218,8 @@ r[coerce.unsize.trait-object] r[coerce.unsize.trait-upcast] * `dyn T` to `dyn U`, when `U` is one of `T`'s [supertraits]. * This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed. + * This allows adding auto traits if the principal trait has the auto trait as a super trait, + i.e. given `trait T: U + Send {}`, `dyn T` to `dyn T + Send` or to `dyn U + Send` coercions are allowed. r[coerce.unsized.composite] * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: