Skip to content

rustdoc: Render <Self as X>::Y type casts properly across crate bounds #86449

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 1 commit into from
Jun 26, 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
21 changes: 18 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
use rustc_middle::middle::resolve_lifetime as rl;
use rustc_middle::ty::fold::TypeFolder;
use rustc_middle::ty::subst::{InternalSubsts, Subst};
use rustc_middle::ty::{self, AdtKind, Lift, Ty, TyCtxt};
use rustc_middle::ty::{self, AdtKind, DefIdTree, Lift, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_mir::const_eval::{is_const_fn, is_unstable_const_fn};
use rustc_span::hygiene::{AstPass, MacroKind};
Expand Down Expand Up @@ -435,8 +435,23 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
let (name, kind) = match self.kind {
ty::GenericParamDefKind::Lifetime => (self.name, GenericParamDefKind::Lifetime),
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
let default =
if has_default { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None };
let default = if has_default {
let mut default = cx.tcx.type_of(self.def_id).clean(cx);

// We need to reassign the `self_def_id`, if there's a parent (which is the
// `Self` type), so we can properly render `<Self as X>` casts, because the
// information about which type `Self` is, is only present here, but not in
// the cleaning process of the type itself. To resolve this and have the
// `self_def_id` set, we override it here.
// See https://github.com/rust-lang/rust/issues/85454
if let QPath { ref mut self_def_id, .. } = default {
*self_def_id = cx.tcx.parent(self.def_id);
}

Some(default)
} else {
None
};
(
self.name,
GenericParamDefKind::Type {
Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc/auxiliary/issue-85454.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @has issue_85454/trait.FromResidual.html
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
pub trait FromResidual<R = <Self as Try>::Residual> {
fn from_residual(residual: R) -> Self;
}

pub trait Try: FromResidual {
type Output;
type Residual;
fn from_output(output: Self::Output) -> Self;
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
}

pub enum ControlFlow<B, C = ()> {
Continue(C),
Break(B),
}
14 changes: 13 additions & 1 deletion src/test/rustdoc/issue-85454.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// @has issue_85454/trait.FromResidual.html
// aux-build:issue-85454.rs
// build-aux-docs
#![crate_name = "foo"]

extern crate issue_85454;

// @has foo/trait.FromResidual.html
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
pub trait FromResidual<R = <Self as Try>::Residual> {
fn from_residual(residual: R) -> Self;
Expand All @@ -15,3 +21,9 @@ pub enum ControlFlow<B, C = ()> {
Continue(C),
Break(B),
}

pub mod reexport {
// @has foo/reexport/trait.FromResidual.html
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
pub use issue_85454::*;
}