Skip to content

Commit 541ad45

Browse files
Add default keyword handling in rustdoc
1 parent 8bf1f1c commit 541ad45

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
@@ -507,6 +507,18 @@ impl Item {
507507
.as_ref()
508508
.or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
509509
}
510+
pub fn is_default(&self) -> bool {
511+
match self.inner {
512+
ItemEnum::MethodItem(ref meth) => {
513+
if let Some(defaultness) = meth.defaultness {
514+
defaultness.has_value() && !defaultness.is_final()
515+
} else {
516+
false
517+
}
518+
}
519+
_ => false,
520+
}
521+
}
510522
}
511523

512524
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -1699,9 +1711,11 @@ pub struct Method {
16991711
pub generics: Generics,
17001712
pub decl: FnDecl,
17011713
pub header: hir::FnHeader,
1714+
pub defaultness: Option<hir::Defaultness>,
17021715
}
17031716

1704-
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
1717+
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
1718+
Option<hir::Defaultness>) {
17051719
fn clean(&self, cx: &DocContext<'_>) -> Method {
17061720
let (generics, decl) = enter_impl_trait(cx, || {
17071721
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
@@ -1710,6 +1724,7 @@ impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId)
17101724
decl,
17111725
generics,
17121726
header: self.0.header,
1727+
defaultness: self.3,
17131728
}
17141729
}
17151730
}
@@ -2015,7 +2030,7 @@ impl Clean<Item> for hir::TraitItem {
20152030
default.map(|e| print_const_expr(cx, e)))
20162031
}
20172032
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
2018-
MethodItem((sig, &self.generics, body).clean(cx))
2033+
MethodItem((sig, &self.generics, body, None).clean(cx))
20192034
}
20202035
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
20212036
let (generics, decl) = enter_impl_trait(cx, || {
@@ -2053,7 +2068,7 @@ impl Clean<Item> for hir::ImplItem {
20532068
Some(print_const_expr(cx, expr)))
20542069
}
20552070
hir::ImplItemKind::Method(ref sig, body) => {
2056-
MethodItem((sig, &self.generics, body).clean(cx))
2071+
MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
20572072
}
20582073
hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
20592074
type_: ty.clean(cx),
@@ -2136,7 +2151,8 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
21362151
abi: sig.abi(),
21372152
constness,
21382153
asyncness: hir::IsAsync::NotAsync,
2139-
}
2154+
},
2155+
defaultness: Some(self.defaultness),
21402156
})
21412157
} else {
21422158
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;
@@ -3434,11 +3434,12 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
34343434
}
34353435
};
34363436
let mut header_len = format!(
3437-
"{}{}{}{}{:#}fn {}{:#}",
3437+
"{}{}{}{}{}{:#}fn {}{:#}",
34383438
VisSpace(&meth.visibility),
34393439
ConstnessSpace(header.constness),
34403440
UnsafetySpace(header.unsafety),
34413441
AsyncSpace(header.asyncness),
3442+
DefaultSpace(meth.is_default()),
34423443
AbiSpace(header.abi),
34433444
name,
34443445
*g
@@ -3450,12 +3451,13 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
34503451
(0, true)
34513452
};
34523453
render_attributes(w, meth)?;
3453-
write!(w, "{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
3454+
write!(w, "{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
34543455
{generics}{decl}{where_clause}",
34553456
VisSpace(&meth.visibility),
34563457
ConstnessSpace(header.constness),
34573458
UnsafetySpace(header.unsafety),
34583459
AsyncSpace(header.asyncness),
3460+
DefaultSpace(meth.is_default()),
34593461
AbiSpace(header.abi),
34603462
href = href,
34613463
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)