Skip to content

Commit 2fb5a16

Browse files
authored
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`
2 parents 4bd40d6 + 2d2163b commit 2fb5a16

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

src/librustdoc/clean/blanket_impl.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,6 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
101101

102102
cx.generated_synthetics.insert((ty, trait_def_id));
103103

104-
let hir_imp = impl_def_id.as_local()
105-
.map(|local| cx.tcx.hir().expect_item(local))
106-
.and_then(|item| if let hir::ItemKind::Impl(i) = &item.kind {
107-
Some(i)
108-
} else {
109-
None
110-
});
111-
112-
let items = match hir_imp {
113-
Some(imp) => imp
114-
.items
115-
.iter()
116-
.map(|ii| cx.tcx.hir().impl_item(ii.id).clean(cx))
117-
.collect::<Vec<_>>(),
118-
None => cx.tcx
119-
.associated_items(impl_def_id)
120-
.in_definition_order()
121-
.map(|x| x.clean(cx))
122-
.collect::<Vec<_>>(),
123-
};
124-
125104
impls.push(Item {
126105
name: None,
127106
attrs: Default::default(),
@@ -138,7 +117,11 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
138117
// the post-inference `trait_ref`, as it's more accurate.
139118
trait_: Some(trait_ref.clean(cx)),
140119
for_: ty.clean(cx),
141-
items,
120+
items: cx.tcx
121+
.associated_items(impl_def_id)
122+
.in_definition_order()
123+
.map(|x| x.clean(cx))
124+
.collect::<Vec<_>>(),
142125
polarity: ty::ImplPolarity::Positive,
143126
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(cx)),
144127
}),

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn build_external_function(cx: &mut DocContext<'_>, did: DefId) -> clean::Functi
228228
let (generics, decl) = clean::enter_impl_trait(cx, |cx| {
229229
// NOTE: generics need to be cleaned before the decl!
230230
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
231-
let decl = clean_fn_decl_from_did_and_sig(cx, did, sig);
231+
let decl = clean_fn_decl_from_did_and_sig(cx, Some(did), sig);
232232
(generics, decl)
233233
});
234234
clean::Function {

src/librustdoc/clean/mod.rs

+34-19
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,20 @@ fn clean_fn_decl_with_args(
891891

892892
fn clean_fn_decl_from_did_and_sig(
893893
cx: &mut DocContext<'_>,
894-
did: DefId,
894+
did: Option<DefId>,
895895
sig: ty::PolyFnSig<'_>,
896896
) -> FnDecl {
897-
let mut names = if did.is_local() { &[] } else { cx.tcx.fn_arg_names(did) }.iter();
897+
let mut names = did.map_or(&[] as &[_], |did| cx.tcx.fn_arg_names(did)).iter();
898+
899+
// We assume all empty tuples are default return type. This theoretically can discard `-> ()`,
900+
// but shouldn't change any code meaning.
901+
let output = match sig.skip_binder().output().clean(cx) {
902+
Type::Tuple(inner) if inner.len() == 0 => DefaultReturn,
903+
ty => Return(ty),
904+
};
898905

899906
FnDecl {
900-
output: Return(sig.skip_binder().output().clean(cx)),
907+
output,
901908
c_variadic: sig.skip_binder().c_variadic,
902909
inputs: Arguments {
903910
values: sig
@@ -1031,20 +1038,18 @@ impl Clean<Item> for hir::ImplItem<'_> {
10311038
}
10321039
};
10331040

1034-
let what_rustc_thinks =
1041+
let mut what_rustc_thinks =
10351042
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
1036-
let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id()));
1037-
if let hir::ItemKind::Impl(impl_) = &parent_item.kind {
1038-
if impl_.of_trait.is_some() {
1039-
// Trait impl items always inherit the impl's visibility --
1040-
// we don't want to show `pub`.
1041-
Item { visibility: Inherited, ..what_rustc_thinks }
1042-
} else {
1043-
what_rustc_thinks
1044-
}
1045-
} else {
1046-
panic!("found impl item with non-impl parent {:?}", parent_item);
1043+
1044+
let impl_ref = cx.tcx.parent(local_did).and_then(|did| cx.tcx.impl_trait_ref(did));
1045+
1046+
// Trait impl items always inherit the impl's visibility --
1047+
// we don't want to show `pub`.
1048+
if impl_ref.is_some() {
1049+
what_rustc_thinks.visibility = Inherited;
10471050
}
1051+
1052+
what_rustc_thinks
10481053
})
10491054
}
10501055
}
@@ -1069,7 +1074,7 @@ impl Clean<Item> for ty::AssocItem {
10691074
tcx.explicit_predicates_of(self.def_id),
10701075
);
10711076
let sig = tcx.fn_sig(self.def_id);
1072-
let mut decl = clean_fn_decl_from_did_and_sig(cx, self.def_id, sig);
1077+
let mut decl = clean_fn_decl_from_did_and_sig(cx, Some(self.def_id), sig);
10731078

10741079
if self.fn_has_self_parameter {
10751080
let self_ty = match self.container {
@@ -1199,7 +1204,18 @@ impl Clean<Item> for ty::AssocItem {
11991204
}
12001205
};
12011206

1202-
Item::from_def_id_and_parts(self.def_id, Some(self.name), kind, cx)
1207+
let mut what_rustc_thinks =
1208+
Item::from_def_id_and_parts(self.def_id, Some(self.name), kind, cx);
1209+
1210+
let impl_ref = tcx.parent(self.def_id).and_then(|did| tcx.impl_trait_ref(did));
1211+
1212+
// Trait impl items always inherit the impl's visibility --
1213+
// we don't want to show `pub`.
1214+
if impl_ref.is_some() {
1215+
what_rustc_thinks.visibility = Visibility::Inherited;
1216+
}
1217+
1218+
what_rustc_thinks
12031219
}
12041220
}
12051221

@@ -1478,8 +1494,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14781494
ty::FnDef(..) | ty::FnPtr(_) => {
14791495
let ty = cx.tcx.lift(*self).expect("FnPtr lift failed");
14801496
let sig = ty.fn_sig(cx.tcx);
1481-
let def_id = DefId::local(CRATE_DEF_INDEX);
1482-
let decl = clean_fn_decl_from_did_and_sig(cx, def_id, sig);
1497+
let decl = clean_fn_decl_from_did_and_sig(cx, None, sig);
14831498
BareFunction(box BareFunctionDecl {
14841499
unsafety: sig.unsafety(),
14851500
generic_params: Vec::new(),

0 commit comments

Comments
 (0)