Skip to content

Commit 48e354d

Browse files
committed
Auto merge of #58927 - GuillaumeGomez:default-keyword, r=QuietMisdreavus
Add default keyword handling in rustdoc Fixes #58898. r? @QuietMisdreavus
2 parents 15a5dfa + 541ad45 commit 48e354d

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/librustdoc/clean/mod.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,18 @@ impl Item {
508508
.as_ref()
509509
.or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
510510
}
511+
pub fn is_default(&self) -> bool {
512+
match self.inner {
513+
ItemEnum::MethodItem(ref meth) => {
514+
if let Some(defaultness) = meth.defaultness {
515+
defaultness.has_value() && !defaultness.is_final()
516+
} else {
517+
false
518+
}
519+
}
520+
_ => false,
521+
}
522+
}
511523
}
512524

513525
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -1700,9 +1712,11 @@ pub struct Method {
17001712
pub generics: Generics,
17011713
pub decl: FnDecl,
17021714
pub header: hir::FnHeader,
1715+
pub defaultness: Option<hir::Defaultness>,
17031716
}
17041717

1705-
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
1718+
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
1719+
Option<hir::Defaultness>) {
17061720
fn clean(&self, cx: &DocContext<'_>) -> Method {
17071721
let (generics, decl) = enter_impl_trait(cx, || {
17081722
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
@@ -1711,6 +1725,7 @@ impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId)
17111725
decl,
17121726
generics,
17131727
header: self.0.header,
1728+
defaultness: self.3,
17141729
}
17151730
}
17161731
}
@@ -2016,7 +2031,7 @@ impl Clean<Item> for hir::TraitItem {
20162031
default.map(|e| print_const_expr(cx, e)))
20172032
}
20182033
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2019-
MethodItem((sig, &self.generics, body).clean(cx))
2034+
MethodItem((sig, &self.generics, body, None).clean(cx))
20202035
}
20212036
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
20222037
let (generics, decl) = enter_impl_trait(cx, || {
@@ -2054,7 +2069,7 @@ impl Clean<Item> for hir::ImplItem {
20542069
Some(print_const_expr(cx, expr)))
20552070
}
20562071
hir::ImplItemKind::Method(ref sig, body) => {
2057-
MethodItem((sig, &self.generics, body).clean(cx))
2072+
MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
20582073
}
20592074
hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
20602075
type_: ty.clean(cx),
@@ -2137,7 +2152,8 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
21372152
abi: sig.abi(),
21382153
constness,
21392154
asyncness: hir::IsAsync::NotAsync,
2140-
}
2155+
},
2156+
defaultness: Some(self.defaultness),
21412157
})
21422158
} else {
21432159
TyMethodItem(TyMethod {

src/librustdoc/html/format.rs

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct GenericBounds<'a>(pub &'a [clean::GenericBound]);
4545
/// Wrapper struct for emitting a comma-separated list of items
4646
pub struct CommaSep<'a, T>(pub &'a [T]);
4747
pub struct AbiSpace(pub Abi);
48+
pub struct DefaultSpace(pub bool);
4849

4950
/// Wrapper struct for properly emitting a function or method declaration.
5051
pub struct Function<'a> {
@@ -1057,3 +1058,13 @@ impl fmt::Display for AbiSpace {
10571058
}
10581059
}
10591060
}
1061+
1062+
impl fmt::Display for DefaultSpace {
1063+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1064+
if self.0 {
1065+
write!(f, "default ")
1066+
} else {
1067+
Ok(())
1068+
}
1069+
}
1070+
}

src/librustdoc/html/render.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::doctree;
6161
use crate::fold::DocFolder;
6262
use crate::html::escape::Escape;
6363
use crate::html::format::{AsyncSpace, ConstnessSpace};
64-
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace};
64+
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
6565
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
6666
use crate::html::format::fmt_impl_for_trait_page;
6767
use crate::html::item_type::ItemType;
@@ -3429,11 +3429,12 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
34293429
}
34303430
};
34313431
let mut header_len = format!(
3432-
"{}{}{}{}{:#}fn {}{:#}",
3432+
"{}{}{}{}{}{:#}fn {}{:#}",
34333433
VisSpace(&meth.visibility),
34343434
ConstnessSpace(header.constness),
34353435
UnsafetySpace(header.unsafety),
34363436
AsyncSpace(header.asyncness),
3437+
DefaultSpace(meth.is_default()),
34373438
AbiSpace(header.abi),
34383439
name,
34393440
*g
@@ -3445,12 +3446,13 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
34453446
(0, true)
34463447
};
34473448
render_attributes(w, meth)?;
3448-
write!(w, "{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
3449+
write!(w, "{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
34493450
{generics}{decl}{where_clause}",
34503451
VisSpace(&meth.visibility),
34513452
ConstnessSpace(header.constness),
34523453
UnsafetySpace(header.unsafety),
34533454
AsyncSpace(header.asyncness),
3455+
DefaultSpace(meth.is_default()),
34543456
AbiSpace(header.abi),
34553457
href = href,
34563458
name = name,
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(specialization)]
2+
3+
pub trait Item {
4+
fn foo();
5+
fn bar();
6+
}
7+
8+
// @has default_trait_method/trait.Item.html
9+
// @has - '//*[@id="method.foo"]' 'default fn foo()'
10+
// @has - '//*[@id="method.bar"]' 'fn bar()'
11+
// @!has - '//*[@id="method.bar"]' 'default fn bar()'
12+
impl<T: ?Sized> Item for T {
13+
default fn foo() {}
14+
fn bar() {}
15+
}

0 commit comments

Comments
 (0)