diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 61dbf00a1f536..114294cde4ee2 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -421,6 +421,9 @@ impl Item { pub fn is_enum(&self) -> bool { self.type_() == ItemType::Enum } + pub fn is_variant(&self) -> bool { + self.type_() == ItemType::Variant + } pub fn is_associated_type(&self) -> bool { self.type_() == ItemType::AssociatedType } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 445ce0637662d..982c033be99fd 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2596,7 +2596,15 @@ fn document_non_exhaustive_header(item: &clean::Item) -> &str { fn document_non_exhaustive(w: &mut fmt::Formatter<'_>, item: &clean::Item) -> fmt::Result { if item.is_non_exhaustive() { write!(w, "
", { - if item.is_struct() { "struct" } else if item.is_enum() { "enum" } else { "type" } + if item.is_struct() { + "struct" + } else if item.is_enum() { + "enum" + } else if item.is_variant() { + "variant" + } else { + "type" + } })?; if item.is_struct() { @@ -2609,6 +2617,10 @@ fn document_non_exhaustive(w: &mut fmt::Formatter<'_>, item: &clean::Item) -> fm write!(w, "Non-exhaustive enums could have additional variants added in future. \ Therefore, when matching against variants of non-exhaustive enums, an \ extra wildcard arm must be added to account for any future variants.")?; + } else if item.is_variant() { + write!(w, "Non-exhaustive enum variants could have additional fields added in future. \ + Therefore, non-exhaustive enum variants cannot be constructed in external \ + crates and cannot be matched against.")?; } else { write!(w, "This type will require a wildcard arm in any match statements or \ constructors.")?; @@ -3671,6 +3683,7 @@ fn item_enum(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item, } write!(w, "")?; document(w, cx, variant)?; + document_non_exhaustive(w, variant)?; use crate::clean::{Variant, VariantKind}; if let clean::VariantItem(Variant { diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index fef6910f40a57..85dc4d57337c8 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -2247,6 +2247,8 @@ if (!DOMTokenList.prototype.remove) { otherMessage += "struct"; } else if (hasClass(e, "non-exhaustive-enum")) { otherMessage += "enum"; + } else if (hasClass(e, "non-exhaustive-variant")) { + otherMessage += "enum variant"; } else if (hasClass(e, "non-exhaustive-type")) { otherMessage += "type"; } @@ -2264,6 +2266,9 @@ if (!DOMTokenList.prototype.remove) { if (hasClass(e, "type-decl") === true && showItemDeclarations === true) { collapseDocs(e.previousSibling.childNodes[0], "toggle"); } + if (hasClass(e, "non-exhaustive") === true) { + collapseDocs(e.previousSibling.childNodes[0], "toggle"); + } } }