Skip to content

Commit 3ddb78a

Browse files
committed
Auto merge of rust-lang#86449 - Stupremee:render-self-cast-in-type-bound, r=GuillaumeGomez
rustdoc: Render `<Self as X>::Y` type casts properly across crate bounds My last PR that introduced the type casting did not work for cross-crate re-exported traits, which is fixed in this PR. Fully resolves rust-lang#85454
2 parents f2571a2 + f14bdb5 commit 3ddb78a

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/librustdoc/clean/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
2020
use rustc_middle::middle::resolve_lifetime as rl;
2121
use rustc_middle::ty::fold::TypeFolder;
2222
use rustc_middle::ty::subst::{InternalSubsts, Subst};
23-
use rustc_middle::ty::{self, AdtKind, Lift, Ty, TyCtxt};
23+
use rustc_middle::ty::{self, AdtKind, DefIdTree, Lift, Ty, TyCtxt};
2424
use rustc_middle::{bug, span_bug};
2525
use rustc_mir::const_eval::{is_const_fn, is_unstable_const_fn};
2626
use rustc_span::hygiene::{AstPass, MacroKind};
@@ -438,8 +438,23 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
438438
let (name, kind) = match self.kind {
439439
ty::GenericParamDefKind::Lifetime => (self.name, GenericParamDefKind::Lifetime),
440440
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
441-
let default =
442-
if has_default { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None };
441+
let default = if has_default {
442+
let mut default = cx.tcx.type_of(self.def_id).clean(cx);
443+
444+
// We need to reassign the `self_def_id`, if there's a parent (which is the
445+
// `Self` type), so we can properly render `<Self as X>` casts, because the
446+
// information about which type `Self` is, is only present here, but not in
447+
// the cleaning process of the type itself. To resolve this and have the
448+
// `self_def_id` set, we override it here.
449+
// See https://github.com/rust-lang/rust/issues/85454
450+
if let QPath { ref mut self_def_id, .. } = default {
451+
*self_def_id = cx.tcx.parent(self.def_id);
452+
}
453+
454+
Some(default)
455+
} else {
456+
None
457+
};
443458
(
444459
self.name,
445460
GenericParamDefKind::Type {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @has issue_85454/trait.FromResidual.html
2+
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
3+
pub trait FromResidual<R = <Self as Try>::Residual> {
4+
fn from_residual(residual: R) -> Self;
5+
}
6+
7+
pub trait Try: FromResidual {
8+
type Output;
9+
type Residual;
10+
fn from_output(output: Self::Output) -> Self;
11+
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
12+
}
13+
14+
pub enum ControlFlow<B, C = ()> {
15+
Continue(C),
16+
Break(B),
17+
}

src/test/rustdoc/issue-85454.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// @has issue_85454/trait.FromResidual.html
1+
// aux-build:issue-85454.rs
2+
// build-aux-docs
3+
#![crate_name = "foo"]
4+
5+
extern crate issue_85454;
6+
7+
// @has foo/trait.FromResidual.html
28
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
39
pub trait FromResidual<R = <Self as Try>::Residual> {
410
fn from_residual(residual: R) -> Self;
@@ -15,3 +21,9 @@ pub enum ControlFlow<B, C = ()> {
1521
Continue(C),
1622
Break(B),
1723
}
24+
25+
pub mod reexport {
26+
// @has foo/reexport/trait.FromResidual.html
27+
// @has - '//pre[@class="rust trait"]' 'pub trait FromResidual<R = <Self as Try>::Residual> { fn from_residual(residual: R) -> Self; }'
28+
pub use issue_85454::*;
29+
}

0 commit comments

Comments
 (0)