From 18938416e4d34d7f7d64d11decd87ce47036bb75 Mon Sep 17 00:00:00 2001 From: David Wood Date: Sat, 23 Mar 2019 02:35:22 +0100 Subject: [PATCH] Update documentation. This commit updates the unstable book and diagnostics to reflect that the `#[non_exhaustive]` attribute is now available for enum variants. --- .../src/language-features/non-exhaustive.md | 11 ++++++----- src/librustc_typeck/diagnostics.rs | 13 +++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/non-exhaustive.md b/src/doc/unstable-book/src/language-features/non-exhaustive.md index f9840e1b83f2..907147c17ef8 100644 --- a/src/doc/unstable-book/src/language-features/non-exhaustive.md +++ b/src/doc/unstable-book/src/language-features/non-exhaustive.md @@ -7,10 +7,12 @@ The tracking issue for this feature is: [#44109] ------------------------ The `non_exhaustive` gate allows you to use the `#[non_exhaustive]` attribute -on structs and enums. When applied within a crate, users of the crate will need -to use the `_` pattern when matching enums and use the `..` pattern when -matching structs. Structs marked as `non_exhaustive` will not be able to be -created normally outside of the defining crate. This is demonstrated below: +on structs, enums and enum variants. When applied within a crate, users of the +crate will need to use the `_` pattern when matching enums and use the `..` +pattern when matching structs. Enum variants cannot be matched against. +Structs and enum variants marked as `non_exhaustive` will not be able to +be created normally outside of the defining crate. This is demonstrated +below: ```rust,ignore (pseudo-Rust) use std::error::Error as StdError; @@ -72,4 +74,3 @@ let config = Config { window_width: 640, window_height: 480 }; // when marked non_exhaustive. let &Config { window_width, window_height, .. } = config; ``` - diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index cde37fb23c32..22f24df450f4 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4341,11 +4341,12 @@ foo.method(); // Ok! "##, E0638: r##" -This error indicates that the struct or enum must be matched non-exhaustively -as it has been marked as `non_exhaustive`. +This error indicates that the struct, enum or enum variant must be matched +non-exhaustively as it has been marked as `non_exhaustive`. When applied within a crate, downstream users of the crate will need to use the `_` pattern when matching enums and use the `..` pattern when matching structs. +Downstream crates cannot match against non-exhaustive enum variants. For example, in the below example, since the enum is marked as `non_exhaustive`, it is required that downstream crates match non-exhaustively @@ -4390,10 +4391,10 @@ Similarly, for structs, match with `..` to avoid this error. "##, E0639: r##" -This error indicates that the struct or enum cannot be instantiated from -outside of the defining crate as it has been marked as `non_exhaustive` and as -such more fields/variants may be added in future that could cause adverse side -effects for this code. +This error indicates that the struct, enum or enum variant cannot be +instantiated from outside of the defining crate as it has been marked +as `non_exhaustive` and as such more fields/variants may be added in +future that could cause adverse side effects for this code. It is recommended that you look for a `new` function or equivalent in the crate's documentation.