Skip to content

rustdoc: Cleanup various clean types #88379

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

Merged
merged 16 commits into from
Oct 9, 2021
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
322 changes: 143 additions & 179 deletions src/librustdoc/clean/auto_trait.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.clean(self.cx),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
trait_: Some(trait_ref.clean(self.cx)),
for_: ty.clean(self.cx),
items: self
.cx
Expand Down
52 changes: 25 additions & 27 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,26 @@ crate fn build_impl(
),
};
let polarity = tcx.impl_polarity(did);
let trait_ = associated_trait.clean(cx).map(|bound| match bound {
clean::GenericBound::TraitBound(polyt, _) => polyt.trait_,
clean::GenericBound::Outlives(..) => unreachable!(),
});
if trait_.def_id() == tcx.lang_items().deref_trait() {
let trait_ = associated_trait.clean(cx);
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like a very helpful change? I would rather keep GetDefId than add more churn like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetDefId::def_id returns an Option, though, but we can always get the DefId of a Path. It adds unnecessary confusion about what information is available. I also find it very confusing that GetDefId is implement for Option<T: GetDefId>. This code is much clearer IMO.

super::build_deref_target_impls(cx, &trait_items, ret);
}

// Return if the trait itself or any types of the generic parameters are doc(hidden).
let mut stack: Vec<&Type> = trait_.iter().collect();
stack.push(&for_);
let mut stack: Vec<&Type> = vec![&for_];

if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
return;
}
}
if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
stack.extend(generics);
}

while let Some(ty) = stack.pop() {
if let Some(did) = ty.def_id() {
if cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
return;
}
}
Expand All @@ -468,14 +474,14 @@ crate fn build_impl(
}
}

if let Some(trait_did) = trait_.def_id() {
record_extern_trait(cx, trait_did);
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
record_extern_trait(cx, did);
}

let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
trace!("merged_attrs={:?}", merged_attrs);

trace!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id());
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
did,
None,
Expand Down Expand Up @@ -526,7 +532,6 @@ fn build_module(
item.ident.name,
clean::ImportSource {
path: clean::Path {
global: false,
res,
segments: vec![clean::PathSegment {
name: prim_ty.as_sym(),
Expand Down Expand Up @@ -621,30 +626,23 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
ref mut bounds,
..
} if *s == kw::SelfUpper => {
bounds.retain(|bound| match *bound {
clean::GenericBound::TraitBound(
clean::PolyTrait { trait_: clean::ResolvedPath { did, .. }, .. },
_,
) => did != trait_did,
bounds.retain(|bound| match bound {
clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
trait_.def_id() != trait_did
}
_ => true,
});
}
_ => {}
}
}

g.where_predicates.retain(|pred| match *pred {
g.where_predicates.retain(|pred| match pred {
clean::WherePredicate::BoundPredicate {
ty:
clean::QPath {
self_type: box clean::Generic(ref s),
trait_: box clean::ResolvedPath { did, .. },
name: ref _name,
..
},
ref bounds,
ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, name: _, .. },
bounds,
..
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
_ => true,
});
g
Expand Down
68 changes: 26 additions & 42 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
}
}

impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
impl Clean<Path> for (ty::TraitRef<'_>, &[TypeBinding]) {
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
let (trait_ref, bounds) = *self;
let kind = cx.tcx.def_kind(trait_ref.def_id).into();
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
Expand All @@ -168,16 +168,13 @@ impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {

debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);

ResolvedPath { path, did: trait_ref.def_id }
path
}
}

impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound {
GenericBound::TraitBound(
PolyTrait { trait_: (*self, &[][..]).clean(cx), generic_params: vec![] },
hir::TraitBoundModifier::None,
)
impl Clean<Path> for ty::TraitRef<'tcx> {
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
(*self, &[][..]).clean(cx)
}
}

Expand Down Expand Up @@ -384,16 +381,13 @@ impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
let lifted = self.lift_to_tcx(cx.tcx).unwrap();
let trait_ = match lifted.trait_ref(cx.tcx).clean(cx) {
GenericBound::TraitBound(t, _) => t.trait_,
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
};
let trait_ = lifted.trait_ref(cx.tcx).clean(cx);
let self_type = self.self_ty().clean(cx);
Type::QPath {
name: cx.tcx.associated_item(self.item_def_id).ident.name,
self_def_id: self_type.def_id(),
self_type: box self_type,
trait_: box trait_,
trait_,
}
}
}
Expand Down Expand Up @@ -896,10 +890,11 @@ impl Clean<bool> for hir::IsAuto {
}
}

