From 69af035f6e8fbff6e4e2303604a27d8c924fcf25 Mon Sep 17 00:00:00 2001 From: Havvy Date: Sun, 20 May 2018 20:26:05 -0700 Subject: [PATCH 1/8] Remove incorrect coercion. Define type coercions. --- src/type-coercions.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 377afe769..9056be142 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -1,10 +1,10 @@ # Type coercions -Coercions are defined in [RFC 401]. [RFC 1558] then expanded on that. -A coercion is implicit and has no syntax. +**Type coercions** are implicit changes of the type of a value. They happen +automatically at specific locations and are highly restricted in what types +actually coerce. -[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md -[RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md +Coercions are originally defined in [RFC 401] and expanded upon in [RFC 1558]. ## Coercion sites @@ -21,7 +21,7 @@ sites are: let _: i8 = 42; ``` -* `static` and `const` statements (similar to `let` statements). +* `static` and `const` items (similar to `let` statements). * Arguments for function calls @@ -41,7 +41,7 @@ sites are: For method calls, the receiver (`self` parameter) can only take advantage of [unsized coercions](#unsized-coercions). -* Instantiations of struct or variant fields +* Instantiations of struct, union, or enum variant fields For example, `42` is coerced to have type `i8` in the following: @@ -53,7 +53,7 @@ sites are: } ``` -* Function results, either the final line of a block if it is not +* Function results – either the final line of a block if it is not semicolon-terminated or any expression in a `return` statement For example, `42` is coerced to have type `i8` in the following: @@ -91,7 +91,7 @@ the block has a known type. Coercion is allowed between the following types: -* `T` to `U` if `T` is a subtype of `U` (*reflexive case*) +* `T` to `U` if `T` is a [subtype] of `U` (*reflexive case*) * `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to `T_3` (*transitive case*) @@ -164,8 +164,7 @@ an implementation of `Unsize` for `T` will be provided: * `[T; n]` to `[T]`. -* `T` to `U`, when `U` is a trait object type and either `T` implements `U` or - `T` is a trait object for a subtrait of `U`. +* `T` to `dyn U`, when `T` implements `U + Sized` * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: * `Foo` is a struct. @@ -182,5 +181,8 @@ unsized coercion to `Foo`. > has been stabilized, the traits themselves are not yet stable and therefore > can't be used directly in stable Rust. +[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md +[RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md +[subtype]: subtyping.html [`Unsize`]: ../std/marker/trait.Unsize.html [`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html From 8983dcc7499458334155f9ed47973142fa39fda5 Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Tue, 30 Jun 2020 15:30:50 -0600 Subject: [PATCH 2/8] Changed invalid coersion examples --- src/type-coercions.md | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 9056be142..9902af165 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -1,8 +1,8 @@ # Type coercions -**Type coercions** are implicit changes of the type of a value. They happen -automatically at specific locations and are highly restricted in what types -actually coerce. +**Type coercions** are implicit operations that change the type of a value. +They happen automatically at specific locations and are highly restricted in +what types actually coerce. Coercions are originally defined in [RFC 401] and expanded upon in [RFC 1558]. @@ -15,26 +15,26 @@ sites are: * `let` statements where an explicit type is given. - For example, `42` is coerced to have type `i8` in the following: + For example, `&mut 42` is coerced to have type `&i8` in the following: ```rust - let _: i8 = 42; + let _: &i8 = &mut 42; ``` -* `static` and `const` items (similar to `let` statements). +* `static` and `const` item declarations (similar to `let` statements). * Arguments for function calls The value being coerced is the actual parameter, and it is coerced to the type of the formal parameter. - For example, `42` is coerced to have type `i8` in the following: + For example, `&mut 42` is coerced to have type `&i8` in the following: ```rust - fn bar(_: i8) { } + fn bar(_: &i8) { } fn main() { - bar(42); + bar(&mut 42); } ``` @@ -43,26 +43,30 @@ sites are: * Instantiations of struct, union, or enum variant fields - For example, `42` is coerced to have type `i8` in the following: + For example, `&mut 42` is coerced to have type `&i8` in the following: ```rust - struct Foo { x: i8 } + struct Foo { x: &i8 } fn main() { - Foo { x: 42 }; + Foo { x: &mut 42 }; } ``` + + (Note that lifetime specifiers on `struct Foo` have been omitted for brevity.) -* Function results – either the final line of a block if it is not +* Function results–either the final line of a block if it is not semicolon-terminated or any expression in a `return` statement - For example, `42` is coerced to have type `i8` in the following: + For example, `x` is coerced to have type `&dyn Display` in the following: ```rust - fn foo() -> i8 { - 42 + fn foo(x: &u32) -> &dyn Display { + x } ``` +* The [as] type cast operator can also explicitly perform type coersion. + If the expression in one of these coercion sites is a coercion-propagating expression, then the relevant sub-expressions in that expression are also @@ -183,6 +187,7 @@ unsized coercion to `Foo`. [RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md [RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md -[subtype]: subtyping.html +[subtype]: subtyping.html` +[as]: operator-expr.html#type-cast-expressions` [`Unsize`]: ../std/marker/trait.Unsize.html [`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html From 79e05895e8f7ecf945cee1ec6806a117d52af55f Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Tue, 30 Jun 2020 16:29:37 -0600 Subject: [PATCH 3/8] Fixed code to pass ci --- src/type-coercions.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 9902af165..17c641fae 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -46,25 +46,25 @@ sites are: For example, `&mut 42` is coerced to have type `&i8` in the following: ```rust - struct Foo { x: &i8 } + struct Foo<'a> { x: &'a i8 } fn main() { Foo { x: &mut 42 }; } ``` - (Note that lifetime specifiers on `struct Foo` have been omitted for brevity.) - * Function results–either the final line of a block if it is not semicolon-terminated or any expression in a `return` statement For example, `x` is coerced to have type `&dyn Display` in the following: ```rust + use std::fmt::Display; fn foo(x: &u32) -> &dyn Display { x } ``` + * The [as] type cast operator can also explicitly perform type coersion. @@ -187,7 +187,7 @@ unsized coercion to `Foo`. [RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md [RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md -[subtype]: subtyping.html` -[as]: operator-expr.html#type-cast-expressions` +[subtype]: subtyping.md +[as]: expressions/operator-expr.md#type-cast-expressions [`Unsize`]: ../std/marker/trait.Unsize.html [`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html From 51648161e23438b43b88d6bbb761e4a87ce4d92e Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt <44003709+AloeareV@users.noreply.github.com> Date: Tue, 7 Jul 2020 12:59:21 -0600 Subject: [PATCH 4/8] Apply suggestions from code review Co-authored-by: Eric Huss --- src/type-coercions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 17c641fae..3caa41c0a 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -53,7 +53,7 @@ sites are: } ``` -* Function results–either the final line of a block if it is not +* Function results—either the final line of a block if it is not semicolon-terminated or any expression in a `return` statement For example, `x` is coerced to have type `&dyn Display` in the following: @@ -65,7 +65,7 @@ sites are: } ``` -* The [as] type cast operator can also explicitly perform type coersion. +* The [as] type cast operator can also explicitly perform type coercion. If the expression in one of these coercion sites is a coercion-propagating From 95383ec3d2fb2287e205d0ea31c7a51f6dcee445 Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Tue, 7 Jul 2020 13:04:45 -0600 Subject: [PATCH 5/8] Removed trailing whitespace, added Object Safety clause --- src/type-coercions.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 3caa41c0a..ae9924e11 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -52,7 +52,7 @@ sites are: Foo { x: &mut 42 }; } ``` - + * Function results—either the final line of a block if it is not semicolon-terminated or any expression in a `return` statement @@ -168,7 +168,7 @@ an implementation of `Unsize` for `T` will be provided: * `[T; n]` to `[T]`. -* `T` to `dyn U`, when `T` implements `U + Sized` +* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [object safe]. * `Foo<..., T, ...>` to `Foo<..., U, ...>`, when: * `Foo` is a struct. @@ -188,6 +188,7 @@ unsized coercion to `Foo`. [RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md [RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md [subtype]: subtyping.md +[object safe]: items/traits.md#object-safety [as]: expressions/operator-expr.md#type-cast-expressions [`Unsize`]: ../std/marker/trait.Unsize.html [`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html From 023b0dbc87d3f83c006173dd48f027bfcd9da15f Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Thu, 9 Jul 2020 13:20:30 -0600 Subject: [PATCH 6/8] Clarified as operator as not coercion site --- src/type-coercions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index ae9924e11..a6fedc0fd 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -65,7 +65,8 @@ sites are: } ``` -* The [as] type cast operator can also explicitly perform type coercion. +* The [as] type cast operator—while not a coercion site—can be used to + explicitly perform type coercion. If the expression in one of these coercion sites is a coercion-propagating From 21bffc242107df72d74cdaabe548409ecee71def Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Tue, 25 Aug 2020 11:51:14 -0600 Subject: [PATCH 7/8] Moved type cast mention to introduction --- src/type-coercions.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index a6fedc0fd..fc856cf8d 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -4,8 +4,12 @@ They happen automatically at specific locations and are highly restricted in what types actually coerce. +The [type cast operator], `as`, is not a coersion site. However, most type +conversions allowed by coersion can also be explicitly performed by `as`. + Coercions are originally defined in [RFC 401] and expanded upon in [RFC 1558]. + ## Coercion sites A coercion can only occur at certain coercion sites in a program; these are @@ -65,10 +69,6 @@ sites are: } ``` -* The [as] type cast operator—while not a coercion site—can be used to - explicitly perform type coercion. - - If the expression in one of these coercion sites is a coercion-propagating expression, then the relevant sub-expressions in that expression are also coercion sites. Propagation recurses from these new coercion sites. @@ -190,6 +190,6 @@ unsized coercion to `Foo`. [RFC 1558]: https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md [subtype]: subtyping.md [object safe]: items/traits.md#object-safety -[as]: expressions/operator-expr.md#type-cast-expressions +[type cast operator]: expressions/operator-expr.md#type-cast-expressions [`Unsize`]: ../std/marker/trait.Unsize.html [`CoerceUnsized`]: ../std/ops/trait.CoerceUnsized.html From d5372c951cc37d76dc5d01e91ebfafdb30e3f50d Mon Sep 17 00:00:00 2001 From: Gygaxis Vainhardt Date: Thu, 27 Aug 2020 13:40:03 -0600 Subject: [PATCH 8/8] Reworded 'as' mention --- src/type-coercions.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index fc856cf8d..d578e042a 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -4,12 +4,11 @@ They happen automatically at specific locations and are highly restricted in what types actually coerce. -The [type cast operator], `as`, is not a coersion site. However, most type -conversions allowed by coersion can also be explicitly performed by `as`. +Any conversions allowed by coercion can also be explicitly performed by the +[type cast operator], `as`. Coercions are originally defined in [RFC 401] and expanded upon in [RFC 1558]. - ## Coercion sites A coercion can only occur at certain coercion sites in a program; these are