Skip to content

Commit a8f2463

Browse files
committed
Auto merge of #88379 - camelid:cleanup-clean, r=jyn514
rustdoc: Cleanup various `clean` types Cleanup various `clean` types.
2 parents bc8ad24 + ebbcafb commit a8f2463

File tree

15 files changed

+415
-514
lines changed

15 files changed

+415
-514
lines changed

src/librustdoc/clean/auto_trait.rs

+143-179
Large diffs are not rendered by default.

src/librustdoc/clean/blanket_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
115115
.clean(self.cx),
116116
// FIXME(eddyb) compute both `trait_` and `for_` from
117117
// the post-inference `trait_ref`, as it's more accurate.
118-
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
118+
trait_: Some(trait_ref.clean(self.cx)),
119119
for_: ty.clean(self.cx),
120120
items: self
121121
.cx

src/librustdoc/clean/inline.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,26 @@ crate fn build_impl(
446446
),
447447
};
448448
let polarity = tcx.impl_polarity(did);
449-
let trait_ = associated_trait.clean(cx).map(|bound| match bound {
450-
clean::GenericBound::TraitBound(polyt, _) => polyt.trait_,
451-
clean::GenericBound::Outlives(..) => unreachable!(),
452-
});
453-
if trait_.def_id() == tcx.lang_items().deref_trait() {
449+
let trait_ = associated_trait.clean(cx);
450+
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
454451
super::build_deref_target_impls(cx, &trait_items, ret);
455452
}
456453

457454
// Return if the trait itself or any types of the generic parameters are doc(hidden).
458-
let mut stack: Vec<&Type> = trait_.iter().collect();
459-
stack.push(&for_);
455+
let mut stack: Vec<&Type> = vec![&for_];
456+
457+
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
458+
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
459+
return;
460+
}
461+
}
462+
if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
463+
stack.extend(generics);
464+
}
465+
460466
while let Some(ty) = stack.pop() {
461467
if let Some(did) = ty.def_id() {
462-
if cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
468+
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
463469
return;
464470
}
465471
}
@@ -468,14 +474,14 @@ crate fn build_impl(
468474
}
469475
}
470476

471-
if let Some(trait_did) = trait_.def_id() {
472-
record_extern_trait(cx, trait_did);
477+
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
478+
record_extern_trait(cx, did);
473479
}
474480

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

478-
trace!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
484+
trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id());
479485
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
480486
did,
481487
None,
@@ -526,7 +532,6 @@ fn build_module(
526532
item.ident.name,
527533
clean::ImportSource {
528534
path: clean::Path {
529-
global: false,
530535
res,
531536
segments: vec![clean::PathSegment {
532537
name: prim_ty.as_sym(),
@@ -621,30 +626,23 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
621626
ref mut bounds,
622627
..
623628
} if *s == kw::SelfUpper => {
624-
bounds.retain(|bound| match *bound {
625-
clean::GenericBound::TraitBound(
626-
clean::PolyTrait { trait_: clean::ResolvedPath { did, .. }, .. },
627-
_,
628-
) => did != trait_did,
629+
bounds.retain(|bound| match bound {
630+
clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
631+
trait_.def_id() != trait_did
632+
}
629633
_ => true,
630634
});
631635
}
632636
_ => {}
633637
}
634638
}
635639

636-
g.where_predicates.retain(|pred| match *pred {
640+
g.where_predicates.retain(|pred| match pred {
637641
clean::WherePredicate::BoundPredicate {
638-
ty:
639-
clean::QPath {
640-
self_type: box clean::Generic(ref s),
641-
trait_: box clean::ResolvedPath { did, .. },
642-
name: ref _name,
643-
..
644-
},
645-
ref bounds,
642+
ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, name: _, .. },
643+
bounds,
646644
..
647-
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
645+
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
648646
_ => true,
649647
});
650648
g

src/librustdoc/clean/mod.rs

+26-42
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
152152
}
153153
}
154154

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

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

171-
ResolvedPath { path, did: trait_ref.def_id }
171+
path
172172
}
173173
}
174174

175-
impl<'tcx> Clean<GenericBound> for ty::TraitRef<'tcx> {
176-
fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound {
177-
GenericBound::TraitBound(
178-
PolyTrait { trait_: (*self, &[][..]).clean(cx), generic_params: vec![] },
179-
hir::TraitBoundModifier::None,
180-
)
175+
impl Clean<Path> for ty::TraitRef<'tcx> {
176+
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
177+
(*self, &[][..]).clean(cx)
181178
}
182179
}
183180

@@ -384,16 +381,13 @@ impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
384381
impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
385382
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
386383
let lifted = self.lift_to_tcx(cx.tcx).unwrap();
387-
let trait_ = match lifted.trait_ref(cx.tcx).clean(cx) {
388-
GenericBound::TraitBound(t, _) => t.trait_,
389-
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
390-
};
384+
let trait_ = lifted.trait_ref(cx.tcx).clean(cx);
391385
let self_type = self.self_ty().clean(cx);
392386
Type::QPath {
393387
name: cx.tcx.associated_item(self.item_def_id).ident.name,
394388
self_def_id: self_type.def_id(),
395389
self_type: box self_type,
396-
trait_: box trait_,
390+
trait_,
397391
}
398392
}
399393
}
@@ -896,10 +890,11 @@ impl Clean<bool> for hir::IsAuto {
896890
}
897891
}
898892

