Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustdoc support for universal_impl_trait #49304

Merged
merged 4 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ pub struct TyParam {
pub did: DefId,
pub bounds: Vec<TyParamBound>,
pub default: Option<Type>,
pub synthetic: Option<hir::SyntheticTyParamKind>,
}

impl Clean<TyParam> for hir::TyParam {
Expand All @@ -1246,6 +1247,7 @@ impl Clean<TyParam> for hir::TyParam {
did: cx.tcx.hir.local_def_id(self.id),
bounds: self.bounds.clean(cx),
default: self.default.clean(cx),
synthetic: self.synthetic,
}
}
}
Expand All @@ -1261,7 +1263,8 @@ impl<'tcx> Clean<TyParam> for ty::TypeParameterDef {
Some(cx.tcx.type_of(self.def_id).clean(cx))
} else {
None
}
},
synthetic: None,
}
}
}
Expand Down Expand Up @@ -1629,6 +1632,16 @@ pub enum GenericParam {
Type(TyParam),
}

impl GenericParam {
pub fn is_synthetic_type_param(&self) -> bool {
if let GenericParam::Type(ref t) = *self {
t.synthetic.is_some()
} else {
false
}
}
}

impl Clean<GenericParam> for hir::GenericParam {
fn clean(&self, cx: &DocContext) -> GenericParam {
match *self {
Expand Down Expand Up @@ -1761,11 +1774,12 @@ pub struct Method {

impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId) {
fn clean(&self, cx: &DocContext) -> Method {
let generics = self.1.clean(cx);
Method {
generics: self.1.clean(cx),
decl: enter_impl_trait(cx, &generics.params, || (&*self.0.decl, self.2).clean(cx)),
generics,
unsafety: self.0.unsafety,
constness: self.0.constness,
decl: (&*self.0.decl, self.2).clean(cx),
abi: self.0.abi
}
}
Expand All @@ -1790,6 +1804,8 @@ pub struct Function {

impl Clean<Item> for doctree::Function {
fn clean(&self, cx: &DocContext) -> Item {
let generics = self.generics.clean(cx);
let decl = enter_impl_trait(cx, &generics.params, || (&self.decl, self.body).clean(cx));
Item {
name: Some(self.name.clean(cx)),
attrs: self.attrs.clean(cx),
Expand All @@ -1799,8 +1815,8 @@ impl Clean<Item> for doctree::Function {
deprecation: self.depr.clean(cx),
def_id: cx.tcx.hir.local_def_id(self.id),
inner: FunctionItem(Function {
decl: (&self.decl, self.body).clean(cx),
generics: self.generics.clean(cx),
decl,
generics,
unsafety: self.unsafety,
constness: self.constness,
abi: self.abi,
Expand Down Expand Up @@ -1885,7 +1901,8 @@ impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
vec![].into_iter()
} else {
cx.tcx.fn_arg_names(did).into_iter()
}.peekable();
};

FnDecl {
output: Return(sig.skip_binder().output().clean(cx)),
attrs: Attributes::default(),
Expand Down Expand Up @@ -2027,10 +2044,13 @@ impl Clean<Item> for hir::TraitItem {
MethodItem((sig, &self.generics, body).clean(cx))
}
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref names)) => {
let generics = self.generics.clean(cx);
TyMethodItem(TyMethod {
unsafety: sig.unsafety.clone(),
decl: (&*sig.decl, &names[..]).clean(cx),
generics: self.generics.clean(cx),
decl: enter_impl_trait(cx, &generics.params, || {
(&*sig.decl, &names[..]).clean(cx)
}),
generics,
abi: sig.abi
})
}
Expand Down Expand Up @@ -2534,6 +2554,12 @@ impl Clean<Type> for hir::Ty {
return new_ty;
}

if let Def::TyParam(did) = path.def {
if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did) {
return ImplTrait(bounds);
}
}

let mut alias = None;
if let Def::TyAlias(def_id) = path.def {
// Substitute private type aliases
Expand Down Expand Up @@ -3246,10 +3272,13 @@ pub struct BareFunctionDecl {

impl Clean<BareFunctionDecl> for hir::BareFnTy {
fn clean(&self, cx: &DocContext) -> BareFunctionDecl {
let generic_params = self.generic_params.clean(cx);
BareFunctionDecl {
unsafety: self.unsafety,
generic_params: self.generic_params.clean(cx),
decl: (&*self.decl, &self.arg_names[..]).clean(cx),
decl: enter_impl_trait(cx, &generic_params, || {
(&*self.decl, &self.arg_names[..]).clean(cx)
}),
generic_params,
abi: self.abi,
}
}
Expand Down Expand Up @@ -3550,9 +3579,12 @@ impl Clean<Item> for hir::ForeignItem {
fn clean(&self, cx: &DocContext) -> Item {
let inner = match self.node {
hir::ForeignItemFn(ref decl, ref names, ref generics) => {
let generics = generics.clean(cx);
ForeignFunctionItem(Function {
decl: (&**decl, &names[..]).clean(cx),
generics: generics.clean(cx),
decl: enter_impl_trait(cx, &generics.params, || {
(&**decl, &names[..]).clean(cx)
}),
generics,
unsafety: hir::Unsafety::Unsafe,
abi: Abi::Rust,
constness: hir::Constness::NotConst,
Expand Down Expand Up @@ -3854,6 +3886,29 @@ pub fn def_id_to_path(cx: &DocContext, did: DefId, name: Option<String>) -> Vec<
once(crate_name).chain(relative).collect()
}

pub fn enter_impl_trait<F, R>(cx: &DocContext, gps: &[GenericParam], f: F) -> R
where
F: FnOnce() -> R,
{
let bounds = gps.iter()
.filter_map(|p| {
if let GenericParam::Type(ref tp) = *p {
if tp.synthetic == Some(hir::SyntheticTyParamKind::ImplTrait) {
return Some((tp.did, tp.bounds.clone()));
}
}

None
})
.collect::<FxHashMap<DefId, Vec<TyParamBound>>>();

let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), bounds);
let r = f();
assert!(cx.impl_trait_bounds.borrow().is_empty());
*cx.impl_trait_bounds.borrow_mut() = old_bounds;
r
}

// Start of code copied from rust-clippy

pub fn get_trait_def_id(tcx: &TyCtxt, path: &[&str], use_local: bool) -> Option<DefId> {
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
/// Table node id of lifetime parameter definition -> substituted lifetime
pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
/// Table DefId of `impl Trait` in argument position -> bounds
pub impl_trait_bounds: RefCell<FxHashMap<DefId, Vec<clean::TyParamBound>>>,
pub send_trait: Option<DefId>,
pub fake_def_ids: RefCell<FxHashMap<CrateNum, DefId>>,
pub all_fake_def_ids: RefCell<FxHashSet<DefId>>,
Expand Down Expand Up @@ -261,6 +263,7 @@ pub fn run_core(search_paths: SearchPaths,
renderinfo: Default::default(),
ty_substs: Default::default(),
lt_substs: Default::default(),
impl_trait_bounds: Default::default(),
mod_ids: Default::default(),
send_trait: send_trait,
fake_def_ids: RefCell::new(FxHashMap()),
Expand Down
27 changes: 11 additions & 16 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ impl fmt::Display for clean::GenericParam {

impl fmt::Display for clean::Generics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.params.is_empty() { return Ok(()) }
let real_params = self.params
.iter()
.filter(|p| !p.is_synthetic_type_param())
.collect::<Vec<_>>();
if real_params.is_empty() {
return Ok(());
}
if f.alternate() {
write!(f, "<{:#}>", CommaSep(&self.params))
write!(f, "<{:#}>", CommaSep(&real_params))
} else {
write!(f, "&lt;{}&gt;", CommaSep(&self.params))
write!(f, "&lt;{}&gt;", CommaSep(&real_params))
}
}
}
Expand Down Expand Up @@ -575,7 +581,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
}
many => {
primitive_link(f, PrimitiveType::Tuple, "(")?;
fmt::Display::fmt(&CommaSep(&many), f)?;
fmt::Display::fmt(&CommaSep(many), f)?;
primitive_link(f, PrimitiveType::Tuple, ")")
}
}
Expand Down Expand Up @@ -661,18 +667,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
}
}
clean::ImplTrait(ref bounds) => {
write!(f, "impl ")?;
for (i, bound) in bounds.iter().enumerate() {
if i != 0 {
write!(f, " + ")?;
}
if f.alternate() {
write!(f, "{:#}", *bound)?;
} else {
write!(f, "{}", *bound)?;
}
}
Ok(())
write!(f, "impl {}", TyParamBounds(bounds))
}
clean::QPath { ref name, ref self_type, ref trait_ } => {
let should_show_cast = match *trait_ {
Expand Down
46 changes: 46 additions & 0 deletions src/test/rustdoc/universal-impl-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(universal_impl_trait)]
#![crate_name = "foo"]

// @has foo/fn.foo.html
// @has - //pre 'foo('
// @matches - '_x: impl <a class="trait" href="[^"]+/trait\.Clone\.html"'
// @matches - '_z: .+impl.+trait\.Copy\.html.+, impl.+trait\.Clone\.html'
pub fn foo(_x: impl Clone, _y: i32, _z: (impl Copy, impl Clone)) {
}

pub trait Trait {
// @has foo/trait.Trait.html
// @has - 'method</a>('
// @matches - '_x: impl <a class="trait" href="[^"]+/trait\.Debug\.html"'
fn method(&self, _x: impl std::fmt::Debug) {
}
}

pub struct S<T>(T);

impl<T> S<T> {
// @has foo/struct.S.html
// @has - 'bar</a>('
// @matches - '_bar: impl <a class="trait" href="[^"]+/trait\.Copy\.html"'
pub fn bar(_bar: impl Copy) {
}

// @has - 'baz</a>('
// @matches - '_baz:.+struct\.S\.html.+impl .+trait\.Clone\.html'
pub fn baz(_baz: S<impl Clone>) {
}
}

// @has - 'method</a>('
// @matches - '_x: impl <a class="trait" href="[^"]+/trait\.Debug\.html"'
impl<T> Trait for S<T> {}