diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 75ee663b926c4..6f4b87750ff85 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -101,27 +101,6 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { cx.generated_synthetics.insert((ty, trait_def_id)); - let hir_imp = impl_def_id.as_local() - .map(|local| cx.tcx.hir().expect_item(local)) - .and_then(|item| if let hir::ItemKind::Impl(i) = &item.kind { - Some(i) - } else { - None - }); - - let items = match hir_imp { - Some(imp) => imp - .items - .iter() - .map(|ii| cx.tcx.hir().impl_item(ii.id).clean(cx)) - .collect::>(), - None => cx.tcx - .associated_items(impl_def_id) - .in_definition_order() - .map(|x| x.clean(cx)) - .collect::>(), - }; - impls.push(Item { name: None, attrs: Default::default(), @@ -138,7 +117,11 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { // the post-inference `trait_ref`, as it's more accurate. trait_: Some(trait_ref.clean(cx)), for_: ty.clean(cx), - items, + items: cx.tcx + .associated_items(impl_def_id) + .in_definition_order() + .map(|x| x.clean(cx)) + .collect::>(), polarity: ty::ImplPolarity::Positive, kind: ImplKind::Blanket(box trait_ref.self_ty().clean(cx)), }), diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index a2e612955b349..4fe433188c90c 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -228,7 +228,7 @@ fn build_external_function(cx: &mut DocContext<'_>, did: DefId) -> clean::Functi let (generics, decl) = clean::enter_impl_trait(cx, |cx| { // NOTE: generics need to be cleaned before the decl! let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates); - let decl = clean_fn_decl_from_did_and_sig(cx, did, sig); + let decl = clean_fn_decl_from_did_and_sig(cx, Some(did), sig); (generics, decl) }); clean::Function { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 33612c8065477..a7eced296106f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -890,13 +890,20 @@ fn clean_fn_decl_with_args( fn clean_fn_decl_from_did_and_sig( cx: &mut DocContext<'_>, - did: DefId, + did: Option, sig: ty::PolyFnSig<'_>, ) -> FnDecl { - let mut names = if did.is_local() { &[] } else { cx.tcx.fn_arg_names(did) }.iter(); + let mut names = did.map_or(&[] as &[_], |did| cx.tcx.fn_arg_names(did)).iter(); + + // We assume all empty tuples are default return type. This theoretically can discard `-> ()`, + // but shouldn't change any code meaning. + let output = match sig.skip_binder().output().clean(cx) { + Type::Tuple(inner) if inner.len() == 0 => DefaultReturn, + ty => Return(ty), + }; FnDecl { - output: Return(sig.skip_binder().output().clean(cx)), + output, c_variadic: sig.skip_binder().c_variadic, inputs: Arguments { values: sig @@ -1029,20 +1036,18 @@ impl Clean for hir::ImplItem<'_> { } }; - let what_rustc_thinks = + let mut what_rustc_thinks = Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx); - let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id())); - if let hir::ItemKind::Impl(impl_) = &parent_item.kind { - if impl_.of_trait.is_some() { - // Trait impl items always inherit the impl's visibility -- - // we don't want to show `pub`. - Item { visibility: Inherited, ..what_rustc_thinks } - } else { - what_rustc_thinks - } - } else { - panic!("found impl item with non-impl parent {:?}", parent_item); + + let impl_ref = cx.tcx.parent(local_did).and_then(|did| cx.tcx.impl_trait_ref(did)); + + // Trait impl items always inherit the impl's visibility -- + // we don't want to show `pub`. + if impl_ref.is_some() { + what_rustc_thinks.visibility = Inherited; } + + what_rustc_thinks }) } } @@ -1067,7 +1072,7 @@ impl Clean for ty::AssocItem { tcx.explicit_predicates_of(self.def_id), ); let sig = tcx.fn_sig(self.def_id); - let mut decl = clean_fn_decl_from_did_and_sig(cx, self.def_id, sig); + let mut decl = clean_fn_decl_from_did_and_sig(cx, Some(self.def_id), sig); if self.fn_has_self_parameter { let self_ty = match self.container { @@ -1197,7 +1202,18 @@ impl Clean for ty::AssocItem { } }; - Item::from_def_id_and_parts(self.def_id, Some(self.name), kind, cx) + let mut what_rustc_thinks = + Item::from_def_id_and_parts(self.def_id, Some(self.name), kind, cx); + + let impl_ref = tcx.parent(self.def_id).and_then(|did| tcx.impl_trait_ref(did)); + + // Trait impl items always inherit the impl's visibility -- + // we don't want to show `pub`. + if impl_ref.is_some() { + what_rustc_thinks.visibility = Visibility::Inherited; + } + + what_rustc_thinks } } @@ -1466,8 +1482,7 @@ impl<'tcx> Clean for Ty<'tcx> { ty::FnDef(..) | ty::FnPtr(_) => { let ty = cx.tcx.lift(*self).expect("FnPtr lift failed"); let sig = ty.fn_sig(cx.tcx); - let def_id = DefId::local(CRATE_DEF_INDEX); - let decl = clean_fn_decl_from_did_and_sig(cx, def_id, sig); + let decl = clean_fn_decl_from_did_and_sig(cx, None, sig); BareFunction(box BareFunctionDecl { unsafety: sig.unsafety(), generic_params: Vec::new(),