diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 1a42b02140cd2..3985229669d5d 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1237,6 +1237,7 @@ pub struct TyParam { pub did: DefId, pub bounds: Vec, pub default: Option, + pub synthetic: Option, } impl Clean for hir::TyParam { @@ -1246,6 +1247,7 @@ impl Clean 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, } } } @@ -1261,7 +1263,8 @@ impl<'tcx> Clean for ty::TypeParameterDef { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None - } + }, + synthetic: None, } } } @@ -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 for hir::GenericParam { fn clean(&self, cx: &DocContext) -> GenericParam { match *self { @@ -1761,11 +1774,12 @@ pub struct Method { impl<'a> Clean 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 } } @@ -1790,6 +1804,8 @@ pub struct Function { impl Clean 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), @@ -1799,8 +1815,8 @@ impl Clean 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, @@ -1885,7 +1901,8 @@ impl<'a, 'tcx> Clean 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(), @@ -2027,10 +2044,13 @@ impl Clean 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 }) } @@ -2534,6 +2554,12 @@ impl Clean 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 @@ -3246,10 +3272,13 @@ pub struct BareFunctionDecl { impl Clean 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, } } @@ -3550,9 +3579,12 @@ impl Clean 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, @@ -3854,6 +3886,29 @@ pub fn def_id_to_path(cx: &DocContext, did: DefId, name: Option) -> Vec< once(crate_name).chain(relative).collect() } +pub fn enter_impl_trait(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::>>(); + + 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 { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 1e0fafc8d9dfd..749d7ec126be3 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -72,6 +72,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> { pub ty_substs: RefCell>, /// Table node id of lifetime parameter definition -> substituted lifetime pub lt_substs: RefCell>, + /// Table DefId of `impl Trait` in argument position -> bounds + pub impl_trait_bounds: RefCell>>, pub send_trait: Option, pub fake_def_ids: RefCell>, pub all_fake_def_ids: RefCell>, @@ -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()), diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 2913ea6a78ec3..0a7e19fc643f6 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -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::>(); + if real_params.is_empty() { + return Ok(()); + } if f.alternate() { - write!(f, "<{:#}>", CommaSep(&self.params)) + write!(f, "<{:#}>", CommaSep(&real_params)) } else { - write!(f, "<{}>", CommaSep(&self.params)) + write!(f, "<{}>", CommaSep(&real_params)) } } } @@ -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, ")") } } @@ -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_ { diff --git a/src/test/rustdoc/universal-impl-trait.rs b/src/test/rustdoc/universal-impl-trait.rs new file mode 100644 index 0000000000000..4cf99562c5299 --- /dev/null +++ b/src/test/rustdoc/universal-impl-trait.rs @@ -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 or the MIT license +// , 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 (' + // @matches - '_x: impl (T); + +impl S { + // @has foo/struct.S.html + // @has - 'bar(' + // @matches - '_bar: impl (' + // @matches - '_baz:.+struct\.S\.html.+impl .+trait\.Clone\.html' + pub fn baz(_baz: S) { + } +} + +// @has - 'method(' +// @matches - '_x: impl Trait for S {}