Rollup merge of #93385 - CraftSpider:rustdoc-ty-fixes, r=camelid

Rustdoc ty consistency fixes

Changes to make rustdoc cleaning of ty more consistent with hir, and hopefully use it in more places.

r? `@camelid`
This commit is contained in:
Dylan DPC 2022-03-01 03:41:47 +01:00 committed by GitHub
commit 2fb5a16438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 42 deletions

View File

@ -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::<Vec<_>>(),
None => cx.tcx
.associated_items(impl_def_id)
.in_definition_order()
.map(|x| x.clean(cx))
.collect::<Vec<_>>(),
};
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::<Vec<_>>(),
polarity: ty::ImplPolarity::Positive,
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(cx)),
}),

View File

@ -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 {

View File

@ -891,13 +891,20 @@ fn clean_fn_decl_with_args(
fn clean_fn_decl_from_did_and_sig(
cx: &mut DocContext<'_>,
did: DefId,
did: Option<DefId>,
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
@ -1031,20 +1038,18 @@ impl Clean<Item> 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
})
}
}
@ -1069,7 +1074,7 @@ impl Clean<Item> 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 {
@ -1199,7 +1204,18 @@ impl Clean<Item> 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
}
}
@ -1478,8 +1494,7 @@ impl<'tcx> Clean<Type> 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(),