impl Clean<Type> for hir::TraitRef<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
impl Clean<Path> for hir::TraitRef<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
let path = self.path.clean(cx);
resolve_type(cx, path)
register_res(cx, path.res);
path
}
}

Expand Down Expand Up @@ -1105,9 +1100,8 @@ impl Clean<Item> for ty::AssocItem {
if *name != my_name {
return None;
}
match **trait_ {
ResolvedPath { did, .. } if did == self.container.id() => {}
_ => return None,
if trait_.def_id() != self.container.id() {
return None;
}
match **self_type {
Generic(ref s) if *s == kw::SelfUpper => {}
Expand Down Expand Up @@ -1273,19 +1267,18 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
return normalized_value.clean(cx);
}

let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
let trait_segments = &segments[..segments.len() - 1];
let trait_segments = &p.segments[..p.segments.len() - 1];
let trait_def = cx.tcx.associated_item(p.res.def_id()).container.id();
let trait_path = self::Path {
global: p.is_global(),
let trait_ = self::Path {
res: Res::Def(DefKind::Trait, trait_def),
segments: trait_segments.clean(cx),
};
register_res(cx, trait_.res);
Type::QPath {
name: p.segments.last().expect("segments were empty").ident.name,
self_def_id: Some(DefId::local(qself.hir_id.owner.local_def_index)),
self_type: box qself.clean(cx),
trait_: box resolve_type(cx, trait_path),
trait_,
}
}
hir::QPath::TypeRelative(ref qself, ref segment) => {
Expand All @@ -1296,12 +1289,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
ty::Error(_) => return Type::Infer,
_ => bug!("clean: expected associated type, found `{:?}`", ty),
};
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
let trait_ = hir::Path { span, res, segments: &[] }.clean(cx);
register_res(cx, trait_.res);
Type::QPath {
name: segment.ident.name,
self_def_id: res.opt_def_id(),
self_type: box qself.clean(cx),
trait_: box resolve_type(cx, trait_path),
trait_,
}
}
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
Expand Down Expand Up @@ -1470,10 +1464,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, did, false, vec![], empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
let bound = PolyTrait {
trait_: ResolvedPath { path, did },
generic_params: Vec::new(),
};
let bound = PolyTrait { trait_: path, generic_params: Vec::new() };
bounds.push(bound);
}

Expand All @@ -1486,10 +1477,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
}

let path = external_path(cx, did, false, bindings, substs);
bounds.insert(
0,
PolyTrait { trait_: ResolvedPath { path, did }, generic_params: Vec::new() },
);
bounds.insert(0, PolyTrait { trait_: path, generic_params: Vec::new() });

DynTrait(bounds, lifetime)
}
Expand Down Expand Up @@ -1728,11 +1716,7 @@ impl Clean<Variant> for hir::VariantData<'_> {

impl Clean<Path> for hir::Path<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
Path {
global: self.is_global(),
res: self.res,
segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
}
Path { res: self.res, segments: self.segments.clean(cx) }
}
}

Expand Down Expand Up @@ -1898,7 +1882,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>

// If this impl block is an implementation of the Deref trait, then we
// need to try inlining the target's inherent impl blocks as well.
if trait_.def_id() == tcx.lang_items().deref_trait() {
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
build_deref_target_impls(cx, &items, &mut ret);
}

Expand All @@ -1907,7 +1891,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
_ => None,
});
let mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
let kind = ImplItem(Impl {
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
unsafety: impl_.unsafety,
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ crate fn merge_bounds(
clean::GenericBound::TraitBound(ref mut tr, _) => tr,
clean::GenericBound::Outlives(..) => return false,
};
let (did, path) = match trait_ref.trait_ {
clean::ResolvedPath { did, ref mut path, .. } => (did, path),
_ => return false,
};
// If this QPath's trait `trait_did` is the same as, or a supertrait
// of, the bound's trait `did` then we can keep going, otherwise
// this is just a plain old equality bound.
if !trait_is_same_or_supertrait(cx, did, trait_did) {
if !trait_is_same_or_supertrait(cx, trait_ref.trait_.def_id(), trait_did) {
return false;
}
let last = path.segments.last_mut().expect("segments were empty");
let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
match last.args {
PP::AngleBracketed { ref mut bindings, .. } => {
bindings.push(clean::TypeBinding {
Expand Down
Loading