Skip to content

[wip] rustdoc: rework of projection predicate to type binding resugaring #9

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

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 19 additions & 16 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,23 +2259,26 @@ fn clean_middle_opaque_bounds<'tcx>(
let bindings: ThinVec<_> = bounds
.iter()
.filter_map(|bound| {
if let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder() {
if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() {
Some(TypeBinding {
assoc: projection_to_path_segment(
bound.kind().rebind(proj.projection_ty),
cx,
),
kind: TypeBindingKind::Equality {
term: clean_middle_term(bound.kind().rebind(proj.term), cx),
},
})
} else {
None
}
} else {
None
let ty::ClauseKind::Projection(proj) = bound.kind().skip_binder() else {
return None;
};
if !simplify::trait_is_same_or_supertrait2(
cx,
trait_ref.skip_binder(),
proj.projection_ty.trait_ref(cx.tcx),
) {
return None;
}

Some(TypeBinding {
assoc: projection_to_path_segment(
bound.kind().rebind(proj.projection_ty),
cx,
),
kind: TypeBindingKind::Equality {
term: clean_middle_term(bound.kind().rebind(proj.term), cx),
},
})
})
.collect();

Expand Down
40 changes: 36 additions & 4 deletions src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> ThinVec<WP
clauses
}

// @Note this basically does the same as clean_middle_opaque_bounds re bound clean
pub(crate) fn merge_bounds(
cx: &clean::DocContext<'_>,
bounds: &mut Vec<clean::GenericBound>,
Expand All @@ -81,9 +82,8 @@ pub(crate) fn merge_bounds(
rhs: &clean::Term,
) -> bool {
!bounds.iter_mut().any(|b| {
let trait_ref = match *b {
clean::GenericBound::TraitBound(ref mut tr, _) => tr,
clean::GenericBound::Outlives(..) => return false,
let clean::GenericBound::TraitBound(trait_ref, _) = b else {
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
Expand Down Expand Up @@ -119,7 +119,12 @@ pub(crate) fn merge_bounds(
})
}

fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
// FIXME(fmease): @Task get rid of this!
pub(crate) fn trait_is_same_or_supertrait(
cx: &DocContext<'_>,
child: DefId,
trait_: DefId,
) -> bool {
if child == trait_ {
return true;
}
Expand All @@ -139,6 +144,33 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId)
.any(|did| trait_is_same_or_supertrait(cx, did, trait_))
}

// @Task docs
pub(crate) fn trait_is_same_or_supertrait2<'tcx>(
cx: &DocContext<'tcx>,
child: ty::TraitRef<'tcx>,
trait_: ty::TraitRef<'tcx>,
) -> bool {
if child == trait_ {
return true;
}
let predicates = cx.tcx.super_predicates_of(child.def_id);
debug_assert!(cx.tcx.generics_of(child.def_id).has_self);
predicates
.predicates
.iter()
.filter_map(|(pred, _)| {
let ty::ClauseKind::Trait(pred) = pred.kind().skip_binder() else {
return None;
};
if pred.trait_ref.self_ty() != cx.tcx.types.self_param {
return None;
}

Some(ty::EarlyBinder::bind(pred.trait_ref).instantiate(cx.tcx, child.args))
})
.any(|child| trait_is_same_or_supertrait2(cx, child, trait_))
}

/// Move bounds that are (likely) directly attached to generic parameters from the where-clause to
/// the respective parameter.
///
Expand Down