Skip to content

Commit 49a6da2

Browse files
committed
Support non-exhaustive enum variants in rustdoc.
This commit adds support for non-exhaustive enum variants in rustdoc, extending the existing support for non-exhaustive enums and structs.
1 parent 1893841 commit 49a6da2

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/librustdoc/clean/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ impl Item {
421421
pub fn is_enum(&self) -> bool {
422422
self.type_() == ItemType::Enum
423423
}
424+
pub fn is_variant(&self) -> bool {
425+
self.type_() == ItemType::Variant
426+
}
424427
pub fn is_associated_type(&self) -> bool {
425428
self.type_() == ItemType::AssociatedType
426429
}

src/librustdoc/html/render.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,15 @@ fn document_non_exhaustive_header(item: &clean::Item) -> &str {
25962596
fn document_non_exhaustive(w: &mut fmt::Formatter<'_>, item: &clean::Item) -> fmt::Result {
25972597
if item.is_non_exhaustive() {
25982598
write!(w, "<div class='docblock non-exhaustive non-exhaustive-{}'>", {
2599-
if item.is_struct() { "struct" } else if item.is_enum() { "enum" } else { "type" }
2599+
if item.is_struct() {
2600+
"struct"
2601+
} else if item.is_enum() {
2602+
"enum"
2603+
} else if item.is_variant() {
2604+
"variant"
2605+
} else {
2606+
"type"
2607+
}
26002608
})?;
26012609

26022610
if item.is_struct() {
@@ -2609,6 +2617,10 @@ fn document_non_exhaustive(w: &mut fmt::Formatter<'_>, item: &clean::Item) -> fm
26092617
write!(w, "Non-exhaustive enums could have additional variants added in future. \
26102618
Therefore, when matching against variants of non-exhaustive enums, an \
26112619
extra wildcard arm must be added to account for any future variants.")?;
2620+
} else if item.is_variant() {
2621+
write!(w, "Non-exhaustive enum variants could have additional fields added in future. \
2622+
Therefore, non-exhaustive enum variants cannot be constructed in external \
2623+
crates and cannot be matched against.")?;
26122624
} else {
26132625
write!(w, "This type will require a wildcard arm in any match statements or \
26142626
constructors.")?;
@@ -3671,6 +3683,7 @@ fn item_enum(w: &mut fmt::Formatter<'_>, cx: &Context, it: &clean::Item,
36713683
}
36723684
write!(w, "</code></span>")?;
36733685
document(w, cx, variant)?;
3686+
document_non_exhaustive(w, variant)?;
36743687

36753688
use crate::clean::{Variant, VariantKind};
36763689
if let clean::VariantItem(Variant {

src/librustdoc/html/static/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,8 @@ if (!DOMTokenList.prototype.remove) {
22472247
otherMessage += "struct";
22482248
} else if (hasClass(e, "non-exhaustive-enum")) {
22492249
otherMessage += "enum";
2250+
} else if (hasClass(e, "non-exhaustive-variant")) {
2251+
otherMessage += "enum variant";
22502252
} else if (hasClass(e, "non-exhaustive-type")) {
22512253
otherMessage += "type";
22522254
}
@@ -2264,6 +2266,9 @@ if (!DOMTokenList.prototype.remove) {
22642266
if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
22652267
collapseDocs(e.previousSibling.childNodes[0], "toggle");
22662268
}
2269+
if (hasClass(e, "non-exhaustive") === true) {
2270+
collapseDocs(e.previousSibling.childNodes[0], "toggle");
2271+
}
22672272
}
22682273
}
22692274

0 commit comments

Comments
 (0)