Auto merge of #58927 - GuillaumeGomez:default-keyword, r=QuietMisdreavus
Add default keyword handling in rustdoc Fixes #58898. r? @QuietMisdreavus
This commit is contained in:
@@ -508,6 +508,18 @@ impl Item {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
|
.or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
|
||||||
}
|
}
|
||||||
|
pub fn is_default(&self) -> bool {
|
||||||
|
match self.inner {
|
||||||
|
ItemEnum::MethodItem(ref meth) => {
|
||||||
|
if let Some(defaultness) = meth.defaultness {
|
||||||
|
defaultness.has_value() && !defaultness.is_final()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
||||||
@@ -1700,9 +1712,11 @@ pub struct Method {
|
|||||||
pub generics: Generics,
|
pub generics: Generics,
|
||||||
pub decl: FnDecl,
|
pub decl: FnDecl,
|
||||||
pub header: hir::FnHeader,
|
pub header: hir::FnHeader,
|
||||||
|
pub defaultness: Option<hir::Defaultness>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
|
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
|
||||||
|
Option<hir::Defaultness>) {
|
||||||
fn clean(&self, cx: &DocContext<'_>) -> Method {
|
fn clean(&self, cx: &DocContext<'_>) -> Method {
|
||||||
let (generics, decl) = enter_impl_trait(cx, || {
|
let (generics, decl) = enter_impl_trait(cx, || {
|
||||||
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
|
(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)
|
|||||||
decl,
|
decl,
|
||||||
generics,
|
generics,
|
||||||
header: self.0.header,
|
header: self.0.header,
|
||||||
|
defaultness: self.3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2016,7 +2031,7 @@ impl Clean<Item> for hir::TraitItem {
|
|||||||
default.map(|e| print_const_expr(cx, e)))
|
default.map(|e| print_const_expr(cx, e)))
|
||||||
}
|
}
|
||||||
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
|
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
|
||||||
MethodItem((sig, &self.generics, body).clean(cx))
|
MethodItem((sig, &self.generics, body, None).clean(cx))
|
||||||
}
|
}
|
||||||
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
|
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
|
||||||
let (generics, decl) = enter_impl_trait(cx, || {
|
let (generics, decl) = enter_impl_trait(cx, || {
|
||||||
@@ -2054,7 +2069,7 @@ impl Clean<Item> for hir::ImplItem {
|
|||||||
Some(print_const_expr(cx, expr)))
|
Some(print_const_expr(cx, expr)))
|
||||||
}
|
}
|
||||||
hir::ImplItemKind::Method(ref sig, body) => {
|
hir::ImplItemKind::Method(ref sig, body) => {
|
||||||
MethodItem((sig, &self.generics, body).clean(cx))
|
MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
|
||||||
}
|
}
|
||||||
hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
|
hir::ImplItemKind::Type(ref ty) => TypedefItem(Typedef {
|
||||||
type_: ty.clean(cx),
|
type_: ty.clean(cx),
|
||||||
@@ -2137,7 +2152,8 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
|
|||||||
abi: sig.abi(),
|
abi: sig.abi(),
|
||||||
constness,
|
constness,
|
||||||
asyncness: hir::IsAsync::NotAsync,
|
asyncness: hir::IsAsync::NotAsync,
|
||||||
}
|
},
|
||||||
|
defaultness: Some(self.defaultness),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
TyMethodItem(TyMethod {
|
TyMethodItem(TyMethod {
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ pub struct GenericBounds<'a>(pub &'a [clean::GenericBound]);
|
|||||||
/// Wrapper struct for emitting a comma-separated list of items
|
/// Wrapper struct for emitting a comma-separated list of items
|
||||||
pub struct CommaSep<'a, T>(pub &'a [T]);
|
pub struct CommaSep<'a, T>(pub &'a [T]);
|
||||||
pub struct AbiSpace(pub Abi);
|
pub struct AbiSpace(pub Abi);
|
||||||
|
pub struct DefaultSpace(pub bool);
|
||||||
|
|
||||||
/// Wrapper struct for properly emitting a function or method declaration.
|
/// Wrapper struct for properly emitting a function or method declaration.
|
||||||
pub struct Function<'a> {
|
pub struct Function<'a> {
|
||||||
@@ -1057,3 +1058,13 @@ impl fmt::Display for AbiSpace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DefaultSpace {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
if self.0 {
|
||||||
|
write!(f, "default ")
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ use crate::doctree;
|
|||||||
use crate::fold::DocFolder;
|
use crate::fold::DocFolder;
|
||||||
use crate::html::escape::Escape;
|
use crate::html::escape::Escape;
|
||||||
use crate::html::format::{AsyncSpace, ConstnessSpace};
|
use crate::html::format::{AsyncSpace, ConstnessSpace};
|
||||||
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace};
|
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
|
||||||
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
|
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
|
||||||
use crate::html::format::fmt_impl_for_trait_page;
|
use crate::html::format::fmt_impl_for_trait_page;
|
||||||
use crate::html::item_type::ItemType;
|
use crate::html::item_type::ItemType;
|
||||||
@@ -3429,11 +3429,12 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut header_len = format!(
|
let mut header_len = format!(
|
||||||
"{}{}{}{}{:#}fn {}{:#}",
|
"{}{}{}{}{}{:#}fn {}{:#}",
|
||||||
VisSpace(&meth.visibility),
|
VisSpace(&meth.visibility),
|
||||||
ConstnessSpace(header.constness),
|
ConstnessSpace(header.constness),
|
||||||
UnsafetySpace(header.unsafety),
|
UnsafetySpace(header.unsafety),
|
||||||
AsyncSpace(header.asyncness),
|
AsyncSpace(header.asyncness),
|
||||||
|
DefaultSpace(meth.is_default()),
|
||||||
AbiSpace(header.abi),
|
AbiSpace(header.abi),
|
||||||
name,
|
name,
|
||||||
*g
|
*g
|
||||||
@@ -3445,12 +3446,13 @@ fn render_assoc_item(w: &mut fmt::Formatter<'_>,
|
|||||||
(0, true)
|
(0, true)
|
||||||
};
|
};
|
||||||
render_attributes(w, meth)?;
|
render_attributes(w, meth)?;
|
||||||
write!(w, "{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
|
write!(w, "{}{}{}{}{}{}fn <a href='{href}' class='fnname'>{name}</a>\
|
||||||
{generics}{decl}{where_clause}",
|
{generics}{decl}{where_clause}",
|
||||||
VisSpace(&meth.visibility),
|
VisSpace(&meth.visibility),
|
||||||
ConstnessSpace(header.constness),
|
ConstnessSpace(header.constness),
|
||||||
UnsafetySpace(header.unsafety),
|
UnsafetySpace(header.unsafety),
|
||||||
AsyncSpace(header.asyncness),
|
AsyncSpace(header.asyncness),
|
||||||
|
DefaultSpace(meth.is_default()),
|
||||||
AbiSpace(header.abi),
|
AbiSpace(header.abi),
|
||||||
href = href,
|
href = href,
|
||||||
name = name,
|
name = name,
|
||||||
|
|||||||
15
src/test/rustdoc/default_trait_method.rs
Normal file
15
src/test/rustdoc/default_trait_method.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#![feature(specialization)]
|
||||||
|
|
||||||
|
pub trait Item {
|
||||||
|
fn foo();
|
||||||
|
fn bar();
|
||||||
|
}
|
||||||
|
|
||||||
|
// @has default_trait_method/trait.Item.html
|
||||||
|
// @has - '//*[@id="method.foo"]' 'default fn foo()'
|
||||||
|
// @has - '//*[@id="method.bar"]' 'fn bar()'
|
||||||
|
// @!has - '//*[@id="method.bar"]' 'default fn bar()'
|
||||||
|
impl<T: ?Sized> Item for T {
|
||||||
|
default fn foo() {}
|
||||||
|
fn bar() {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user