Skip to content

Commit

Permalink
Support non-exhaustive enum variants in rustdoc.
Browse files Browse the repository at this point in the history
This commit adds support for non-exhaustive enum variants in rustdoc,
extending the existing support for non-exhaustive enums and structs.
  • Loading branch information
davidtwco committed Mar 29, 2019
1 parent 1893841 commit 49a6da2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion src/librustdoc/html/render.rs
Expand Up @@ -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, "<div class='docblock non-exhaustive non-exhaustive-{}'>", {
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() {
Expand All @@ -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.")?;
Expand Down Expand Up @@ -3671,6 +3683,7 @@ fn item_enum(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
}
write!(w, "</code></span>")?;
document(w, cx, variant)?;
document_non_exhaustive(w, variant)?;

use crate::clean::{Variant, VariantKind};
if let clean::VariantItem(Variant {
Expand Down
5 changes: 5 additions & 0 deletions src/librustdoc/html/static/main.js
Expand Up @@ -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";
}
Expand All @@ -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");
}
}
}

Expand Down

0 comments on commit 49a6da2

Please sign in to comment.