Auto merge of #85479 - Stupremee:render-Self_as-type-casts, r=CraftSpider
rustdoc: render `<Self as X>::Y` type casts properly Rustdoc didn't render any `<Self as X>` casts which causes invalid code inside the documentation. This is fixed by this PR by checking if the target type `X` is different from `Self`, and if so, it will render a typecast. Resolves #85454
This commit is contained in:
commit
13bf0b2a3c
@ -561,7 +561,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
WherePredicate::EqPredicate { lhs, rhs } => {
|
WherePredicate::EqPredicate { lhs, rhs } => {
|
||||||
match lhs {
|
match lhs {
|
||||||
Type::QPath { name: left_name, ref self_type, ref trait_ } => {
|
Type::QPath { name: left_name, ref self_type, ref trait_, .. } => {
|
||||||
let ty = &*self_type;
|
let ty = &*self_type;
|
||||||
match **trait_ {
|
match **trait_ {
|
||||||
Type::ResolvedPath {
|
Type::ResolvedPath {
|
||||||
|
@ -588,6 +588,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
|
|||||||
self_type: box clean::Generic(ref s),
|
self_type: box clean::Generic(ref s),
|
||||||
trait_: box clean::ResolvedPath { did, .. },
|
trait_: box clean::ResolvedPath { did, .. },
|
||||||
name: ref _name,
|
name: ref _name,
|
||||||
|
..
|
||||||
},
|
},
|
||||||
ref bounds,
|
ref bounds,
|
||||||
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
|
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
|
||||||
|
@ -418,9 +418,11 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
|
|||||||
GenericBound::TraitBound(t, _) => t.trait_,
|
GenericBound::TraitBound(t, _) => t.trait_,
|
||||||
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
|
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
|
||||||
};
|
};
|
||||||
|
let self_type = self.self_ty().clean(cx);
|
||||||
Type::QPath {
|
Type::QPath {
|
||||||
name: cx.tcx.associated_item(self.item_def_id).ident.name,
|
name: cx.tcx.associated_item(self.item_def_id).ident.name,
|
||||||
self_type: box self.self_ty().clean(cx),
|
self_def_id: self_type.def_id(),
|
||||||
|
self_type: box self_type,
|
||||||
trait_: box trait_,
|
trait_: box trait_,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1104,7 +1106,7 @@ impl Clean<Item> for ty::AssocItem {
|
|||||||
.filter_map(|pred| {
|
.filter_map(|pred| {
|
||||||
let (name, self_type, trait_, bounds) = match *pred {
|
let (name, self_type, trait_, bounds) = match *pred {
|
||||||
WherePredicate::BoundPredicate {
|
WherePredicate::BoundPredicate {
|
||||||
ty: QPath { ref name, ref self_type, ref trait_ },
|
ty: QPath { ref name, ref self_type, ref trait_, .. },
|
||||||
ref bounds,
|
ref bounds,
|
||||||
} => (name, self_type, trait_, bounds),
|
} => (name, self_type, trait_, bounds),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
@ -1282,16 +1284,15 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
|
|||||||
|
|
||||||
let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
|
let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
|
||||||
let trait_segments = &segments[..segments.len() - 1];
|
let trait_segments = &segments[..segments.len() - 1];
|
||||||
|
let trait_def = cx.tcx.associated_item(p.res.def_id()).container.id();
|
||||||
let trait_path = self::Path {
|
let trait_path = self::Path {
|
||||||
global: p.is_global(),
|
global: p.is_global(),
|
||||||
res: Res::Def(
|
res: Res::Def(DefKind::Trait, trait_def),
|
||||||
DefKind::Trait,
|
|
||||||
cx.tcx.associated_item(p.res.def_id()).container.id(),
|
|
||||||
),
|
|
||||||
segments: trait_segments.clean(cx),
|
segments: trait_segments.clean(cx),
|
||||||
};
|
};
|
||||||
Type::QPath {
|
Type::QPath {
|
||||||
name: p.segments.last().expect("segments were empty").ident.name,
|
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),
|
self_type: box qself.clean(cx),
|
||||||
trait_: box resolve_type(cx, trait_path, hir_id),
|
trait_: box resolve_type(cx, trait_path, hir_id),
|
||||||
}
|
}
|
||||||
@ -1306,6 +1307,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
|
|||||||
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
|
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
|
||||||
Type::QPath {
|
Type::QPath {
|
||||||
name: segment.ident.name,
|
name: segment.ident.name,
|
||||||
|
self_def_id: res.opt_def_id(),
|
||||||
self_type: box qself.clean(cx),
|
self_type: box qself.clean(cx),
|
||||||
trait_: box resolve_type(cx, trait_path, hir_id),
|
trait_: box resolve_type(cx, trait_path, hir_id),
|
||||||
}
|
}
|
||||||
|
@ -1519,6 +1519,7 @@ crate enum Type {
|
|||||||
QPath {
|
QPath {
|
||||||
name: Symbol,
|
name: Symbol,
|
||||||
self_type: Box<Type>,
|
self_type: Box<Type>,
|
||||||
|
self_def_id: Option<DefId>,
|
||||||
trait_: Box<Type>,
|
trait_: Box<Type>,
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1665,7 +1666,7 @@ impl Type {
|
|||||||
|
|
||||||
crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {
|
crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {
|
||||||
let (self_, trait_, name) = match self {
|
let (self_, trait_, name) = match self {
|
||||||
QPath { self_type, trait_, name } => (self_type, trait_, name),
|
QPath { self_type, trait_, name, .. } => (self_type, trait_, name),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
let trait_did = match **trait_ {
|
let trait_did = match **trait_ {
|
||||||
|
@ -175,8 +175,9 @@ crate fn strip_type(ty: Type) -> Type {
|
|||||||
Type::BorrowedRef { lifetime, mutability, type_ } => {
|
Type::BorrowedRef { lifetime, mutability, type_ } => {
|
||||||
Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
|
Type::BorrowedRef { lifetime, mutability, type_: Box::new(strip_type(*type_)) }
|
||||||
}
|
}
|
||||||
Type::QPath { name, self_type, trait_ } => Type::QPath {
|
Type::QPath { name, self_type, trait_, self_def_id } => Type::QPath {
|
||||||
name,
|
name,
|
||||||
|
self_def_id,
|
||||||
self_type: Box::new(strip_type(*self_type)),
|
self_type: Box::new(strip_type(*self_type)),
|
||||||
trait_: Box::new(strip_type(*trait_)),
|
trait_: Box::new(strip_type(*trait_)),
|
||||||
},
|
},
|
||||||
|
@ -18,7 +18,7 @@ use rustc_span::def_id::CRATE_DEF_INDEX;
|
|||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
|
|
||||||
use crate::clean::{
|
use crate::clean::{
|
||||||
self, utils::find_nearest_parent_module, ExternalCrate, FakeDefId, PrimitiveType,
|
self, utils::find_nearest_parent_module, ExternalCrate, FakeDefId, GetDefId, PrimitiveType,
|
||||||
};
|
};
|
||||||
use crate::formats::item_type::ItemType;
|
use crate::formats::item_type::ItemType;
|
||||||
use crate::html::escape::Escape;
|
use crate::html::escape::Escape;
|
||||||
@ -836,10 +836,13 @@ fn fmt_type<'cx>(
|
|||||||
write!(f, "impl {}", print_generic_bounds(bounds, cx))
|
write!(f, "impl {}", print_generic_bounds(bounds, cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clean::QPath { ref name, ref self_type, ref trait_ } => {
|
clean::QPath { ref name, ref self_type, ref trait_, ref self_def_id } => {
|
||||||
let should_show_cast = match *trait_ {
|
let should_show_cast = match *trait_ {
|
||||||
box clean::ResolvedPath { ref path, .. } => {
|
box clean::ResolvedPath { ref path, .. } => {
|
||||||
!path.segments.is_empty() && !self_type.is_self_type()
|
!path.segments.is_empty()
|
||||||
|
&& self_def_id
|
||||||
|
.zip(trait_.def_id())
|
||||||
|
.map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_)
|
||||||
}
|
}
|
||||||
_ => true,
|
_ => true,
|
||||||
};
|
};
|
||||||
|
@ -396,7 +396,7 @@ impl FromWithTcx<clean::Type> for Type {
|
|||||||
mutable: mutability == ast::Mutability::Mut,
|
mutable: mutability == ast::Mutability::Mut,
|
||||||
type_: Box::new((*type_).into_tcx(tcx)),
|
type_: Box::new((*type_).into_tcx(tcx)),
|
||||||
},
|
},
|
||||||
QPath { name, self_type, trait_ } => Type::QualifiedPath {
|
QPath { name, self_type, trait_, .. } => Type::QualifiedPath {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
self_type: Box::new((*self_type).into_tcx(tcx)),
|
self_type: Box::new((*self_type).into_tcx(tcx)),
|
||||||
trait_: Box::new((*trait_).into_tcx(tcx)),
|
trait_: Box::new((*trait_).into_tcx(tcx)),
|
||||||
|
17
src/test/rustdoc/issue-85454.rs
Normal file
17
src/test/rustdoc/issue-85454.rs
Normal file
@ -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),
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user