899-
impl Clean<Type> for hir::TraitRef<'_> {
900-
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
893+
impl Clean<Path> for hir::TraitRef<'_> {
894+
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
901895
let path = self.path.clean(cx);
902-
resolve_type(cx, path)
896+
register_res(cx, path.res);
897+
path
903898
}
904899
}
905900

@@ -1105,9 +1100,8 @@ impl Clean<Item> for ty::AssocItem {
11051100
if *name != my_name {
11061101
return None;
11071102
}
1108-
match **trait_ {
1109-
ResolvedPath { did, .. } if did == self.container.id() => {}
1110-
_ => return None,
1103+
if trait_.def_id() != self.container.id() {
1104+
return None;
11111105
}
11121106
match **self_type {
11131107
Generic(ref s) if *s == kw::SelfUpper => {}
@@ -1273,19 +1267,18 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
12731267
return normalized_value.clean(cx);
12741268
}
12751269

1276-
let segments = if p.is_global() { &p.segments[1..] } else { &p.segments };
1277-
let trait_segments = &segments[..segments.len() - 1];
1270+
let trait_segments = &p.segments[..p.segments.len() - 1];
12781271
let trait_def = cx.tcx.associated_item(p.res.def_id()).container.id();
1279-
let trait_path = self::Path {
1280-
global: p.is_global(),
1272+
let trait_ = self::Path {
12811273
res: Res::Def(DefKind::Trait, trait_def),
12821274
segments: trait_segments.clean(cx),
12831275
};
1276+
register_res(cx, trait_.res);
12841277
Type::QPath {
12851278
name: p.segments.last().expect("segments were empty").ident.name,
12861279
self_def_id: Some(DefId::local(qself.hir_id.owner.local_def_index)),
12871280
self_type: box qself.clean(cx),
1288-
trait_: box resolve_type(cx, trait_path),
1281+
trait_,
12891282
}
12901283
}
12911284
hir::QPath::TypeRelative(ref qself, ref segment) => {
@@ -1296,12 +1289,13 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
12961289
ty::Error(_) => return Type::Infer,
12971290
_ => bug!("clean: expected associated type, found `{:?}`", ty),
12981291
};
1299-
let trait_path = hir::Path { span, res, segments: &[] }.clean(cx);
1292+
let trait_ = hir::Path { span, res, segments: &[] }.clean(cx);
1293+
register_res(cx, trait_.res);
13001294
Type::QPath {
13011295
name: segment.ident.name,
13021296
self_def_id: res.opt_def_id(),
13031297
self_type: box qself.clean(cx),
1304-
trait_: box resolve_type(cx, trait_path),
1298+
trait_,
13051299
}
13061300
}
13071301
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
@@ -1470,10 +1464,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14701464
let empty = cx.tcx.intern_substs(&[]);
14711465
let path = external_path(cx, did, false, vec![], empty);
14721466
inline::record_extern_fqn(cx, did, ItemType::Trait);
1473-
let bound = PolyTrait {
1474-
trait_: ResolvedPath { path, did },
1475-
generic_params: Vec::new(),
1476-
};
1467+
let bound = PolyTrait { trait_: path, generic_params: Vec::new() };
14771468
bounds.push(bound);
14781469
}
14791470

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

14881479
let path = external_path(cx, did, false, bindings, substs);
1489-
bounds.insert(
1490-
0,
1491-
PolyTrait { trait_: ResolvedPath { path, did }, generic_params: Vec::new() },
1492-
);
1480+
bounds.insert(0, PolyTrait { trait_: path, generic_params: Vec::new() });
14931481

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

17291717
impl Clean<Path> for hir::Path<'_> {
17301718
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
1731-
Path {
1732-
global: self.is_global(),
1733-
res: self.res,
1734-
segments: if self.is_global() { &self.segments[1..] } else { &self.segments }.clean(cx),
1735-
}
1719+
Path { res: self.res, segments: self.segments.clean(cx) }
17361720
}
17371721
}
17381722

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

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

@@ -1907,7 +1891,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
19071891
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
19081892
_ => None,
19091893
});
1910-
let mut make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
1894+
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
19111895
let kind = ImplItem(Impl {
19121896
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
19131897
unsafety: impl_.unsafety,

src/librustdoc/clean/simplify.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,13 @@ crate fn merge_bounds(
9999
clean::GenericBound::TraitBound(ref mut tr, _) => tr,
100100
clean::GenericBound::Outlives(..) => return false,
101101
};
102-
let (did, path) = match trait_ref.trait_ {
103-
clean::ResolvedPath { did, ref mut path, .. } => (did, path),
104-
_ => return false,
105-
};
106102
// If this QPath's trait `trait_did` is the same as, or a supertrait
107103
// of, the bound's trait `did` then we can keep going, otherwise
108104
// this is just a plain old equality bound.
109-
if !trait_is_same_or_supertrait(cx, did, trait_did) {
105+
if !trait_is_same_or_supertrait(cx, trait_ref.trait_.def_id(), trait_did) {
110106
return false;
111107
}
112-
let last = path.segments.last_mut().expect("segments were empty");
108+
let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
113109
match last.args {
114110
PP::AngleBracketed { ref mut bindings, .. } => {
115111
bindings.push(clean::TypeBinding {

0 commit comments

Comments
 (0)